Click here to Skip to main content
15,867,835 members
Articles / Desktop Programming / WTL
Article

WTL Splash Window

Rate me:
Please Sign up or sign in to vote.
4.06/5 (12 votes)
10 Jul 2003 69K   2K   26   10
Display a splash screen when your WTL app starts

Introduction

Ever wanted to display a splash bitmap when starting your WTL app?  Well here's how - and you only need to add two lines of code to your project!  Marvellous.

First include the header file:

#include "splashwnd.h"

Then, in your apps Run function simply create a new CSplashWnd object after creating your main window, passing it the resource ID of the image you want to display:

new CSplashWnd(IDB_SPLASH);

That's it!  It couldn't be much easier.

You can also supply a timeout value and the handle of an existing window (used when centering the splash) to the constructor.  For example, to display the splash for 3 seconds, centered over your main window:

if(wndMain.CreateEx() == NULL)
{
  ATLTRACE(_T("Main window creation failed!\n"));
  return 0;
} 
wndMain.ShowWindow(nCmdShow); 
new CSplashWnd(IDB_SPLASH, 3000, wndMain.m_hWnd);

Note that you can dispense with the splash window by left/right-clicking it with the mouse or by pressing ESC.

CSplashWnd

Here is the source:

#pragma once
 
#include <atlmisc.h>
 
class CSplashWnd :
 public CWindowImpl<CSplashWnd, CWindow, 
    CWinTraits<WS_POPUP|WS_VISIBLE, WS_EX_TOOLWINDOW> >
{
private:
 enum
 {
  DEF_TIMER_ID  = 1001,
  DEF_TIMER_ELAPSE = 2500,
 };
private:
 CBitmap m_bmp;
 int m_nTimeout;
 HWND m_hParent;
public:
 CSplashWnd(UINT nBitmapID, int nTimeout = DEF_TIMER_ELAPSE, 
    HWND hParent = NULL)
  : m_nTimeout(nTimeout)
  , m_hParent(hParent)
 {
  // Load the bitmap
  if (!m_bmp.LoadBitmap(nBitmapID))
  {
   ATLTRACE(_T("Failed to load spash bitmap\n"));
   return;
  }
  // Get the bitmap size
  CSize size;
  if (!m_bmp.GetSize(size))
  {
   ATLTRACE(_T("Failed to get spash bitmap size\n"));
   return;
  }
  // Create the window rect (we will centre the window later)
  CRect rect(0, 0, size.cx, size.cy);
  // Create the window
  if (!Create(NULL, rect))
  {
   ATLTRACE(_T("Failed to create splash window\n"));
   return;
  }
  UpdateWindow();
 }

 /// Called when the window is destroyed
 virtual void OnFinalMessage(HWND /*hWnd*/)
 {
  delete this;
 }

 BEGIN_MSG_MAP(CSplashWnd)
  MESSAGE_HANDLER(WM_CREATE, OnCreate)
  MESSAGE_HANDLER(WM_PAINT, OnPaint)
  MESSAGE_HANDLER(WM_TIMER, OnTimer)
  MESSAGE_HANDLER(WM_ERASEBKGND, OnEraseBkgnd)
  MESSAGE_HANDLER(WM_LBUTTONDOWN, OnButtonDown)
  MESSAGE_HANDLER(WM_RBUTTONDOWN, OnButtonDown)
  MESSAGE_HANDLER(WM_KEYDOWN, OnKeyDown)
 END_MSG_MAP()
 
 LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, 
    LPARAM /*lParam*/, BOOL& /*bHandled*/)
 {
  CenterWindow(m_hParent);
  // Set the timer
  SetTimer(DEF_TIMER_ID, m_nTimeout);
  return 0;
 }

 LRESULT OnPaint(UINT /*uMsg*/, WPARAM /*wParam*/, 
    LPARAM /*lParam*/, BOOL& /*bHandled*/)
 {
  // Draw the bmp
  CPaintDC dc(m_hWnd);
  
  CDC dcImage;
  if (dcImage.CreateCompatibleDC(dc.m_hDC))
  {
   CSize size;
   if (m_bmp.GetSize(size))
   {
    HBITMAP hBmpOld = dcImage.SelectBitmap(m_bmp);
    dc.BitBlt(0, 0, size.cx, size.cy, dcImage, 0, 0, SRCCOPY);
    dcImage.SelectBitmap(hBmpOld);
   }
  }

  return 0;
 }

 LRESULT OnTimer(UINT /*uMsg*/, WPARAM /*wParam*/, 
    LPARAM /*lParam*/, BOOL& /*bHandled*/)
 {
  KillTimer(DEF_TIMER_ID);
  PostMessage(WM_CLOSE);
  return 0;
 }
 
 LRESULT OnEraseBkgnd(UINT /*uMsg*/, WPARAM /*wParam*/, 
    LPARAM /*lParam*/, BOOL& /*bHandled*/)
 {
  // No need to paint a background
  return TRUE;
 }

 LRESULT OnButtonDown(UINT /*uMsg*/, WPARAM /*wParam*/, 
    LPARAM /*lParam*/, BOOL& /*bHandled*/)
 {
  PostMessage(WM_CLOSE);
  return 0;
 }

 

 LRESULT OnKeyDown(UINT /*uMsg*/, WPARAM wParam, 
    LPARAM /*lParam*/, BOOL& /*bHandled*/)
 {
  if (wParam == VK_ESCAPE)
   PostMessage(WM_CLOSE);
  return 0;
 }
};

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here



Comments and Discussions

 
Generalconcise Pin
wshcdr15-Dec-09 23:03
wshcdr15-Dec-09 23:03 
well done! Laugh | :laugh:

It is better of me!

QuestionHow to close host application after timer is up? Pin
TigerNinja_9-Jun-06 10:23
TigerNinja_9-Jun-06 10:23 
GeneralUnusual question Pin
AghaKhan10-Feb-06 13:48
AghaKhan10-Feb-06 13:48 
GeneralMemory Leaks!!! Pin
vatic16-Jan-06 9:34
vatic16-Jan-06 9:34 
GeneralRe: Memory Leaks!!! Pin
Rob Caldecott16-Jan-06 10:12
Rob Caldecott16-Jan-06 10:12 
GeneralRe: Memory Leaks!!! Pin
vatic17-Jan-06 2:39
vatic17-Jan-06 2:39 
GeneralRe: Memory Leaks!!! Pin
Rob Caldecott17-Jan-06 3:22
Rob Caldecott17-Jan-06 3:22 
GeneralAdding Transparent Bitmaps Pin
KK Adams18-Aug-05 5:33
KK Adams18-Aug-05 5:33 
GeneralFocus issues for some apps Pin
Feature Extraction19-Apr-04 7:59
Feature Extraction19-Apr-04 7:59 
GeneralRe: Focus issues for some apps Pin
Rob Caldecott19-Apr-04 21:07
Rob Caldecott19-Apr-04 21:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.