During the design and implementation of dialog box re-painting, the main areas of the dialog box to be drawn include the title, border, and customer area. Shows the specific regions.
Since bitmap display is required for multiple regions, we first encapsulate a BMP bitmap display output function as follows:
Void cctestdlg: showbmp (int x, int y, int W, int H, int NID)
{// NID indicates the ID of the bitmap Resource
Crect winrc;
CDC * PDC = getwindowdc ();
CDC memdc;
Memdc. createcompatibledc (PDC );
Bitmapinfo BMP Info;
Cbitmap BMP;
Getwindowrect (& winrc );
BMP. loadbitmap (NID );
BMP. GetObject (sizeof (bitmapinfo), & BMP info );
Int nbmpcx = BMP info. bmiheader. biwidth;
Int nbmpcy = BMP info. bmiheader. biheight;
Memdc. SelectObject (BMP );
PDC-> stretchblt (X, Y, W, H,
& Memdc, nbmpcx, nbmpcy, srccopy); // draw a bitmap in the window
BMP. deleteobject ();
Releasedc (PDC );
}
(1) redraw bitmap output for each region. Because the title bar and border are mainly drawn from non-customer areas, it should be drawn in the wm_ncpaint message. Of course, you must first import the used BMP bitmap resources to the project by adding resources.
First, define some constant values to indicate the components of the dialog box.CodeAs follows:
# Define lefttitle 1 // left title
# Define midtitle 1 // intermediate title
# Define righttitle 1 // right title
# Define minbutton 1 // right title
# Define maxbutton 1 // right title
# Define closebutton 1 // right title
# Define appicon 1 //ProgramIcon
# Define leftbar 1 // left border
# Define rightbar 1 // right border
# Define bottombar 1 // Bottom Border
In the method onncpaint () of the wm_ncpaint message pair, call the method setface () in the dialog box (). This method is used to draw bitmaps in various areas of the dialog box. The main code is as follows:
Void cctestdlg: setfact ()
{
// Todo: add the control notification handler code here
Int nframecy = getsystemmetrics (sm_cyfixedframe); // obtain the height of the border in the dialog box.
Int nframecx = getsystemmetrics (sm_cxdlgframe); // gets the width of the dialog border.
Int m_nbordercy;
Int m_nbordercx;
Int m_ntitlebarcy;
Int m_ntitlebarcx;
If (getstyle () & ws_border) // obtain whether the dialog box has a border
{
M_nbordercy = getsystemmetrics (sm_cyborder) + nframecy;
M_nbordercx = getsystemmetrics (sm_cxborder) + nframecx;
}
Else
{
M_nbordercx = nframecx;
M_nbordercy = nframecy;
}
M_ntitlebarcy = getsystemmetrics (sm_cycaption) + m_nbordercy; // calculate the Title Bar Height
M_ntitlebarcx = m_nbordercx;
Crect winrect, factrect;
Getwindowrect (& winrect); // obtain the window area
Factrect. copyrect (crect (0, 0, winrect. Width (), winrect. Height ()));
Cwindowdc windowsdc (this); // obtain the context of the window Device
// Obtain the height and width of the entire MFC window
Int wincx = winrect. Width ();
Int Wincy = winrect. Height ();
If (lefttitle)
{// Bitmap in the left title bar of the draw dialog box
Showbmp (0,0, 100, m_ntitlebarcy, idb_righttitle );
}
If (righttitle)
{// Draw the right title bar bitmap in the dialog box
Showbmp (winCX-100, 0,100, m_ntitlebarcy, idb_righttitle );
}
If (midtitle)
{// The title bar bitmap in the draw dialog box
Showbmp (, 0, winCX-200, m_ntitlebarcy, idb_midtitle );
}
If (leftbar)
{// The Left Border bitmap of the draw dialog box showbmp (0, m_ntitlebarcy, m_nbordercx, factrect. Height ()-m_nbordercy, idb_leftbar );
}
If (bottombar)
{// Draw the bottom border bitmap showbmp (m_nbordercx, winCY-m_nBorderCX, winCX-2 * m_nbordercx, m_nbordercx, idb_bottombar );
}
If (rightbar)
{// Drawing dialog box left border bitmap showbmp (winCX-m_nBorderCX, m_ntitlebarcy, m_nbordercx, factrect. Height ()-m_nbordercy, idb_rightbar );
}
If (minbutton)
{// Draw the minimization button for the dialog box
Showbmp (winCX-3-24-3-24-3-24, 24, idb_minbutton );
}
If (maxbutton)
{// Draw the maximize button for the dialog box
Showbmp (winCX-3-24-3-24, 24, idb_maxbutton );
}
If (closebutton)
{// Draw the close button for the dialog box
Showbmp (winCX-3-24, 24, idb_closebutton );
}
Releasedc (& windowsdc );
Drawtitlebartext (); // output the title bar text
}
The main code of drawtitlebartext () is as follows:
Cstring strtitle = "title bar and border of the Self-painted window ";
CDC * PDC = getwindowdc (); // obtain the context of the window Device
PDC-> setbkmode (transparent); // you can specify a transparent background.
PDC-> settextcolor (RGB (255,255,255); // you can specify the text color.
PDC-> settextalign (ta_center); // sets the text alignment mode.
Crect rect;
Getclientrect (& rect); // obtain the customer region of the window.
Csize sztext = PDC-> gettextextent (strtitle); // obtain the text height
PDC-> textout (rect. Width ()/2, 3, strtitle, 20); // output text in the window
Releasedc (PDC); // release the device context in the window
As shown in.
After completing the bitmap in the corresponding area of the dialog box, the task has not been completed. You also need to handle the hotspot effect of the title bar button and the button click event. First, you must process the event when the mouse moves in a non-customer area, that is, the wm_ncmousemove message. In the message processing function, you must determine whether the current mouse is located in the button area of the title bar, if yes, set the hotspot effect of the button and record the current button status and the button on which the mouse is located. Similarly, if the event is not clicked in the customer region of the processing dialog box, that is, the wm_nclbuttondown message, click the event in its message processing function. The main code is as follows:
Void cctestdlg: onncmousemove (uint nhittest, cpoint point)
{
// Todo: add the message processing program code and/or call the default value here
Crect minrc, maxrc, closerc, winrc;
Getwindowrect (& winrc );
Closerc. copyrect (crect (winrc. Width ()-27,1, winrc. Width ()-27 + 24,1 + 24 ));
Maxrc. copyrect (crect (winrc. Width ()-27*2, 1, winrc. Width ()-27*2 + 24, 1 + 24 ));
Minrc. copyrect (crect (winrc. Width ()-27*3, 1, winrc. Width ()-27*3 + 24, 1 + 24 ));
Point. offset (-winrc. Left,-winrc. Top); // convert the point to the window coordinate because it is the screen coordinate.
If (closerc. ptinrect (point) // determines whether the mouse is in the close button Area
{
Showbmp (winrc. Width ()-3-24, 1, 24, 24, idb_closebutton2 );
}
Else if (maxrc. ptinrect (point) // determines whether the mouse is in the maximize button Area
{
Showbmp (winrc. Width ()-27*2, 1, 24, 24, idb_maxbutton2 );
}
Else if (minrc. ptinrect (point) // determines whether the mouse is in the Minimize button Area
{
Showbmp (winrc. Width ()-27 *, 1, 24, 24, idb_minbutton2 );
}
Else // The mouse is not in the button area of the title bar
{
Showbmp (winrc. Width ()-3-24-54,1, 24, 24, idb_minbutton );
Showbmp (winrc. Width ()-3-24-27,1, 24,24, idb_maxbutton );
Showbmp (winrc. Width ()-3-24, 1, 24, 24, idb_closebutton );
}
}
Shows the effect after adding a hotspot effect.