7.1 Changing the window size, title, style7.1.1 Changes before the window is created
If you want to modify its size, title, and style before the application window is created, you should do so in the PreCreateWindow member function of the CMainFrame class. The function has a type that is the parameter of the CREATESTRUCT structure, if the value of the member variable in this parameter is modified, then this change will be reflected in the MFC underlying code, when the MFC underlying code calls the CreateWindowEx function to create the window, It will use the changed parameter values to create this window.
1) Change the window size
BOOL CMainFrame::P Recreatewindow (createstruct&cs) { if (! CFrameWnd::P Recreatewindow (CS)) return FALSE; TODO: Modify the window class or style Cs.cx = n here by modifying//CREATESTRUCT CS; cs.cy = $; return TRUE;}
2) Change the application title
The default window style for frames is a combination of Ws_overlappedwindow and fws_addtotitle styles. Where Fws_addtotitle is an MFC-specific style that instructs the framework to add a document title to the window caption. Therefore, if you want the window to display its own caption, simply remove the fws_addtotitle style from the window. Before you set the code for the window caption, add:
BOOL CMainFrame::P Recreatewindow (createstruct&cs) { if (! CFrameWnd::P Recreatewindow (CS)) return FALSE; TODO: Modify the window class or style Cs.cx = n here by modifying//CREATESTRUCT CS; cs.cy = $; Cs.style = cs.style & ~fws_addtotitle;// cs.style = Ws_overlappedwindow; Cs.lpszname = "title"; return TRUE;}
7.1.2 changing its style after a window is created
Modify its style properties after the application window is created, and call the SetWindowLong function implementation in the OnCreate function of the CMainFrame class.
SetWindowLong (HWND hwnd, int nIndex, Longdwnewlong)
The function is to change the properties of the setting window (including setting a new window style, setting a new window procedure, setting a new application instance handle, etc.). To change the style of the window, specify the second argument of the function as Gwl_style, and then specify the new window style by the third parameter.
If it is modified on the basis of an existing type, then you can use the GetWindowLong function to get the current type of the window and then modify it. For example:
SetWindowLong (M_hwnd, Gwl_style, GetWindowLong (M_hwnd, Gwl_style) & ~ws_maximizebox);
7.1.3 Changing the title and size after the window is created
Modify the caption after the application window is created, and call the SetWindowText function implementation in the OnCreate function of the CMainFrame class.
Modify the size after the application window is created, and call the SetWindowPos function implementation in the OnCreate function of the CMainFrame class.
7.2 Modifying the cursor, icon, background7.2.3 Changes before the window is created
Previously , the size, title, and style of the window were set when the window was created . The cursor, icon, and background are specified when the window class is designed . The design and registration of the window class is done automatically by MFC's underlying code, and we cannot, and should not, modify MFC's underlying code. But we can write our own window class registration and then let the subsequent window be created according to the window class we wrote.
BOOL CMainFrame::P Recreatewindow (createstruct&cs) { if (! CFrameWnd::P Recreatewindow (CS)) return FALSE; ... Wndclassmywnd; Mywnd.cbclsextra = NULL; Mywnd.cbwndextra = NULL; Mywnd.hbrbackground = (hbrush) getstockobject (Black_brush); Mywnd.hcursor = LoadCursor (NULL, idc_cross); Mywnd.hicon = LoadIcon (NULL, idi_warning); Mywnd.hinstance = AfxGetInstanceHandle (); Mywnd.lpfnwndproc =::D efwindowproc; Mywnd.lpszclassname = "Hello"; Mywnd.lpszmenuname = NULL; Mywnd.style = Cs_hredraw | Cs_vredraw; RegisterClass (&mywnd); Cs.lpszclass = "Hello"; return TRUE;}
The result of the above code is that only the program's title bar icon has changed, but the background and cursor of the window have not changed. The reason is: depending on the window of the main window, we see the window is actually the View class window, and the above code modifies the Frame class window background and cursor. The icon for the application belongs to the frame window, so the icon has changed since the program was run.
Conclusion: In MFC, if you want to modify the icon of an application window, you should do so in the framework class , because the frame window has a title bar, and if you want to modify the background and cursor of the program window, you should do so in the view class . Workaround: Add code in the PreCreateWindow function of the view class: cs. lpszclass="Hello";.
At the same time, MFC provides us with a global function afxregisterwndclass ().
LPCTSTR AFXAPI AfxRegisterWndClass (uintnclassstyle, Hcrsor hcursor = 0, Hbrush hbrbackground = 0, Hicon HICON = 0);
7.2.2 Changes after a window is created
To modify its cursor, icon, and background after the application window is created, you can call the Setclasslong function in the OnCreate function. DWORD Setclasslong (hwndhwnd, int nIndex, LONG dwnewlong)
The function is to reset the properties of the specified data member in the WNDCLASS structure of the window class to which the specified window belongs (including setting a new window background brush, cursor, icon, and window class style).
int CMainFrame::OnCreate (lpcreatestruct lpcreatestruct) { if (cframewnd::oncreate (lpcreatestruct) = =-1) return-1; ... Setclasslong (M_hwnd,gcl_hicon, (LONG) LoadIcon (NULL, idi_warning)); return 0;} int cmymfcappview::oncreate (lpcreatestruct lpcreatestruct) { if (cview::oncreate (lpcreatestruct) = =-1) return-1; ..... 7.2.2 changes the cursor, title bar icon, window background setclasslong (M_hwnd, Gcl_hbrbackground, (LONG) Getstockobject (Black_brush)) After the window is created; Setclasslong (M_hwnd, Gcl_hcursor, (LONG) loadcursor (NULL, Idc_cross)); return 0;}
7.3 Analog Animation icons7.3.1 Load icon resource
Add idi_icon1-4 resources, climate in the framework class defines an icon handle member variable, when an array is added, the type is set to Hicon [4] to be set successfully. Add code to the view class.
M_hicons[0] =:: LoadIcon (AfxGetInstanceHandle (), Makeintresource (Idi_icon1)); M_HICONS[1] =:: LoadIcon (Theapp.m_hinstance,makeintresource (Idi_icon2)); M_HICONS[2] =:: LoadIcon (AfxGetApp ()->m_hinstance,makeintresource (Idi_icon3)); M_HICONS[3] = AfxGetApp ()->loadicon (IDI_ICON4);
Where LoadIcon is the first parameter, when the first load is loaded, AfxGetInstanceHandle can get the application's current handle. The second argument, through the Makeintresourse macro, converts the ID to the corresponding resource designator string. The second application Theapp gets the CWinApp object of the application, whose data member M_hinstance gets the instance handle. However, an THEAPP global variable has already been defined in the app file, so extern Cmymfcapp Theapp before the Framework class OnCreate function. The third load is implemented using the AfxGetApp global function.
7.3.2 Setting Timers
In the Framework class OnCreate function add SetTime function, no 1000ms trigger a timer message.
SetTimer (1, +, NULL);
In the Framework class, add the timer message (Wm_timer) response function and change the application window icon in the response function call Setclasslong function.
void Cmainframe::ontimer (Uint_ptr nidevent) { //todo: Add Message Handler code here and/or call default value static int index = 1; Setclasslong (M_hwnd, Gcl_hicon, (LONG) m_hicons[index]); index = ++index% 4; Cframewnd::ontimer (nidevent); Cframewnd::ontimer (nidevent);}
Because the program calls the OnTimer function every time the timer message is sent, the index is defined as static (allocated in the stack).
7.4 Toolbar programming7.4.1 adding buttons to toolbars
Add a button idm_test to the far right of idr_mainframe in the toolbar of the resource. And under the menu bar Help, add a button with the same ID, caption set to test. Add a command response function Ontestshow. You can drag the T button to a point where there is some gap between the help and the T button. Want to delete just Del.
void Cmainframe::ontestshow () { //todo: Add Command handler code in MessageBox ("Teston toolbar");}
7.4.2 Creating a toolbar--4 steps
STEP1: creating toolbar resources;
STEP2: constructing CToolBar objects; main frame adding private variables
STEP3: Call the Create or CreateEx function for creating the window toolbar (toolbar is also a window)
STEP4: Call the LoadToolBar function to load the toolbar resource.
7.4.3 Creating a custom toolbar
In toolbar add a new resource, in the Framework class plus a member variable of type CToolBar, call the Create function created toolbar, associated with CToolBar, can be implemented in the OnCreate function of the framework class. Add a bit of code.
<span style= "White-space:pre" ></span>if (!m_newtoolbar.createex (this, Tbstyle_flat, WS_CHILD | ws_visible | Cbrs_top | Cbrs_gripper | Cbrs_tooltips | cbrs_flyby | cbrs_size_dynamic) | | ! M_newtoolbar.loadtoolbar (IDR_TOOLBAR1)) { TRACE0 ("Failed to create toolbar\n"); return-1; Fail to create } m_newtoolbar.enabledocking (cbrs_align_any); DockControlBar (&m_newtoolbar);
The CreateEx function creates a toolbar and associates it with the Toolbar object: M_newtoolbar, and the docking position is set to Cbrs_right. Call LoadToolBar to load the resource. Calling the EnableDocking function allows the toolbar to dock anywhere in the client area. Finally, call the DockControlBar function to dock the toolbar to the main frame window.
7.4.4 Show and Hide toolbars
In "View" from this ADA you download add a menu item. The id attribute is idm_view_newtoolbar,caption to "new Toolbar". Then add the command response function. In the response function, the display and hiding of the previously created toolbar is implemented. You can call the ShowWindow function. After that, you need to adjust their location to call the RecalcLayout function. Limit or hide the frame class DockControlBar function again after you call it. To dock the toolbar to the main frame window, use the DockControlBar function.
void Cmainframe::onviewnewtoolbar () { //todo: Add Command handler code here if (m_newtoolbar.iswindowvisible ()) m_ Newtoolbar.showwindow (sw_hide); else M_newtoolbar.showwindow (sw_show); RecalcLayout (); DockControlBar (&m_newtoolbar);}
How to make the newly created toolbar appear in the original display position. You need to call the Showcontrolbar function.
Below, add a check edit. To do this, add a update_command_ui function to the menu item. Add code internally.
void Cmainframe::onupdateviewnewtoolbar (CCmdUI *pcmdui) { //todo: Add command Update UI handler code //Add check box here pcmdui- >setcheck (M_newtoolbar.iswindowvisible ());}
7.5 Status Bar programming7.5.1 Cue lines and indicators for the status bar
The status bar is divided into two parts: a cue line and an indicator. The longest part of the left is the cue line, which is typically used to display a ToolTip for a menu item or a tool button. The section on the right, composed of several panes, is a status bar indicator, usually used to display information such as the case key, the number lock key, and so on.
The framework program specifically provides a indicators array to manage cue lines and indicators. If you want to modify the appearance of the status bar, you can simply add or reduce the corresponding string resource ID in the indicators array.
① new string resource ID in the resource editor:
Id_timer Clock
② adding a new string resource ID to the indicators array
③ gets the current time of the system (added to the back of the OnCreate function in CMainFrame)
CTime t = ctime::getcurrenttime (); CString str = T.format ("%h:%m:%s");
④ The string to the status bar's pane and calls the member function SetPaneText of the CStatusBar class. If you do not know the index of the pane, you can call the member function of the CStatusBar class Commandtoindex get. M_wndstatusbar.commandtoindex (id_timer)
M_wndstatusbar.setpanetext (1, str);
⑤ resizing panes
The member function of the CStatusBar class: Setpaneinfo, which sets a new ID, style, and width for the specified pane.
CCLIENTDC DC (this); CSize sz = DC. GetTextExtent (str); M_wndstatusbar.setpaneinfo (1, Id_timer, sbps_normal,sz.cx);
⑥ Add the relevant code in the OnTimer.
7.6 Adding a mouse coordinate display in the status bar
Method One: Call the SetWindowText function to set the status bar prompt line text
Need to add header file and change M_wndstatusbar to public
CString str; Str. Format ("x=%d, y=%d", Point.x, Point.y); ((cmainframe*) GetParent ())->m_wndstatusbar.setwindowtext (str);
Method Two: Using the CFrameWnd class member function Setmessagetext implementation, the function is to set the text in the prompt line of the status bar.
((cmainframe*) GetParent ())->setmessagetext (str);
Method Three: Use the member function of the CFrameWnd class Getmessagebar to return a pointer to the status bar object, so that you do not have to access the CMainFrame class's protected member variable: M_wndstatusbar.
((cmainframe*) GetParent ())->getmessagebar ()->setwindowtext (str);
Digits diagram in the cue line (with the help of bitmap buttons)
Add the CBitmapButton m_bmp variable to the framework class.
CRect RC (4, +); if (!m_bmp. Create ("", Ws_child | ws_visible | Bs_ownerdraw, RC, &m_wndstatusbar, 0)) return FALSE; if (!m_bmp. Loadbitmaps (IDB_BITMAP1, NULL, NULL, NULL)) return FALSE;
7.7 Adding a splash screen7.7.1 uses the classes provided in the VC + + component library to complete
Click [Project \ ADD to project \components and controls ...], and in the popup Components and Controls Library dialog box, double-click the "Visual C + + components" directory where the SplashScreen component is found.
Replace the splsh.bmp bitmap file to replace the boot interface;
Change SetTimer (1, +, NULL) to set the dwell time of the splash screen.
7.7.2 vs Add splash screen
1) Create a new bitmap resource for ID_BITMAP3.
2) Now a new class Csplashwnd under the project, the base class is CWnd;
3) Add a variant of type protected under this class CBitmap M_bitmap
4) Add a CREATE function to load the bitmap, creating the window;
Public: BOOL Create (); BOOL csplashwnd::create () { if (!m_bitmap). LoadBitmap (IDB_BITMAP3)) return false; BITMAP BM; M_bitmap. Getbitmap (&BM); Return CreateEx (0, AfxRegisterWndClass (0,afxgetapp ()->loadstandardcursor (Idc_arrow)), NULL, Ws_popup | Ws_visible, 0,0, bm.bmwidth, bm.bmheight, NULL, NULL);}
5) Display window send WM_PAINT message, map this message;
void Csplashwnd::onpaint () {CPAINTDC DC (this),//device context for painting //TODO: Add Message Handler code here //Do not call C for drawing messages Wnd::onpaint () CDC dcimage;if (!dcimage. CreateCompatibleDC (&DC)) return; BITMAP Bm;m_bitmap. Getbitmap (&BM); cbitmap* Poldbitmap = dcimage. SelectObject (&m_bitmap);d C. BitBlt (0, 0, bm.bmwidth, bm.bmheight, &dcimage, 0, 0, srccopy);d CImage. SelectObject (POLDBITMAP);}
6) After you include the header file for the new class in the App class, add the following code to the InitInstance () function:
Csplashwnd *psplashwindow = new csplashwnd;//Create object psplashwindow->create (); Psplashwindow->centerwindow (); Psplashwindow->showwindow (sw_show); Display window Psplashwindow->updatewindow (); Sleep (2000); Indicates the splash screen duration psplashwindow->destroywindow ();//Destroy the splash screen delete psplashwindow;//delete
Seven. Interface Programming--windows Programming Course study notes