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

  • According to a 2014 Javelin Strategy & Research Study, there is a new victim of fraud every two seconds – and that's just in the U.S. With identity theft and consumer fraud rising, and more frequent and sizable data breaches in the news, financial institutions need to work harder than ever to protect their customers and their business. Download this white paper to learn how you can use multi-channel communications to minimize the costs of fraud while also increasing customer loyalty.

  • Finance leaders have been talking about expanding the value-added role of their teams for a long time. The debate is no longer whether the finance and accounting function needs to become a more strategic partner to the rest of the business but rather how to get there. Technology innovation has caught up to this ambition, and what was once aspiration can be a reality – and the choice is now yours. Read this research report to learn how to make the most of information tools to enable innovation and growth.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date