Displays the bitmap as the background of a window or dialog box.

Source: Internet
Author: User

Problem
YesProgramThe engineer wants to replace the annoying gray background in the dialog box with interesting bitmaps in his own applications, it is expected that the bitmap looks like a wallpaper in the dialog box and does not affect the control or static text display in the dialog box.
Many programmers cannot find a simple method to change the window background. Is there a way to use Windows API functions to change the background of a dialog box to a bitmap?

Method
It is not difficult to change the background of a dialog box to a bitmap. The key is to know how the dialog box and window set the background color, and how the programmer should modify the dialog box and window change the display behavior.
When Windows is about to change the background color of the dialog box, two messages are usually sent to the dialog box. The first message is wm_erasebkgnd. This message indicates the background color of the draw dialog box to erase any display in the display area of the dialog box.
The second message is wm_ctlcolor, which is sent to the dialog box or window to indicate that Windows needs to know the color controlled in the dialog box.
In this section, the processing of the message wm_erasebkgnd is reset to draw the bitmap on the background of the window. In addition, the processing of the message wm_ctlcolor is reset to avoid the control of the "cut-and-fill" bitmap in the dialog box. The final result is that the background bitmap of the dialog box is drawn on the background of the dialog box and "above" is controlled on the background bitmap ".

Procedure
Follow these steps to implement an example program. Run this subroutine and select menu dialog and menu item bitmap background. A dialog box is displayed, showing the background bitmap and several controls.

The procedure for implementing the example program is as follows:
1. in Visual C ++, use Appwizard to create a new project file and name it ld145.
2. Go to the resource editor and create a new dialog box template. In the dialog box, add several static text fields and editing fields, as well as several radio buttons and list boxes. The actual composition of the dialog box is not important, as long as some bitmaps can be overwritten.
3. Select classwizard to create a dialog box class for the newly created dialog box template. The new class is named cbitmapbkgddlg.
4. Create a New bitmap in the resource editor.
5. enter classwizard, select cbitmapbkgddlg from the drop-down list, select cbitmapbkgddlg from the object list, select the message wm_initdialog from the message list, click the Add function button, and add the following to the cbitmapbkgddlg MethodCode:

Bool cbitmapbkgddlg: oninitdialog ()
{
Cbitmap * pbmpold;
Rect rectclient;
Verify (m_brush = (hbrush) getstockobject (hollow_brush ));
Verify (m_bitmap.loadbitmap (idb_bitmap1 ));

M_bitmap.getobject (sizeof (Bitmap), & m_bminfo );
Getclientrect (& rectclient );
M_size.cx = rectclient. Right;
M_size.cy = rectclient. bottom;
M_pt.x = rectclient. Left;
M_pt.y = rectclient. Top;
Cclientdc DC (this );
Verify (m_dcmem.createcompatibledc (& DC ));
Verify (pbmpold = m_dcmem.selectobject (& m_bitmap ));
Verify (m_hbmpold = (hbitmap) pbmpold-> getsafehandle ());

Return true; // return true unless you set the focus to a control
}

6. Then, in classwizard, select the object cbitmapbkgddlg from the object list, select the message wm_ctlcolor from the message list, click the Add function button, and add the following code to the onctlcolor method of cbitmapbkgddlg:

Hbrush cbitmapbkgddlg: onctlcolor (CDC * PDC, cwnd * pwnd, uint nctlcolor)
{
PDC-> setbkmode (transparent );

Return m_brush;
}

7. Then, in classwizard, select the object cbitmapbkgddlg from the object list, select the message wm_destroy from the message list, click the Add function button, and add the following code to the ondestroy method of cbitmapbkgddlg:

Void cbitmapbkgddlg: ondestroy ()
{
Cdialog: ondestroy ();

Assert (m_hbmpold );
Verify (m_dcmem.selectobject (cbitmap: fromhandle (m_hbmpold )));

M_bitmap.deleteobject ();
}

8. Edit the message ing of cbitmapbkgddlg as follows. The new lines are marked in red:

Begin_message_map (cbitmapbkgddlg, cdialog)
// {Afx_msg_map (cbitmapbkgddlg)
On_wm_ctlcolor ()
On_wm_destroy ()
On_wm_erasebkgnd ()
//} Afx_msg_map
End_message_map ()

9. Add the following methods to the source file bitmapbkgddlg. cpp of cbitmapbkgddlg:

Bool cbitmapbkgddlg: onerasebkgnd (CDC * PDC)
{
PDC-> stretchblt (m_pt.x, m_pt.y, m_size.cx, m_size.cy, & m_dcmem,
0 0, m_bmInfo.bmWidth-1, m_bmInfo.bmHeight-1, srccopy );

Return true;
}

10. Make the following changes in the header file bitmapbkgddlg. h of cbitmapbkgddlg, which is in a dark red font.

Class cbitmapbkgddlg: Public cdialog
{
Protected:
CDC m_dcmem;
Cbitmap m_bitmap;
Hbitmap m_hbmpold;
Hbrush m_brush;
Bitmap m_bminfo;
Cpoint m_pt;
Csize m_size;

// Construction
Public:
Cbitmapbkgddlg (cwnd * pparent = NULL); // standard Constructor

// Dialog data
// {Afx_data (cbitmapbkgddlg)
Enum {IDD = idd_dialog1 };
// Note: The classwizard will add data members here
//} Afx_data

// Overrides
// Classwizard generated virtual function overrides
// {Afx_virtual (cbitmapbkgddlg)
Protected:
Virtual void dodataexchange (cdataexchange * PDX); // DDX/DDV support
//} Afx_virtual

// Implementation
Protected:

// Generated message map Functions
// {Afx_msg (cbitmapbkgddlg)
Virtual bool oninitdialog ();
Afx_msg hbrush onctlcolor (CDC * PDC, cwnd * pwnd, uint nctlcolor );
Afx_msg void ondestroy ();
Virtual bool onerasebkgnd (CDC * PDC );
//} Afx_msg
Declare_message_map ()
};

11. Go to the resource editor and add a new menu named "dialog" to the menu idr_mainframe. Add a new menu item in the menu dialog. The title is bitmap background and the identifier is id_bitmap_bkgnd. Exit the resource editor and save the resource file.
12. Enter classwizard, select the object cmainframe from the drop-down list, select the object id_bitmap_bkgnd from the object list, select the message command from the message list, and click the Add function button. The new function is named onbitmapbkgnd. Add the following code to the onbitmapbkgnd method of cmainframe:

Void cmainframe: onbitmapbkgnd ()
{
Cbitmapbkgddlg DLG;

DLG. domodal ();
}

13. Add the following lines at the top of the source file mainfrm. cpp:
# Include "bitmapbkgddlg. H"
14. Compile and run this subprogram.

usage
in the Windows initialization dialog box, wm_erasebkgnd is sent to the window handle of the dialog box. The programmer can capture this message to erase the background of the dialog box in the application. In this section, first capture the message, and then call the API function stretchblt to copy the bitmap (loaded from the resource file) to the background of the dialog box.
In the onctlcolor method of the dialog box, you can set the background mode to transparent to ensure that the control in the dialog box does not "cut and fill" the fill image, so that the bitmap looks as drawn in the dialog box, blank due to the absence of static control background.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.