Visual c ++ programming skills 4
25. How to obtain information about the current message being processed by the window
26. How to create an irregular window
27. How to get the pointer of toolbar and status bar in code
28. How to enable and disable tooltip
29. How to set the toolbar title
30. How to create and use the modeless dialog box
31. How to display a bitmap in the dialog box
32. How to change the background color of a dialog or form window
25. How to obtain information about the current message being processed by the window
Call cwnd: getcurrentmessage to obtain a MSG pointer. For example, you can use
Classwizard maps several menu item handlers to a function, and then calls getcurrentmessage
To determine the selected menu items.
Viod cmainframe: oncommmonmenuhandler ()
{
// Display selected menu item in debug window.
Trace ("menu item % u was selected./N ",
Getcruuentmessage ()-> wparam );
}
26. How to create an irregular window
You can use the new SDK function setjavaswrgn. This function limits painting and mouse messages to one in the window.
In fact, the window is made into a specified irregular shape.
Use Appwizard to create a pair-based application and use the resource editor to delete
In addition to the default control, title, and border.
Add a crgn data member to the dialog class and use the data member to create a window area later.
Class crounddlg: Public cdialog
{
...
PRIVATE:
Crgn m_rgn: // WINDOW REGION
...
};
Modify the oninitdialog function to create an elliptical region and call setdomainwrgn to allocate the region
Window:
Bool crounddlg: oninitdialog ()
{
Cdialog: oninitdialog ();
// Get size of dialog.
Crect rcdialog;
Getclientrect (rcdialog );
// Create region and assign to window.
M_rgn. createellipticrgn (0, 0, rcdialog. Width (), rcdialog. Height ());
Setjavaswrgn (getsafehwnd (), (hrgn) M _ RGN, true );
Return true;
}
By creating a region and calling setjavaswrgn, you have created an irregular window, as shown in the following example:
The subroutine is used to modify the onpaint function so that the window shape looks like a spherical body.
Voik crounddlg: onpaint ()
{
Cpaintdc de (this); // device context for painting.
// Draw Ellipse with out any border
DC. selecstockobject (null_pen );
// Get the RGB color components of the sphere color
Colorref color = RGB (0, 0,255 );
Byte byred = getrvalue (color );
Byte bygreen = getgvalue (color );
Byte byblue = getbvalue (color );
// Get the size of the View window
Crect rect;
Getclientrect (rect );
// Get minimun number of units
Int nunits = min (rect. Right, rect. Bottom );
// Calculate he horiaontal and vertical step size
Float fltstephen orz = (float) rect. Right/nunits;
Float fltstepvert = (float) rect. Bottom/nunits;
Int nellipse = nunits/3; // calculate how many to draw
Int nindex; // current ellipse that is being draw
Cbrush brush; // bursh used for ellipse fill color
Cbrush * pbrushold; // previous brush that was selected into DC
// Draw Ellipse, gradually moving towards upper-right corner
For (nindex = 0; nindes <+ nellipse; nindes ++)
{
// Creat solid brush
Brush. creatsolidbrush (RGB (nindex * byred)/nellipse ).
(Nindex * bygreen)/nellipse), (nindex * byblue)/nellipse )));
// Select brush into DC
Pbrushold = Dc. SelectObject (& brhsh );
// Draw Ellipse
DC. ellipse (INT) fltstephen orz * 2, (INT) fltstepvert * nindex,
Rect. Right-(INT) fltstephen orz * nindex) + 1,
Rect. Bottom-(INT) fltstepvert * (nindex * 2) + 1 );
// Delete the brush
Brush. delecteobject ();
}
}
Finally, the wm_nchittest message is processed so that the window can be moved whenever the window is hit.
Uint crounddlg: onnchittest (cpoint point)
{
// Let user move window by clickign anywhere on the window.
Uint nhittest = cdialog: onnchittest (point );
Rerurn (nhittest = htclient )? Htcaption: nhittest;
}
27. How to get the pointer of toolbar and status bar in code
The status bar and toolbar are used as subwindows and status bars of the main window when the work box is created.
There is an afx_idw_status_bar identifier, and the toolbar has a afx_idw_toolbar identifier.
Demonstrate how to call cwnd: getdescendantwindow and afxgetmainwnd together to obtain these
Sub-window pointer:
// Get pointer to status bar.
Cstatusbar * pstatusbar =
(Cstatusbar *) afxgetmainwnd ()-> getdescendantwindow
(Afx_idw_stutus_bar );
// Get pointer to toolbar.
Ctoolbar * ptoolbar =
(Ctoolbar *) afxgetmainwnd ()-> getdescendantwindow (afx_idw_toolbar );
28. How to enable and disable tooltip
If the cbrs_tooltips style bit is set, the toolbar displays a tooltip to enable or disable
Tooltip: You need to set or clear the style bit. In the following example, call ccontrolbar: getbarstyle
Create a member function with ccontrolbar: setbarstyle to complete this function:
Void cmainframe: enabletooltips (bool bdisplaytips)
{
Assert_valid (m_wndtoolbar );
DWORD dwstyle = M _ wndtoolbar. getbarstyle ();
If (bdisplaytips)
Dwstyle | = cbrs_tooltips;
Else
Dwstyle & = ~ Cbrs_tooltips;
M_wndtoolbar.setbarstyle (dwstyle );
}
29. How to set the toolbar title
The toolbar is a window, so you can call cwnd: setwindowtext to set the title. For example:
Int cmainframe: oncreate (maid)
{
...
// Set the caption of the toolbar.
M_wndtoolbar.setwindowtext (_ t "standdard ");
30. How to create and use the modeless dialog box
MFC encapsulates the mode and mode-free dialog in the same class, but it requires a few
A dialog takes several steps. First, use the resource editor to create a dialog resource and use
Classwizard creates a cdialog derived class. The pause of a Mode dialog is different from that of a Non-mode dialog:
A mode dialog is aborted by calling cdialog: enddialog. A mode dialog is called.
Cwnd: destroywindow to abort, function cdialog: onok and cdialog: oncancel
Call enddialog, so you need to call destroywindow and reset the function of the mode-free dialog.
Void csampledialog: onok ()
{
// Retrieve and validate dialog data.
If (! Updatedata (true ))
{
// The updatedata rountine will set focus to correct item
Traceo ("updatedata failed during dialog termination./N ");
Return;
}
// Call destroywindow instead of enddialog.
Destroywindow ();
}
Void csampledialog: oncancel ()
{
// Call destroywindow instead of enddialog.
Destroywindow ();
}
Second, you must delete the C ++ object indicating the dialog correctly. For mode, this is very easy. You need to create a function to return and then delete the C ++ object. If the mode-free dialog is not synchronous, it is returned immediately after the function is called, therefore, the user does not know when to delete the C ++ object. When you cancel a window, the workbox calls cwnd: postncdestroy to reset the function and perform cleanup operations, such as deleting the this pointer.
Void csampledialog: postncdestroy ()
{
// Declete the C ++ object that represents this dialog.
Delete this;
}
Finally, create a mode-free dialog. You can call cdialog: domodal to create a mode pair. To create a mode-free dialog, you must call cdialog: create. The following example illustrates how an application creates a mode-free dialog:
Void cmainframe: onsampledialog ()
{
// Allocate a modeless dialog object.
Csampledilog * pdialog = new csampledialog;
Assert_valid (pdialog );
// Create the modeless dialog.
Bool bresult = pdialog-> creste (idd_idalog );
Assert (bresult );
}
31. How to display a bitmap in the dialog box
This is due to Win 32's Advanced static controls and Microsoft's resource editor. It is easy to display bitmaps in the dialog box. You only need to drag the graphical controls into the dialog box and select the appropriate properties, you can also display icons, bitmaps, and Enhanced Meta files.
32. How to change the background color of a dialog or form window
Call cwinapp: setdialogbkcolor to change the background color of all applications. The first parameter specifies the background color, and the second parameter specifies the text color. In the following example, set the application dialog to blue background and yellow text.
Bool csampleapp: initinstance ()
{
...
// Use blue dialog with yellow text.
Setdialogbkcolor (RGB (0, 0,255), RGB (255,255, 0 ));
...
}
When you need to re-draw a dialog (or a sub-control of the dialog), Windows sends the message wm_ctlcolor to the dialog. Generally, you can ask windows to select a paint brush for the background or reset the specified brush for the message. The following example describes how to create a red background dialog.
First, add the one-person member variable cbursh to the dialog base class:
Class cmyformview: Public cformview
{
...
PRIVATE:
Cbrush M _ brush; // background brush
...
};
Second, initialize the brush to the desired background color in the class constructor.
Cmyformview: cmyformview ()
{
// Initialize background brush.
M_brush. createsolidbrush (RGB (0, 0,255 ))
}
Finally, use classwizard to process the wm_ctlcolor message and return a brush handle used to paint the conversation background. Note: The nctlcolor parameter must be checked because this function is also called when you redraw the dialog control.
Hbrush cmyformview: onctlcolor (CDC * PDC, cwnd * pwnd, uint nctlcolor)
{
// Determine if drawing a dialog box. If we are, return + handle
// Our own background brush. Otherwise let windows handle it.
If (nctlcolor = ctlcolor _ DLG)
Return (hbrush) m_brush. getsafehandle ();
Return cformview: onctlcolor (PDC, pwnd, nctlcolor );
}