Click here to Skip to main content
15,885,914 members
Articles / Desktop Programming / MFC
Article

Drawing Image on MDI Main Frame background

Rate me:
Please Sign up or sign in to vote.
4.90/5 (12 votes)
26 Mar 20022 min read 157.2K   3.9K   59   24
This article explains how to draw images on MDI Main Frame background using window subclassing

Image 1

Introduction

Some time ago I worked with Altera Max Plus II to design simple electronic chip. I noticed that this application draws a company logo on Main Frame background. Once I had more free time then usually, I wanted to implement this by myself. This article is the result :)

Implementation

So you may wonder how to draw image on background window of MDI application. First you have to subclass window you want to draw on. To do this call GetWindowLong(hMain, GWL_WNDPROC). This function returns pointer to old window procedure and we want to store it for further use. Place this code in CBackgroundImageApp::InitInstance() just before showing the Main Frame.

HWND hMain = pMainFrame->GetWindow(GW_CHILD)->GetSafeHwnd();
pfnOldWndProc = (WNDPROC)GetWindowLong(hMain, GWL_WNDPROC);
SetWindowLong(hMain, GWL_WNDPROC, (long)pfnNewWndProc);

Now we have sub-classed window, but the window procedure is missing. Lets write it now. The window procedure should handle at least two messages to work well :

  • WM_ERASEBKGND
  • WM_SIZE

First message is sent when Windows wants application to draw its background - we'll use this to draw our image. Second message is also important because when you resize window image should also change its size to fit into new window size. Our window proc would look like this :

LRESULT CALLBACK pfnNewWndProc(HWND hwnd, 
       UINT uMsg, WPARAM wParam,LPARAM lParam)
{
    // local variables goes here

    switch (uMsg)   {
        case WM_SIZE :
            // ...
        case WM_ERASEBKGND :
            // ...
            return 1;
        default :
            return CallWindowProc(pfnOldWndProc,
                hwnd, uMsg, wParam, lParam);
    }
}

Important note

Returing 1 from WM_ERASEBKGND case is very important because it tells Windows that we already erased window and it has nothing to do, otherwise Windows would erase this window again with standard background, which I suppose is not we wanted to achieve.

What about other messages ?

Here is the moment we are using stored old window procedure pointer. We simply call old procedure and return its result. So lets look closer to the WM_SIZE and WM_ERASEBKGND cases.

WM_SIZE - Simply force window background to redraw itself by sending WM_ERASEBKGND to it :

SendMessage(hwnd, WM_ERASEBKGND, (WPARAM)GetDC(hwnd), 0);
return 1;

WM_ERASEBKGND - Code below will simply BitBlt the bitmap. Because the window size generally very rarely is the same as image size BitBlt() becomes StretchBlt(). Finally it looks like below :

hcompdc = CreateCompatibleDC(hdc);
SelectObject(hcompdc, hBmp);
GetClientRect(hwnd, &rect);

if (0 == StretchBlt(hdc, 
    rect.left, rect.top, rect.right, rect.bottom,
    hcompdc, 0, 0, 1152, 864, SRCCOPY))
    MessageBox(hwnd, "error",
        "while StretchBlt", MB_OK);

DeleteDC(hcompdc);
return 1;

Where :

  • hdc, hcompdc - Device context
  • hBmp - bitmap handle

It is very important to invoke DeleteDC() because we don't want to cause memory leaks.

Final notes

Performance of this solution strongly depends on operating system , graphics card installed, and on window size. In my case Windows 98 was better choice than Windows 2000. On second one redrawing an image took significantly more time to make the user feel comfortably. Application running on Windows 98 looked very nice. My graphics device is Geforce 2 GTS. If you have experiences from other systems or configurations please post it below at article's discussion board. StretchBlt function also differs between systems. One used in Windows 98 was better this time too.

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


Written By
Web Developer
Poland Poland
At the moment I'm a student at Warsaw University of Technology in Poland.

My programming langugage of choice is C++.
I also like C# and Java.

Comments and Discussions

 
GeneralInitial Drawing Pin
Dimitrios Fountoukidis26-Oct-20 7:18
Dimitrios Fountoukidis26-Oct-20 7:18 
GeneralWM_SIZE Pin
mail762628-Jun-10 16:44
mail762628-Jun-10 16:44 
Generalwhy can't we do it in mainframe Pin
baolin,jiang2-Jul-07 21:44
baolin,jiang2-Jul-07 21:44 
GeneralRe: why can't we do it in mainframe Pin
James Vanderhyde20-Feb-09 10:45
James Vanderhyde20-Feb-09 10:45 
GeneralA new found Pin
yongbin_jing26-Oct-06 17:27
yongbin_jing26-Oct-06 17:27 
GeneralClick on Background Pin
blubb24-Oct-06 7:35
blubb24-Oct-06 7:35 
GeneralRe: Click on Background Pin
blubb24-Oct-06 8:07
blubb24-Oct-06 8:07 
GeneralHard-Coded image size Pin
jocool15-Sep-04 4:07
jocool15-Sep-04 4:07 
GeneralTile not Stretch Pin
S.Cartwright17-Jun-04 16:19
S.Cartwright17-Jun-04 16:19 
GeneralRe: Tile not Stretch Pin
S.Cartwright17-Jun-04 16:29
S.Cartwright17-Jun-04 16:29 
Generalchild windows does not maximize Pin
Camran10-Feb-04 8:36
Camran10-Feb-04 8:36 
GeneralRe: child windows does not maximize Pin
mickdo18-Oct-04 20:30
mickdo18-Oct-04 20:30 
GeneralRe: child windows does not maximize Pin
Pavel Machyniak13-Oct-06 4:23
Pavel Machyniak13-Oct-06 4:23 
GeneralReading the Pixels From the Graph Pin
Rajeev Kumar M5-Feb-04 18:36
Rajeev Kumar M5-Feb-04 18:36 
Generalchild windows overpaint Pin
Rostislaw7-Oct-03 11:18
Rostislaw7-Oct-03 11:18 
GeneralImage quality Pin
ajhuddy26-Jun-03 5:22
ajhuddy26-Jun-03 5:22 
GeneralRe: Image quality Pin
ajhuddy26-Jun-03 5:29
ajhuddy26-Jun-03 5:29 
GeneralRe: Image quality Pin
WhiTeWo1F6-Nov-05 10:34
WhiTeWo1F6-Nov-05 10:34 
GeneralRe: Image quality Pin
drai_kin4-Apr-06 0:09
drai_kin4-Apr-06 0:09 
GeneralGreat article, but... Pin
1-Apr-02 20:27
suss1-Apr-02 20:27 
GeneralRe: Great article, but... Pin
1-Apr-02 20:54
suss1-Apr-02 20:54 
GeneralWM_ERASEBKGND Pin
Paul M Watt1-Apr-02 21:13
mentorPaul M Watt1-Apr-02 21:13 
GeneralRe: WM_ERASEBKGND Pin
Konrad Rotuski3-Apr-02 1:17
Konrad Rotuski3-Apr-02 1:17 
Generalm_hWndMDIClient Pin
Tomasz Sowinski27-Mar-02 3:36
Tomasz Sowinski27-Mar-02 3:36 

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.