Click here to Skip to main content
Click here to Skip to main content

WTL Splash Window

By , 10 Jul 2003
 

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

About the Author

Rob Caldecott
Architect
United Kingdom United Kingdom
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Generalconcisememberwshcdr15 Dec '09 - 23:03 
QuestionHow to close host application after timer is up?memberSoliant9 Jun '06 - 10:23 
GeneralUnusual questionmemberBilloKhan10 Feb '06 - 13:48 
GeneralMemory Leaks!!!membervatic16 Jan '06 - 9:34 
GeneralRe: Memory Leaks!!!memberRobert Edward Caldecott16 Jan '06 - 10:12 
GeneralRe: Memory Leaks!!!membervatic17 Jan '06 - 2:39 
GeneralRe: Memory Leaks!!!memberRobert Edward Caldecott17 Jan '06 - 3:22 
GeneralAdding Transparent BitmapsmemberKK Adams18 Aug '05 - 5:33 
GeneralFocus issues for some appsmemberLionel Lobo19 Apr '04 - 7:59 
GeneralRe: Focus issues for some appsmemberRobert Edward Caldecott19 Apr '04 - 21:07 

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 11 Jul 2003
Article Copyright 2003 by Rob Caldecott
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid