Click here to Skip to main content
Licence 
First Posted 10 Jul 2003
Views 51,248
Bookmarked 24 times

WTL Splash Window

By | 10 Jul 2003 | Article
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

About the Author

Rob Caldecott

Architect

United Kingdom United Kingdom

Member



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

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalconcise Pinmemberwshcdr23:03 15 Dec '09  
QuestionHow to close host application after timer is up? PinmemberSoliant10:23 9 Jun '06  
GeneralUnusual question PinmemberBilloKhan13:48 10 Feb '06  
GeneralMemory Leaks!!! Pinmembervatic9:34 16 Jan '06  
GeneralRe: Memory Leaks!!! PinmemberRobert Edward Caldecott10:12 16 Jan '06  
GeneralRe: Memory Leaks!!! Pinmembervatic2:39 17 Jan '06  
GeneralRe: Memory Leaks!!! PinmemberRobert Edward Caldecott3:22 17 Jan '06  
GeneralAdding Transparent Bitmaps PinmemberKK Adams5:33 18 Aug '05  
GeneralFocus issues for some apps PinmemberLionel Lobo7:59 19 Apr '04  
GeneralRe: Focus issues for some apps PinmemberRobert Edward Caldecott21:07 19 Apr '04  

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

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

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