Dialog with Splash Screen Example Code

Download Example Project - (142 kb)


Follow these steps to add a splash screen to your dialog based application:

  • Copy the Splash.cpp and Splash.h files from this example project to your project.
  • Add the following code to your applications InitInstance() method in the CWinApp derived class:
  • #include "Splash.h"
    
    BOOL CDialogsplApp::InitInstance()
    {
      // CG: The following block was added by the Splash Screen component.
      {
         CCommandLineInfo cmdInfo;
         ParseCommandLine(cmdInfo);
    
         CSplashWnd::EnableSplashScreen(cmdInfo.m_bShowSplash);
      }
      ...
      ...
    }
    
  • Next, use class wizard to add the OnCreate method to the CDialog derived class .cpp file, and add the following code to it:
  • #include "Splash.h"
    
    int CDialogsplDlg::OnCreate(LPCREATESTRUCT lpCreateStruct) 
    {
       if (CDialog::OnCreate(lpCreateStruct) == -1)
          return -1;
       
       // CG: The following line was added by the Splash Screen component.
       CSplashWnd::ShowSplashScreen(this);
    
       return 0;
    }
    

Last updated: 29 March 1998


Comments

  • Added code to specify bmp file.

    Posted by kluzra on 06/18/2004 04:45pm

    I like the splasher code.  Works great.  I added the following code to use a file or resource item.  Code checks for filename in the home directory of the app, specified in string constant IDS_SPLASH_FILE, i.e. splash.bmp.  If it doesn't exist, then it will default to the resource, IDB_SPLASH.
    
    Splash.h:
    protected:
    	BOOL Create(CWnd* pParentWnd = NULL);
    	void HideSplashScreen();
    	static BOOL c_bShowSplashWnd;
    	static CSplashWnd* c_pSplashWnd;
    //New code
    	BOOL LoadBitmap();
    	CPalette  m_Palette;
    	int       m_nHeight;
    	int       m_nWidth;
    
    Splash.cpp:
    
    #include NameApp.h
    
    BOOL CSplashWnd::Create(CWnd* pParentWnd /*= NULL*/)
    {
    //	if (!m_bitmap.LoadBitmap(IDB_SPLASH))
    //		return FALSE;
    
            // Call LoadBitmap to load file or resource
    	VERIFY(LoadBitmap());
    ...
    }
    
    BOOL CSplashWnd::LoadBitmap()
    {     
    	CString mBmpName;
    
    	mBmpName.LoadString(IDS_SPLASH_FILE);
    	//Tell the splash screen which bitmap to use, 
    	CString mBitMap = ((CNameApp *)AfxGetApp())->GetHomeDirectory() + mBmpName;
    	CFileFind	mFind ;
    
      //Use LoadImage to get the image loaded into a DIBSection
      HBITMAP hBitmap;
      if (mFind.FindFile(mBitMap) )
        hBitmap = (HBITMAP) ::LoadImage(NULL, mBitMap, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_DEFAULTSIZE | LR_LOADFROMFILE);
      else
        hBitmap = (HBITMAP) ::LoadImage(AfxGetResourceHandle(), MAKEINTRESOURCE(IDB_SPLASH), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_DEFAULTSIZE);
    
      //Check that we could load it up  
      if (hBitmap == NULL)       
        return FALSE;
    
      //Get the color depth of the DIBSection
      BITMAP bm;
      GetObject(hBitmap, sizeof(BITMAP), &bm);
      m_nHeight = bm.bmHeight;
      m_nWidth = bm.bmWidth;
    
      //Covert from the SDK bitmap handle to the MFC equivalent
      m_bitmap.Attach(hBitmap);
    
      //If the DIBSection is 256 color or less, it has a color table
      if ((bm.bmBitsPixel * bm.bmPlanes) <= 8 )     
      {
        //Create a memory DC and select the DIBSection into it
        CDC memDC;
        memDC.CreateCompatibleDC(NULL);
        CBitmap* pOldBitmap = memDC.SelectObject(&m_bitmap);
    
        //Get the DIBSection's color table
        RGBQUAD rgb[256];
        ::GetDIBColorTable(memDC.m_hDC, 0, 256, rgb);
    
        //Create a palette from the color table
        LPLOGPALETTE pLogPal = (LPLOGPALETTE) new BYTE[sizeof(LOGPALETTE) + (256*sizeof(PALETTEENTRY))];
        pLogPal->palVersion = 0x300;       
        pLogPal->palNumEntries = 256;
    
        for (WORD i=0; i<256; i++)       
        {
          pLogPal->palPalEntry[i].peRed = rgb[i].rgbRed;
          pLogPal->palPalEntry[i].peGreen = rgb[i].rgbGreen;
          pLogPal->palPalEntry[i].peBlue = rgb[i].rgbBlue;
          pLogPal->palPalEntry[i].peFlags = 0;
        }
        VERIFY(m_Palette.CreatePalette(pLogPal));
        
        //Clean up
        delete pLogPal;
        memDC.SelectObject(pOldBitmap);
      }
      else  //It has no color table, so use a halftone palette     
      {
        CDC* pRefDC = GetDC();
        m_Palette.CreateHalftonePalette(pRefDC);
        ReleaseDC(pRefDC);     
      }     
    
      return TRUE;   
    }
    
    
    NameApp.h
    public:
    	CString GetHomeDirectory();
    
    NameApp.cpp:
    
    CString CNameApp::GetHomeDirectory()
    {
      TCHAR sFilename[_MAX_PATH];
      TCHAR sDrive[_MAX_DRIVE];
      TCHAR sDir[_MAX_DIR];
      TCHAR sFname[_MAX_FNAME];
      TCHAR sExt[_MAX_EXT];
      GetModuleFileName(AfxGetInstanceHandle(), sFilename, _MAX_PATH);
      _tsplitpath(sFilename, sDrive, sDir, sFname, sExt);
    
      CString rVal(CString(sDrive) + CString(sDir));
      int nLen = rVal.GetLength();
      if (rVal.GetAt(nLen-1) != _T('\\'))
        rVal += _T("\\");
    
      return rVal;
    }

    Reply
  • Display Splash Longer.

    Posted by Legacy on 11/13/2003 12:00am

    Originally posted by: Bulldog XTRM

    I'm new to programming.

    I'm trying to make a simple text based program and have included a splash screen with photo. Currently the program works as intended, but the splash screen is extremely fast. How can I add a delay so that the splash screen is displayed for a longer time? I'm using MS Visual Basic 6.0.

    • SetTimer

      Posted by kluzra on 06/18/2004 04:13pm

      In the SetTimer line, SetTimer(1, 750, NULL); change the 750 to a higher number. Russ

      Reply
    Reply
  • how to show the .gif type pictures

    Posted by Legacy on 07/03/2001 12:00am

    Originally posted by: wangsh

    i find the background picture used in the splashscreen is .bmp type picture,i want to show a moving picture such as .gif type picture,how do?

    Reply
  • How to add delay for displaying the main window

    Posted by Legacy on 03/25/1999 12:00am

    Originally posted by: V

    How can I add a delay so that the main window isn't shown until the splash screen is gone?

    I tried to hide the main window, and show it when the HideSplashScreen function is called, but I always get a very short flash of the outline of the main window before it gets hidden.

    Otherwise, it looks like it's working fine.

    Any ideas?

    -V-

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • In support of their business continuity and disaster recovery plans, many midsized companies endeavor to avoid putting all their eggs in one basket. Understanding the critical role of last-mile connectivity and always available Internet access for their enterprises, savvy firms utilize redundant connections from multiple service providers. Despite the good intentions, their Internet connectivity risk may still be in a single basket. That is because internet service providers (ISPs) and competitive local …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds