Create a personalized Dialog Box Using VC ++

Source: Internet
Author: User

This article involves the following questions:

1. Modify the background color of the dialog box.
2. Use bitmap as the background of the dialog box
3. Change the text and background color of the static control.
4. Change the text and background color of the edit box.
5. Change the text and background color of the checkbox.
6. Change the text and background color of radiobox.
7. Change the background color and text color of the button.
8. Use the picture control in the dialog box.
9. Modify the bitmap displayed by the picture control.
10. Use LoadImage to load bitmap from a resource

If you want to make your software stand out, you need to add a "color" to the software. A color-matching window is more eye-catching than a Windows-like dark box. Imagine if all the web pages displayed in the HTML browser are white and black characters, will there be so many MMS who prefer to access the Internet? The popularity of the Internet may be halved. It may be difficult for a veteran to make a good interface, but it is a good choice for beginners to use bcgcontrolbar and Xtreme toolkit, however, it seems that a small program uses such a large library. In fact, you can make a very "color" interface without using these giants. This article introduces several methods to color the Dialog Box Based on the frequently asked questions on the csdn forum. The methods in this article are for the MFC program. For other methods, see "Create a personalized dialog box for ATL/wtl ".

Step 1: change the background color of the dialog box

The question of how to change the background color of the dialog box often appears on the Forum. It can be seen that you are not satisfied with the default gray dialog box in windows. The simplest way to modify the background and text color of the dialog box of the MFC program is to call the setdialogbkcolor function. setdialogbkcolor is a member function of the cwinapp class. The following is the prototype of the function:

 

Void cwinapp: setdialogbkcolor (colorref clrctlbk, colorref clrctltext );

 

Note that the setdialogbkcolor function is not an encapsulation of an API of windows. It is part of the MFC framework, so it is not convenient to use a program without using MFC. This function is easy to use. Just add a line of code to the initinstance function of the cwinapp derived class:

 

Setdialogbkcolor (RGB (188,197,230), RGB (13,125,188 ));

 

Figure. 1 shows the running effect:

Figure. 1 setdialogbkcolor

The use of setdialogbkcolor is also limited, that is, all controls have the same text color. Different text colors cannot be set for different controls, or the edit control color cannot be set. Without using the setdialogbkcolor function, it is not difficult to directly write the background color of the code control dialog box and the text color of the control. In addition, this method can provide more flexible color settings, for example, you can use different text colors for different types of controls and highlight a widget with a high-brightness background color. The most important thing is that you can control the text and background colors of the Edit Control, this method is described below.
First, change the background color of the dialog box. When the Windows system needs to redraw the background of a window's client area, it will send the wm_erasebkgnd message to the window. The window's processing process responds to the message and re-draws the background of the window, this process is called "Self-painting ". The principle of changing the background color of the dialog box is very simple, that is, to respond to this message, fill the background of the dialog box with a custom color, instead of the default background filling action in the dialog box window. Many new users often ask: "Why can't the wm_erasebkgnd message in the dialog box be found in Class Wizard? Is this message unavailable in the dialog box "? In fact, the dialog box is also a window, and it also contains the wm_erasebkgnd message, but it is filtered out by the dialog filter used by the MFC class wizard (only in the display of the message window, does not really do not respond to this message), in order to highlight the exclusive message and control events in the dialog box during code writing .. As shown in figure 2, if you change the message filter to windows under the "class Info" Table label in the Class Wizard, You can see wm_erasebkgnd in the message list of the dialog box.

Figure 2 modify a message Filter

Now you can use Class Wizard to add the message response function of wm_erasebkgnd and modify this function as follows:

 

Bool ccustdlgdlg: onerasebkgnd (CDC * PDC)
{
Crect rcclient;
Getclientrect (& rcclient );
PDC-> fillrect (& rcclient, & m_brbkgnd );

Return true;

// Return cdialog: onerasebkgnd (PDC );
}

 

M_brbkgnd is a cbrush, which has been initialized before. The key code is to return true at last, instead of calling the base class function by default. Returning true is intended to tell windows: "I have already painted the background. You should not draw any more ". Now let's take a look at the running effect:

Figure 3 Effect of repainting the background

It is not difficult to use bitmap as the background of the dialog box, that is, to draw a bitmap background in the entire customer area,

Step 2: change the color of the widget

The color and background color of the control text are not changed because we have not processed the wm_ctlcolor message. Wm_ctlcolor is one of the most frequent notification messages sent by Windows controls to its parent window. For example, many controls send wm_ctlcolor messages to the parent window, allowing the parent window to provide a painting brush to draw their own background. The window class of MFC treats the notification message specially. If the parent window does not process the notification message, the window class of MFC sends the wm_ctlcolor message back to the control based on the source of the wm_ctlcolor notification message, let the control handle it by yourself. This is the so-called "Message reflection". Not only is wm_ctlcolor, but MFC does reflection on many notification messages. However, in today's example, "Message reflection" is not used ", we can process the notification message in the control's parent window, that is, the dialog box window. Note that the wm_ctlcolor message is a 16-bit Windows platform message, which is replaced by a series of more specific notification messages on a 32-bit Windows platform:

Wm_ctlcolorbtn button control
Wm_ctlcolordlg dialog box
Wm_ctlcoloredit Edit Control
Wm_ctlcolorlistbox list box Control
Wm_ctlcolorscrollbar scroll bar Control
Wm_ctlcolorstatic text Control

For compatibility considerations, MFC still uses onctlcolor to respond to these messages, but uses the nctlcolor parameter to differentiate them. In this function, we can change the drawing of the control by changing the attribute of the PDC parameter, and return the corresponding paint brush handle to the control. The control uses this paint brush to draw its own background. Here is the modified onctlcolor function:

 

Hbrush ccustdlgdlg: onctlcolor (CDC * PDC, cwnd * pwnd, uint nctlcolor)
{
Hbrush HBr = cdialog: onctlcolor (PDC, pwnd, nctlcolor );

PDC-> settextcolor (m_clrtext );
PDC-> setbkmode (transparent );

Return (hbrush) m_brbkgnd; // because the cbrush class implements the hbrush type conversion Operator

// Return HBr;
}

 

Figure. 4 is the effect of this Code. Here we will return our own paint brush to all the controls, which looks good and the text color of the Edit Control is changed, however, it seems that the multi-row edit control is troublesome. It seems that special treatment is needed for the multi-row edit control.

Figure 4 effect after onctlcolor is reloaded

For special processing of the multi-row edit control, the preceding problem is solved as follows:

 

Hbrush ccustdlgdlg: onctlcolor (CDC * PDC, cwnd * pwnd, uint nctlcolor)
{
Hbrush HBr = cdialog: onctlcolor (PDC, pwnd, nctlcolor );

If (pwnd-> getdlgctrlid () = idc_edit_multi_line) // idc_edit_multi_line is the ID of the multi-row edir control.
{
PDC-> settextcolor (m_clrtext );
Return HBr;
}
Else
{
PDC-> settextcolor (m_clrtext );
PDC-> setbkmode (transparent );

Return (hbrush) m_brbkgnd;
}
}

 

The above code solves the idc_edit_multi_line problem, but the ID must be determined for each multi-line edit control. The following method can solve the problem of multi-line editing controls once and for all:

 

Hbrush ccustdlgdlg: onctlcolor (CDC * PDC, cwnd * pwnd, uint nctlcolor)
{
Hbrush HBr = cdialog: onctlcolor (PDC, pwnd, nctlcolor );
Tchar szclassname [64];

: Getclassname (pwnd-> getsafehwnd (), szclassname, 64 );
If (lstrcmpi (szclassname, _ T ("edit") = 0) // is the Edit Control
{
DWORD dwstyle = pwnd-> getstyle ();
If (dwstyle & es_multiline) = es_multiline) // multi-row Edit Control
{
PDC-> settextcolor (m_clrtext );
Return HBr;
}
Else
{
PDC-> settextcolor (m_clrtext );
PDC-> setbkmode (transparent );

Return (hbrush) m_brbkgnd;
}
}
Else // not an editing Control
{
PDC-> settextcolor (m_clrtext );
PDC-> setbkmode (transparent );

Return (hbrush) m_brbkgnd;
}
}

 

Next we will set a special color for each control. To distinguish between controls, we can use the control ID to modify the control background. It is also very easy to directly return the corresponding paint brush, the complete color settings code is as follows:

 

 

 

Hbrush ccustdlgdlg: onctlcolor (CDC * PDC, cwnd * pwnd, uint nctlcolor)
{
Hbrush HBr = cdialog: onctlcolor (PDC, pwnd, nctlcolor );
Tchar szclassname [64];

: Getclassname (pwnd-> getsafehwnd (), szclassname, 64 );
If (lstrcmpi (szclassname, _ T ("edit") = 0) // is the Edit Control
{
DWORD dwstyle = pwnd-> getstyle ();
If (dwstyle & es_multiline) = es_multiline) // multi-row Edit Control
{
PDC-> settextcolor (m_clrtext );
Return HBr;
}
Else
{
PDC-> settextcolor (m_clrtext );
PDC-> setbkmode (transparent );

Return (hbrush) m_brbkgnd;
}
}
Else // not an editing Control
{
If (pwnd-> getdlgctrlid () = idc_stc_redtext)
{
PDC-> settextcolor (RGB (255, 0, 0 ));
PDC-> setbkmode (transparent );
Return (hbrush) m_brbkgnd;
}
Else if (pwnd-> getdlgctrlid () = idc_stc_bluetext)
{
PDC-> settextcolor (RGB (0, 0, 255 ));
PDC-> setbkmode (transparent );
Return (hbrush) m_brbkgnd;
}
Else if (pwnd-> getdlgctrlid () = idc_stc_bluetextwhiteback)
{
PDC-> settextcolor (RGB (0, 0, 255 ));
PDC-> setbkmode (transparent );
Return (hbrush) m_brcontrolbkgnd1;
}
Else if (pwnd-> getdlgctrlid () = idc_chk_green)
{
PDC-> settextcolor (RGB (0,255, 0 ));
PDC-> setbkmode (transparent );
Return (hbrush) m_brbkgnd;
}
Else if (pwnd-> getdlgctrlid () = idc_rad_blue)
{
PDC-> settextcolor (RGB (0, 0, 255 ));
PDC-> setbkmode (transparent );
Return (hbrush) m_brbkgnd;
}
Else if (pwnd-> getdlgctrlid () = idc_chk_green2)
{
PDC-> settextcolor (RGB (0,255, 0 ));
PDC-> setbkmode (transparent );
Return (hbrush) m_brcontrolbkgnd2;
}
Else if (pwnd-> getdlgctrlid () = idc_radio2)
{
PDC-> settextcolor (RGB (0, 0, 255 ));
PDC-> setbkmode (transparent );
Return (hbrush) m_brcontrolbkgnd2;
}
Else
{
PDC-> settextcolor (m_clrtext );
PDC-> setbkmode (transparent );
Return (hbrush) m_brbkgnd;
}
}
}

 

Now let's take a look at the effect:

Figure 5 effect after onctlcolor is modified

The above Code sets the color based on the control ID. You can also set the color of a control based on the control type. The nctlcolor parameter is required, the nctlcolor parameter specifies the type of the control that sends the notification message. The nctlcolor can be of the following values:

Ctlcolor_btn
Ctlcolor_dlg
Ctlcolor_edit
Ctlcolor_listbox
Ctlcolor_msgbox
Ctlcolor_scrollbar
Ctlcolor_static

Step 3: Use bitmap as the background of the dialog box

It is also very easy to use bitmap as the background of the dialog box, that is, to fill the customer area with bitmap in onerasebkgnd, but in onctlcolor, you must note that the returned blank image brush replaces the original image brush, an empty image brush is returned to prevent the control from drawing its own background, which destroys the integrity of the bitmap background. However, sometimes returning an empty image brush will have adverse effects on other controls, therefore, we only process messages of the ctlcolor_btn and ctlcolor_static types:

 

Hbrush cbmpbkgnddlg: onctlcolor (CDC * PDC, cwnd * pwnd, uint nctlcolor)
{
Hbrush HBr = cdialog: onctlcolor (PDC, pwnd, nctlcolor );

If (nctlcolor = ctlcolor_btn | nctlcolor = ctlcolor_static)
{
PDC-> settextcolor (RGB (0, 0, 255 ));
PDC-> setbkmode (transparent );
Return (hbrush) m_hollowbrush;
}

PDC-> settextcolor (RGB (0, 0, 255 ));
PDC-> setbkmode (transparent );
Return HBr;
}

 

The following figure shows the effects of Bitmap backgrounds and empty image Brushes:

Figure 6 effect of using a bitmap background

Step 4: Process button controls separately

Now it seems that the button control still affects the overall effect. wm_ctlcolorbtn seems to have no effect on the push button control, but the push button also supports self-painting. before using the self-painting button, let's take a look at the Principle of Self-painting controls. Windows controls have a default appearance, but many controls support "self-painting", that is, to allow users to customize the appearance of the control. After a custom style is specified for a control, when re-painting, the control sends the wm_measureitem and wm_drawitem messages to the parent window. The parent window responds to the two messages, locates the control size, and draws the control to make the control have a custom appearance. However, the self-painting of each control is completed by the parent window, which increases the burden on the parent window and is not conducive to code reuse. Therefore, MFC performs reflection processing on these messages, that is, sending the message back control, the self-drawn code is encapsulated in the control class, which improves the reusability of the Code. Many control classes of MFC process these two messages by themselves. The Derived classes can overload measureitem and drawitem to draw their own controls. cbutton is such a control class.
Now we will create a self-drawn button class. First we will derive a class from cbutton, We will name it csmbutton, and then reload drawitem and presubclasswindow, the reason for reloading presubclasswindow is to add the bs_ownerdraw style to the button before the csmbutton subclass control. Otherwise, the button will not send the wm_drawitem message to the parent window, and the message reflection of MFC will not occur, our drawitem won't be called. Well, the consequences are very serious. Of course, the csmbutton user can also add bs_ownerdraw style to the button by himself, but it may make people feel that there is no Level, well, the consequences are also very serious. Next, we will add response functions for the four messages wm_capturechanged, wm_mousemove, wm_setcursor, and wm_killfocus. The response to these four messages is to add more functions to the buttons, for example, make the button look like a button on the toolbar, and change the shape of the mouse.
The use of the csmbutton class is just like that of cbutton. Just add a variable for the button. The Demo code contains the source code and usage of the class, which is not described here. The csmbutton class has very simple functions, but it completes a self-drawn button framework. You can modify the code to implement your own style. There are also many such classes on the Internet, providing more powerful functions, for example, stbutton. Now let's take a look at the csmbutton effect:

Figure. 7 effect of using a self-drawn button

Step 5: Use the picture box Control

To display bitmap in a dialog box, you can use complicated controls or libraries such as cximage, or use picture box very easily. The default style of picture box enables frame. You need to manually change it to bitmap, as shown in:

Figure 8 use bitmap

The integration environment of vc6 does not support browsing and editing of 24-bit bitmaps, but it does not affect usage. The bitmaps used in this example are 24-bit. In order to save the processing of the color palette, I am relatively lazy. Use the following code to change the bitmap in the picture box:

 

M_hcat1 = (hbitmap): LoadImage (afxgetresourcehandle (), makeintresource (idb_bitmap1), image_bitmap, 0, 0, lr_createdibsection );
Getdlgitem (idc_stc_picture)-> sendmessage (pai_setimage, image_bitmap, (lparam) m_hcat1 );

 

You can load bitmap as follows:

 

M_hcat1 = (hbitmap): LoadImage (afxgetresourcehandle (), (lpctstr) idb_bitmap1, image_bitmap, 0, 0, lr_createdibsection );

 

This is the final result:

Figure 9 final result of the dialog box

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.