71. Hide the title bar and menu bar
Hide the title bar modifystyle (ws_caption, 0)
Hide the menu bar setmenu (null)
72. inflaterect
The inflaterect function is used to increase or decrease the size of a rectangle.
For example, m_graphrect.inflaterect (-70,-30,-30,-50 );
Add 70 to the left coordinate of the rectangle, 30 to the top, 30 to the right, and 50 to the bottom.
73. How to Make the modeless dialog box appear behind the main window
The key to solving this problem is that cdialog's create cannot create a non-owner window. You must create a window in another way.
For example, if your dialog box class is cdlgnoowner, add a member variable of the cdlgnoowner class to the cmainframe,
The message processing function in the pop-up dialog box is
Void cmainframe: onnoowner ()
{
Cdlgnoowner * m_dlgtest = new cdlgnoowner (this );
Hwnd hwnddlg =: createdialog (AfxGetInstanceHandle (), makeintresource (cdlgnoowner: IDD), null/* owner */, null/* dlgproc */);
// Note that dlgproc is null, and it doesn't matter, because subclass is required next.
M_dlgtest-> subclasswindow (hwnddlg); // The member variable is mounted!
M_dlgtest-> showwindow (sw_show );
// A "free" dialog box is displayed, which is the same as the main window.
}
Of course, do not forget destroywindow () when the dialog box is closed. It is all standard processing in the dialog box class.
74. Hide the window (when the child window has no focus)
When the program starts, use setwindowpos in initdialog to set the form out of the screen.
Then hide
1. Set the timer in the oninitdialog () function: (wm_initdialog in the Windows API)
Settimer (1, 1, null );
2. Add the message processing function ontimer for processing wm_timer and add the Code:
If (nidevent = 1)
{
Deletetimer (1 );
Showwindow (sw_hide );
}
75. Modify the view background
How do I change the background color of a view?
To change the background color for a cview, cframewnd, or cwnd object,
Process the wm_erasebkgnd message. The following code shows how:
Bool csampleview: onerasebkgnd (CDC * PDC)
{
// Set brush to desired background color.
Cbrush backbrush (RGB (255,128,128 ));
// Save old brush.
Cbrush * poldbrush = PDC-> SelectObject (& backbrush );
Crect rect;
PDC-> getclipbox (& rect); // erase the area needed.
PDC-> patblt (rect. Left, rect. Top, rect. Width (),
Rect. Height (), patcopy );
PDC-> SelectObject (poldbrush );
Return true;
}
I solved the problem like this:
Hbrush dlgtest: onctlcolor (CDC * PDC, cwnd * pwnd, uint nctlcolor)
{
Switch (nctlcolor)
{
Case ctlcolor_btn:
Case ctlcolor_static:
{
PDC-> setbkmode (transparent );
}
Case ctlcolor_dlg:
{
Cbrush * back_brush;
Colorref color;
Color = (colorref) getsyscolor (color_btnface );
Back_brush = new cbrush (color );
Return (hbrush) (back_brush-> m_hobject );
}
}
Return (cformview: onctlcolor (PDC, pwnd, nctlcolor ));
}
76. How to click a place outside the dialog box to bring the dialog box to the end of the main window
Only the desktop can be used as the parent window
Pmdlg = new cmdlg;
Pmdlg-> Create (idd_m_dialog, cwnd: getdesktopwindow ()/* set parent window */);
Pmdlg-> showwindow (sw_show );
Then, hide the dialog box program in the taskbar.
How to make the dialog box application appear on the taskbar without hiding the window.
[Solution]
Modify the extended properties of the dialog box to ws_ex_toolwindow.
[Program Implementation]
Set the properties of the dialog box to toolwindow, and then execute this code as needed.
DWORD style =: getwindowlong (afxgetmainwnd ()-> m_hwnd, gwl_exstyle );
Style = ws_ex_toolwindow;
Afxgetmainwnd ()-> showwindow (false );
: Setwindowlong (afxgetmainwnd ()-> m_hwnd, gwl_exstyle, style );
Afxgetmainwnd ()-> showwindow (true );
77. to automatically close the window when the program is started, it is not displayed in the taskbar.
Use Ctrl + W to open classwizard;
Click the class info page. The class name is the project name DLG,
Select "Windows" in "filter" in the lower-left corner ";
Return to the Message maps page and you will see wm_windowposchanging in the message,
Add the code, as shown above.
If you run *. EXE in this way, you will not see the main interface or the taskbar, that is, it is not listed in the "application" in the task manager. How can you disable it?
You can find it in the process in the task manager. This is a common method used by hackers.
If necessary, you cannot even see the process. It is a problem to terminate it.
78. Modify the toolbar for print preview
Create a dialogbar for the ID afx_idd_preview_toolbar. Then the system replaces the default dialogbar.
79. How to Implement the conversion between SDI and MDI?
I want to convert a compiled SDI application to MDI, and there are obviously many changes.
You can do this: Create a class that inherits cmdichidwnd, without setting it as cchldfrm. Make the following changes in cwinapp.
Initinstance ()
{
....
// Instead of adding csingledoctemplate
// Add cmultidoctemplate.
Pdoctemplate = new cmultidoctemplate (
Idr_mainframe,
Runtime_class (csdidoc ),
Runtime_class (cchldfrm ),
// For main MDI frame change this frame window from
// Cframewnd derivative (I. e. cmainframe)
// To your ichildwnd derived cchldfrm.
Runtime_class (csdiview ));
/// After this it is required to create the main frame window
// Which will contain all the child windows. Now this window is
// What was initially frame window for SDI.
Cmainframe * pmainframe = new cmainframe;
If (! Pmainframe-> loadframe (idr_mainframe ))
Return false;
M_pmainwnd = pmainframe;
.....
}
After cmainframe of the class inherited from cmdiframewnd replaces cframwnd, all classes will be inherited from cmdiframe instead of cframewnd. After compilation and running, you will find that the program has been transformed from SDI to MDI.
Note: In cmainfram, you must change the constructor from private to public. Otherwise, an error occurs.
80. What is the vertical text in CDC?
In the ondraw member function, I want to vertically align the text, but the CDC class does not seem to support this processing.
Method 1: If your vertical alignment refers to rotating text, the following code will help you: this code checks a check box control to check whether text needs to be rotated.
// M_pcfytitle is a cfont * to the selected font.
// M_btotateytitle is a bool (= true if rotated)
Void cpage1: onrotateytitle ()
{
Logfont LGF;
M_pcfytitle-> getlogfont (& LGF );
M_brotateytitle =
(Cbutton *) getdlgitem (idc_rotateytitle)-> getcheck ()> 0;
// Escapement is reckoned clockwise in 1/10ths of a degree:
LGF. lfescapement =-(m_brotateytitle * 900 );
M_pcfytitle-> deleteobject ();
M_pcfytitle-> createfontindirect (& LGF );
Drawsamplechart ();
}
Note that if you select different fonts from cfontdialog, you should set the lfescapement member of logfont by yourself. Upload the initialized lfescapement value to the cfontdialog.
Method 2: See the following code:
Logfont locallogfont;
Strcpy (locallogfont. lffacename, typeface );
Locallogfont. lfweight = fweight;
Locallogfont. lfescapement = Orient;
Locallogfont. lforientation = Orient;
If (myfont. createfontindirect (& locallogfont ))
{
Cmyoldfont = CDC-> SelectObject (& myfont );
}
81. How do I use a keyboard to scroll the separated viewport?
My problem is that when I scroll the mouse to split the window, the view scrolling is normal, but nothing happens when I use the keyboard.
Add the following two functions to your view inheritance class, assuming the class is cscrollerview:
Void cscrollerview: onkeydown (uint nchar, uint nrepcnt, uint nflags)
{
Bool processed;
For (unsigned int I = 0; I <nrepcnt & processed; I ++)
Processed = keyscroll (nchar );
If (! Processed)
Cscrollview: onkeydown (nchar, nrepcnt, nflags );
}
Bool cscrollerview: keyscroll (uint nchar)
{
Switch (nchar)
{
Case vk_up:
Onvscroll (sb_lineup, 0, null );
Break;
Case vk_down:
Onvscroll (sb_linedown, 0, null );
Break;
Case vk_left:
Onhscroll (sb_lineleft, 0, null );
Break;
Case vk_right:
Onhscroll (sb_lineright, 0, null );
Break;
Case vk_home:
Onhscroll (sb_left, 0, null );
Break;
Case vk_end:
Onhscroll (sb_right, 0, null );
Break;
Case vk_prior:
Onvscroll (sb_pageup, 0, null );
Break;
Case vk_next:
Onvscroll (sb_pagedown, 0, null );
Break;
Default:
Return false; // not for us
// And let the default class
// Process it.
}
Return true;
}
82. How can I change the default cursor shape?
I tried to change the cursor to another shape and color, but it didn't change.
Add wm_setcursor message processing in the dialog box/window/where you need it.
Bool mydialog: onsetcursor (cwnd * pwnd, uint nhittest, uint message)
{
// Todd add your message handler code here and/or call default
: Setcursor (afxgetapp ()-> loadcursor (idc_mycursor ));
Return true;
// Return cdialog: onsetcursor (pwnd, nhittest, message );
}
The reason for your failure is that the cursor style of the window cannot be null.
83. How do I select the node text in ctreectrl for editing?
After adding an item to ctreectrl, how can I edit the text of this node?
First, set your ccomptreectrl to have the tvs_editlabels attribute. Use the control attribute during design to set it with the getstyle ()/setstyle () member function at runtime. Then, see the following code:
Htreeitem ccomptreectrl: addset ()
{
Static int setcnt = 3D 1;
Htreeitem hitem;
Cstring csset;
// Create text for new Note: New Set 1, new set 2...
Csset. Format (_ T ("New set % d"), setcnt ++ );
Hitem = 3D insertitem (csset, img_closedfolder, img_closedfolder );
If (hitem! = 3D null)
Editlabel (hitem );
Return hitem;
}
84. In clistctrl, how do I receive notifications when I select a change?
I used a clistctrl (self-drawn type) in the report view. I want to know when the selected item has changed.
When the selected item changes, you can use the button to be valid or invalid. follow these steps:
Add lvn_itemchanged to message processing.
Void cyourclassnamehere: onitemchangedeventlist (nmhdr * pnmhdr, lresult * presult)
{
Nm_listview * pnmlistview = (nm_listview *) pnmhdr;
* Presult = 0;
If (pnmlistview-> uchanged = lvif_state)
{
If (pnmlistview-> unewstate)
Getdlgitem (idc_delete)-> enablewindow (true );
Else
Getdlgitem (idc_delete)-> enablewindow (false );
}
}
85. Select the entire column in the list control?
I encountered a problem when processing the list control. I want to create a listview to display the list information in both listview and reportview based on the selection of the tree control. The following is the relevant code:
// Set full line select
Listview_setextendedlistviewstyle (m_plstcustomers-> getsafehwnd (),
Lvs_ex_fullrowselect );
Handle the problem as follows:
// -------------------- Begin of snippet --------------------------------
Bool ccommctrlutil32: listctrl_modifyextendedstyle (clistctrl & p_rlistctrl,
Const DWORD p_dwstyleex,
Const bool p_badd)
{
Hwnd t_hwnd = p_rlistctrl.getsafehwnd ();
DWORD t_dwstyleex = listview_getextendedlistviewstyle (t_hwnd );
If (p_badd)
{
If (0 = (p_dwstyleex & t_dwstyleex ))
{
// Add Style
T_dwstyleex | = p_dwstyleex;
}
}
Else
{
If (0! = (P_dwstyleex & t_dwstyleex ))
{
// Remove Style
T_dwstyleex & = ~ P_dwstyleex;
}
}
Listview_setextendedlistviewstyle (t_hwnd, t_dwstyleex );
Return true;
}
86. How do I limit the maximum size of the MDI sub-framework?
Use ptmaxtracksize instead of prmaxsize, as shown below:
Void cchildframe: ongetminmaxinfo (minmaxinfo far * lpmmi)
{
// Todd add your message handler code here and/or call default
Cchildframe: ongetminmaxinfo (lpmmi );
Lpmmi-> ptmaxtracksize. x = 300;
Lpmmi-> ptmaxtracksize. Y = 400;
}
87. How to Achieve 3D effects?
In the dialog box, how does one implement the 3D effect of the edit and listboxes controls? (Environment 95/NT vc5.0)
1). Replace: createwindow with ws_ex_clientedge or cwnd: Create with cwnd: createex.
2) After the control is created, call modifystyleex (0, ws_ex_clientedge ).
88. How do I update the text of a pane in a status bar?
By default, a cstatusbar pane is not enabled when the pane is created.
To activate a pane, you must call the on_update_command_ui () macro
Each pane on the status bar and update the panes. Because panes do not
Send wm_command messages, you cannot use classwizard to activate panes;
You must type the code manually. For example, suppose one pane has
Id_indicator_page as its identifier and that it contains the current
Page number in a document. To make the id_indicator_page pane display
Text, add the following to a header file (probably the mainfrm. h file ):
Afx_msg void onupdatepage (ccmdui * pcmdui );
Add the following to the application message map:
On_update_command_ui (id_indicator_page, onupdatepage)
Add the following to a source code file (probably mainfrm. cpp ):
Void cmainframe: onupdatepage (ccmdui * pcmdui)
{
Pcmdui-> enable ();
}
To display text in the panes, either call setpanetext () or call
Ccmdui: settext () in the onupdate () function. For example, you might
Want to set up an integer variable m_npage that contains the current
Page number. Then, the onupdatepage () function might read as follows:
Void cmainframe: onupdatepage (ccmdui * pcmdui)
{
Pcmdui-> enable ();
Char szpage [16];
Wsprintf (lpstr) szpage, "Page % d", m_npage );
Pcmdui-> settext (lpstr) szpage );
}
This technique causes the page number to appear in the pane during idle
Processing in the same manner that the application updates other
Indicators.
89. dynamically modify the dialog box size
[Question]
I made a demo about how to dynamically change the dialog box size. Let's take a look.
[Program Implementation]
// Usage of this function:
// The first parameter: True indicates that the expanded dialog box is displayed. False indicates that the expanded dialog box is displayed.
// The second parameter indicates the hwnd in this dialog box,
// The third parameter indicates the ID of the scaled-down control.
Void coptiondlg: expandbox (bool fexpand, hwnd, int niddefadefabox)
{
Cwnd * pwndbox = getdlgitem (niddefabox box );
Rect rcdefaultbox, rcchild, rcintersection, rcwnd;
Pwndbox-> getwindowrect (& rcdefaultbox );
Hwnd hwndchild =: gettopwindow (hwnd );
For (; hwndchild! = NULL; hwndchild =: getnextwindow (hwndchild, gw_hwndnext ))
{
: Getwindowrect (hwndchild, & rcchild );
If (! Intersectrect (& rcintersection, & rcchild, & rcdefabox box ))
: Enablewindow (hwndchild, fexpand );
}
: Getwindowrect (hwnd, & rcwnd );
If (getwindowlong (hwnd, gwl_userdata) = 0)
{
Setwindowlong (hwnd, gwl_userdata,
Makelong (rcwnd. Right-rcwnd. Left,
Rcwnd. Bottom-rcwnd. Top ));
: Showwindow (pwndbox-> m_hwnd, sw_hide );
}
: Setwindowpos (hwnd, null, 0, 0,
Rcdefaultbox. Right-rcwnd. Left,
Rcdefaultbox. Bottom-rcwnd. Top,
Swp_nozorder | swp_nomove );
If (fexpand)
{
DWORD dwdims = getwindowlong (hwnd, gwl_userdata );
: Setwindowpos (hwnd, null, 0, 0,
Loword (dwdims), hiword (dwdims), swp_nozorder | swp_nomove );
: Sendmessage (hwnd, dm_reposition, 0, 0 );
}
}
90. When the modal dialog box is called with domodal (), it is always displayed in the center. I reload it and add movewindow (), but its m_hwnd is a string of zero and the call fails. How can I display the called modal dialog box in a custom position?
Thanks
I don't know where you add movewindow (). The correct method is to add movewindow in oninitdialog, for example:
Movewindow (0, 1,300,200 );
Note that the first two parameters cannot be 0. If you really want to place the window at (0, 0), you can select absolute align in the properties of the dialog box design window, and then add
Movewindow (0, 0,300,200 );
Why? After reading the source program of MFC, you will understand it. It turns out that after MFC calls your oninitdialog, it will call
Cdialog: checkautocenter () (IN dlgcore. cpp) check whether the window needs to be centered. After reading this function, you can see why you need the above
.