Click here to Skip to main content
6,629,377 members and growing! (21,343 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » WTL » General     Intermediate License: The Code Project Open License (CPOL)

CDialogRegionT - Skinning a WTL Dialog

By Robin Debnath

Simplest way to skin a WTL Dialog in 3 easy steps
C++, Windows, Visual Studio, WTL, Dev, Design
Posted:8 Sep 2006
Updated:29 Mar 2007
Views:24,616
Bookmarked:48 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
18 votes for this article.
Popularity: 6.05 Rating: 4.82 out of 5

1

2

3
2 votes, 11.1%
4
16 votes, 88.9%
5

CDialogRegionT - Sample Image

Introduction

Having a hard time putting a skin on a WTL Dialog, or want to create transparent dialogs? From here on you'll have to write three odd lines of code! You can create dialogs with the same shape of the supplied bitmap with a transparency feature.

Background

Earlier, if you wanted to skin a dialog you had to handle WM_ERASEBKGND message and then write the paint routine by yourself.

BEGIN_MSG_MAP(CMainDlg)
    MESSAGE_HANDLER(WM_ERASEBKGND, OnEraseBkGnd)

CDialogRegionT template takes all of the burden from the programmer and does everything by itself. There are a couple of more messages which are taken care by this template class. These are WM_CTLCOLORSTATIC and WM_NCHITTEST. Handling WM_NCHITTEST message in our case allows us to drag the dialog by clicking anywhere on the dialog; this is done by returning HTCAPTION in this routine.

LRESULT OnNCHitTest(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, 
    BOOL& bHandled)
{
    // We handle this message only if we have set the region

    bHandled = m_bIsRgnSet;

    if(m_bDlgMove)    // check if we can move the dialog

    {
        return HTCAPTION;
    }
    else
    {
        return 0;
    }
}

Using the code

I attempted to make the code usage as simple as possible. After including the DialogRegion.h in your WTL dialog class you just have to follow these three steps:

  1. Inherit the WTL dialog from CDialogRegionT template class.
    class CMainDlg : public CDialogImpl<CMainDlg>, public CUpdateUI<CMainDlg>,
        public CMessageFilter, public CIdleHandler, CDialogRegionT<CMainDlg>
    {
  2. In the message map add CHAIN_MSG_MAP entry to chain messages to this class
  3.     BEGIN_MSG_MAP(CMainDlg)
            MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
            COMMAND_ID_HANDLER(ID_APP_ABOUT, OnAppAbout)
            COMMAND_ID_HANDLER(IDOK, OnOK)
            COMMAND_ID_HANDLER(IDCANCEL, OnCancel)
            CHAIN_MSG_MAP(CDialogRegionT<CMainDlg>)
        END_MSG_MAP()
  4. Use SetBkBitmap method in the WM_INITDIALOG routine to specify the skin file. SetBkBitmap is an overloaded function and takes the color which is to be set as transparent (RGB(255,0,255) is default transparent color)

    • If you are providing a resource bitmap:
      SetBKBitmap(IDB_BITMAP1);
      Here IDB_BITMAP1 is the bitmap resource identifier.

    • In case you are providing a bitmap file:
      SetBKBitmap("MyBitmap.bmp");
      Here MyBitmap.bmp is the name of bitmap file in the directory of the target executable generated.

Things to note

In the background section I did not discuss the WM_CTLCOLORSTATIC message. The CDialogRegionT template class uses this message to return the color for the static controls. In our case we will be using any color bitmap so the static controls needs to be drawn as transparent. So when the static controls are to be colored we set the background color as transparent and return hollow brush.

LRESULT OnCtlColorStatic(UINT /*uMsg*/, WPARAM wParam, LPARAM lParam, 
    BOOL& bHandled)
{
    // We handle this message only if we have set the region

    bHandled = m_bIsRgnSet;

    if(m_bIsRgnSet)
    {
        ::SetBkMode((HDC)wParam, TRANSPARENT);
        return (LRESULT) GetStockObject (HOLLOW_BRUSH);
    }
    return 0;
}

I came across one problem: if I add the manifest for common controls 6, the static controls are drawn black. So you have to ensure that you are not using the common controls 6 in your project. You can choose not to add common controls manifest when you are creating the project using the WTL project wizard. If you already have a WTL project and upon adding CDialogRegionT class make the static controls to be drawn black, you can remove the common controls manifest from the .rc file.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Robin Debnath


Member

Occupation: Software Developer (Senior)
Location: India India

Other popular WTL articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 4 of 4 (Total in Forum: 4) (Refresh)FirstPrevNext
GeneralBug Report Pinmemberhawkgao01296:22 2 Jun '09  
GeneralGreat PinmemberSachin_Developer20:34 30 Apr '08  
GeneralRe: Great Pinmemberjoyjjjz22:14 22 Sep '08  
GeneralSupporting visual styles PinmemberJim Barry15:34 11 Sep '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 29 Mar 2007
Editor: Sean Ewington
Copyright 2006 by Robin Debnath
Everything else Copyright © CodeProject, 1999-2009
Web21 | Advertise on the Code Project