25. How to get information about the current message related to the window being processed
Call CWnd:: getCurrentMessage can get an MSG pointer. For example, you can use
ClassWizard maps several menu items to a function, then call GetCurrentMessage
To determine the selected menu item.
VIOD CMAINFRAME:: OncommmonMenuHandler ()
{
// Display Selected Menu Item in Debug WINDOW.
"" Menu Item% u was selected. / N ",
GetCruuentMessage () -> wparam);
}
26, how to create an irregular shape window
The new SDK function setWindowRGN can be used. This function limits the painting and mouse message in one window.
The specified area actually causes the window into the specified irregular shape.
Create a pair-based application with AppWizard and use the resource editor from the main dialogue resources
In addition to the default control, title, and boundaries.
Add a CRGN data member to the dialogue, and then use the data member to establish a window area.
Class CROUNDDLG: Public CDIALOG
{
...
Private:
CRGN M_RGN: // WINDOW Region
...
}
Modify the OnInitDialog function to create an ellipse area and call SETWINDOWRGN to assign the area to
window:
Bool crossdlg:: oninitdialog ()
{
CDIALOG:: OnInitDialog ();
// Get size of dialog.
CRECT RCDIALOG;
GetClientRect (rcdialog);
// Create Region and assign to window.
M_RGN. CreatellipticRGN (0, 0, RcDialog.width (), rcdialog .Height ());
SetwindowRgn (GetsafehWnd (), (HRGN) M_ RGN, TRUE);
Return True;
}
By establishing an area and calling setWindowRGN, an irregular shape window has been established, and the following example
The subroutine is modified onpaint function to make the window shape look like a sphere.
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 = GtrValue (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 fltstephaz = (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) Fltstepharz * 2, (int) Fltstepvert * Nindex,
Right. Right - ((int) fltstepharz * 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 move the window when the window is hit.
UINT CROUNDDLG:: Onnchittest (CPoint Point)
{
// let User Move Window by Clickign Anywhere on the window.
UINT NHITTEST = CDIALOG:: Onnchittest (Point);
RERN (NHITTEST = = HTCLIENT)? HTCAPTION: NHITTEST;
}
27, how to get a pointer to the toolbar and status strip in your code
When the default, the workflow is created by the Status and the toolbar as the sub-window of the main box window, the status bar
There is an AFX_IDW_STATUS_BAR identifier, the toolbar has an AFX_IDW_TOOLBAR identifier, the following example said
How to call CWnd in:: getDescendantWindow and AFXGETMAINDOW AND AFXGETMAINDOW AND
Pointer of the sub-window:
// 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 prohibit the tooltle tips
If the CBRS_Tooltips style bit is set, the Toolbar will display the tooltip, to enable or disable the tooltip, you need to set or clear the style bit. The following example is called CControlbar:: getBarstyle
And ccontrolbar:: setbarstyle build a member function that completes this feature:
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 set the title in calling CWnd:: setWindowText, as follows:
INT CMAINFRAME:: oncreate (lpcreateStruct lpcreatestruct)
{
...
// set the capen of the toolbar.
m_wndtoolbar.SetWindowText (_t "standdard");
30. How to create and use the Modeless dialog
The MFC will encapsulate the mode and the non-mode dialog in the same class, but use the non-mode dialogue requires a few
A dialog requires a few steps. First, use the resource editor to establish a dialogue and use
ClassWizard creates a CDIALOG derived class. The stop of the mode and the non-mode dialog is different:
Mode dialogue is aborted by calling cdialog:: enddialog, and there is no mode dialogue is called
CWnd:: DestroyWindow Sad, Functions CDIALOG:: Onok and CDIALOG:: Oncancel
Call EndDialog, so you need to call DESTROYWINDOW and reset the function of the modified dialog.
Void csampledialog:: onok ()
{
// Retrieve and Validate Dialog Data.
IF (! Updatedata (TRUE))
{
// The Updatedata Rountine Will Set Focus to Correct Item
"" Updatedata Failed Database ("Updatedata Failed Database (" UpdatedTa Failed Database;);
Return;
}
// Call DestroyWindow INSTEAD OF EndDialog.
DESTROYWINDOW ();
}
Void csampledialog:: oncancel ()
{
// Call DestroyWindow INSTEAD OF EndDialog.
DESTROYWINDOW ();
}
Second, it is necessary to properly remove the C object indicating the conversation. For mode pair, this is easy, you need to create a function to return to the C object; no mode dialog is not synchronized, then return to the function call, so the user does not know when to delete the C object. Workframe call CWnd: postncdestroy when the window is revoked: postncdestroy can reset the function and perform a clear operation, such as deleting the THIS pointer.
Void csampledialog:: postncdestroy ()
{
// Declete the C Object this represents this dialog .delete this;
}
Finally, create a modeless dialogue. You can call CDIALOG:: Domodal Create a mode alignment, to create a non-mode dialog, call CDIALOG:: CREATE. The following example illustrates how the application creates a modeless dialogue:
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
This is due to Win 32 Advanced Static Controls and Microsoft Resource Editor, which is easy to display bitmaps in the dialog, just drag the graphic control to the dialogue and select the appropriate properties, users can also display icons. , Bitmaps, and enhanced meta files.
32, how to change the background color of the conversation or form window
Call CWINAPP:: SetDialogBkcolor can change the background color of all applications. The first parameter specifies the background color, and the second parameter specifies the text color. The following example sets the application dialog to a 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 redraw dialog (or dialogue sub-control), Windows sends a message WM_CTLCOLOR to the dialog, usually the user can let Windows select the brush of the painting background, or reset the message to specify the brush. The following example shows the steps to create a red background dialog.
First, add a member variable of a member variable for the dialogue class:
Class CMYFORMVIEW: PUBLIC CFORMVIEW
{
...
Private:
CBRUSH M_ Brush; // Background Brush
...
}
Second, the brush is initialized to the desired background color in the constructor of the class.
CMYFORMVIEW:: CMYFORMVIEW ()
{
// Initialize Background Brush.
M_brush .createsolidbrush (RGB (0, 0, 255)))
}
Finally, use the ClassWizard to process the WM_CTLCOLOR message and return a brush handle for painting the background of dialogue. Note: This function is to be called because it is also called the dialog control, so it is necessary to detect NCTLCOLOR parameters.
Hbrush CMYFORMVIEW:: oncTlcolor (CDC * PDC, CWND * PWND, UINT NCTLCOLOR)
{
// DETERMINE IF Drawing a Dialog Box. If We are, Return Handle To
// OUR OWN Background Brush. OtherWise Let Windows Handle IT.
IF (nctlcolor == ctlcolor _ dlg)
Return (Hbrush) m_brush .getsafehandle ();
Return CFormview:: OnctLcolor (PDC, PWND, NCTLCOLOR);}