Windows Programming _ sun Xin C ++ lesson9 application appearance Modification

Source: Internet
Author: User

Windows Programming _ sun Xin C ++ lesson9 application appearance Modification

Highlights of this section:
1. Change the window style
2. toolbar Programming
3. Status Bar Programming
4. progress bar Programming
5. Start the screen splashwnd
//************************************** **************************************** ****************************
1. Change the window style
(1) Use the precreatewindow function before creating a window, and use the createstruct structure in reference mode to modify the window style.
//************************************** **************************************** ****************************
Bool cmainframe: precreatewindow (createstruct & CS)
{
If (! Cframewnd: precreatewindow (CS ))
Return false;
// Todo: Modify the window class or styles here by modifying
// The createstruct CS
// Modify the window style using the referenced createstruct Structure
CS. Style = cs. Style &~ Fws_addtotitle; // unlike the operation to remove the automatically added document name to the window, it is equivalent to CS. Style = ws_overlappedwindow;
CS. lpszname = "changed window name ";
CS. Cy =: getsystemmetrics (sm_cyscreen );
CS. Cx =: getsystemmetrics (sm_cxscreen); // create full screen
Return true;
}
//************************************** **************************************** ****************************
(2) Use the setwindowlong function in the oncreate function (create Framework Window) after the window is created.
//: Setwindowlong (m_hwnd, gwl_style, ws_overlappedwindow); // set the window style
//: Setwindowlong (m_hwnd, gwl_style, getwindowlong (m_hwnd, gwl_style )&~ Ws_maximizebox); // you can specify a window style.
(3) change the icon and cursor of the window, use the wndclass compiled by yourself, and register it (the underlying code of the pre-defined class generated by MFC cannot be modified ).
Implemented in the precreatewindow of the cmainframe class and the precreatewindow of the View class. The afxregisterwndclass function is concise.
//************************************** **************************************** ****************************
Bool cmainframe: precreatewindow (createstruct & CS)
{
If (! Cframewnd: precreatewindow (CS ))
Return false;
// Todo: Modify the window class or styles here by modifying
// The createstruct CS
// Register your own window to change the window style
/*
Wndclass wndcls;
Wndcls. Style = cs_hredraw | cs_vredraw; // window class type
Wndcls. lpfnwndproc =: defwindowproc; // global
Wndcls. cbclsextra = 0;
Wndcls. cbwndextra = 0;
Wndcls. hinstance = AfxGetInstanceHandle (); // obtain the application instance handle
Wndcls. hicon = loadicon (null, idi_error );
Wndcls. hcursor = loadcursor (null, idc_help );
Wndcls. hbrbackground = (hbrush) (getstockobject (black_brush ));
Wndcls. lpszmenuname = NULL;
Wndcls. lpszclassname = "qiaoqiao ";
// Register a new window class
Registerclass (& wndcls );
// Modify the class name
CS. lpszclass = "qiaoqiao ";
*/
// Quick method for modifying window styles
// CS. lpszclass =: afxregisterwndclass (cs_hredraw | cs_vredraw, 0, loadicon (null, idi_warning ));
// CS. lpszclass =: afxregisterwndclass (cs_hredraw | cs_vredraw );
Return true;
}
//************************************** **************************************** ****************************
(4) After the window is created, use the setclasslong function to modify the window class value (icon, cursor, and background color)
Setclasslong (m_hwnd, gcl_hicon, (long) loadicon (null, idi_error ));
Setclasslong (m_hwnd, gcl_hbrbackground, (long) (getstockobject (black_brush )));
Setclasslong (m_hwnd, gcl_hcursor, (long) loadcursor (null, idc_help ));

//************************************** **************************************** ******************************

After changing the window style during the experiment, the effect is shown in:

(5) applications of the above functions-constantly changing application icons
First, add the icon resource, load and save the icon resource, and then use setclasslong to regularly change the icon of the window.
The experiment code is as follows: add
Hicon m_hicon [3]; // Save the resource handle of the icon
// Load and save the resource handle of the icon
// In cmainframe: oncreate (), you can obtain the application instance handle in three ways.
M_hicon [0] = loadicon (AfxGetInstanceHandle (), makeintresource (idi_icon1 ));
M_hicon [1] = loadicon (theapp. m_hinstance, makeintresource (idi_icon2 ));
M_hicon [2] = loadicon (afxgetapp ()-> m_hinstance, makeintresource (idi_icon3 ));
Setclasslong (m_hwnd, gcl_hicon, (long) m_hicon [0]);
Settimer (, null); // sets the timer
// Respond to the wm_timer message
//************************************** **************************************** ****************************
Void cmainframe: ontimer (uint nidevent)
{
// Todo: add your message handler code here and/or call default
Static Index = 1;
Setclasslong (m_hwnd, gcl_hicon, (long) m_hicon [Index]);
Index = ++ index % 3;
Cframewnd: ontimer (nidevent );
}
//************************************** **************************************** ****************************

2. toolbar Programming
(1) the delimiters between buttons differentiate functions.
(2) Add a separator and drag the small icon to the right.
Delete the icon and drag it out of the workspace on the toolbar.
(3) create and display the toolbar
STEP (there are two methods, one of which ):
Step 1: Create a toolbar resource.
In the resource editing view, add the toolbar idm_toolbar1 and add an icon to it.
Step 2: Construct the ctoolbar object.
Ctoolbar m_wndnewtoolbar; // step2 Add a member variable
Step 3: Call the create (or createex) function to create the Windows toolbar and attach it to the ctoolbar object.
Step 4: Call loadtoolbar to load the toolbar resource.
If (! M_wndnewtoolbar.createex (this, tbstyle_flat, ws_child | ws_visible | cbrs_right
| Cbrs_gripper | cbrs_tooltips | cbrs_flyby | cbrs_size_dynamic) |
! M_wndnewtoolbar.loadtoolbar (idr_toolbar1 ))
{
Trace0 ("failed to create Toolbar \ n ");
Return-1; // fail to create
}
Step 5: Set docking Step 5
M_wndnewtoolbar.enabledocking (cbrs_align_any );
Dockcontrolbar (& m_wndnewtoolbar );
(4) Add a menu item, there is no toolbar, and the bar is hidden and displayed in the original position.
// Display or hide the toolbar experiment Code as follows:
//************************************** **************************************** ****************************
Void cmainframe: ontoolbarnew ()
{
// Todo: add your command handler code here
// Display or hide toolbar method 1 recommended method 2
/*
If (! M_wndnewtoolbar.iswindowvisible ())
{
M_wndnewtoolbar.showwindow (sw_shownormal );
}
Else
M_wndnewtoolbar.showwindow (sw_hide );
*/
// Method 2
Showcontrolbar (& m_wndnewtoolbar ,! M_wndnewtoolbar.iswindowvisible (), false );
Recalclayout (); // adjust the Layout
}
Void cmainframe: onupdatemedilbarnew (ccmdui * pcmdui)
{
// Todo: add your command update UI handler code here
Pcmdui-> setcheck (m_wndnewtoolbar.iswindowvisible (); // mark the newly created Toolbar
}
//************************************** **************************************** ****************************
3. Status Bar Programming
(1) pane of the status bar
Static uint indicato is determined by the number of members in the indicator array.
RS [] =
{
Id_separator, // status line indicator
Id_indicator_caps,
Id_indicator_num,
Id_indicator_scrl,
};
Setindicators (indicators, sizeof (indicators)/sizeof (uint) is used to set the indicator.
(2) set the text in the pane of the status bar
Commandtoindex: Obtain the index from the ID.
Setpanetext: Set panel text
Setpaneinfo: Set Panel specifications
(3) write time in the status bar
In the wm_timer message response, obtain the position and size of the time pane in the status bar, and then fill the time.
The experiment code is as follows:
//************************************** **************************************** ****************************
Step 1: add an indicator
Static uint indicators [] =
{
Id_separator, // status line indicator
Ids_timer,
Ids_progress,
Id_indicator_caps,
Id_indicator_num,
Id_indicator_scrl,
};
Step 2: // display the time
Void cmainframe: ontimer (uint nidevent)
{
// Todo: add your message handler code here and/or call default
// Display time
Ctime T = ctime: getcurrenttime ();
Cstring strtime = T. Format ("% H: % m: % s ");
Cclientdc DC (this );
Csize cs = Dc. gettextextent (strtime); // obtain the string width
M_wndstatusbar.setpaneinfo (1, ids_timer, sbps_normal, CS. CX );
// M_wndstatusbar.setpanetext (1, strtime );
M_wndstatusbar.setpanetext (m_wndstatusbar.commandtoindex (ids_timer), strtime );
}
//************************************** **************************************** ****************************
(4) write text information to the leftmost of the status bar. The cursor coordinate information is used as an example.
Here, pay attention to three methods to obtain the status bar pointer. The experiment code is as follows:
//************************************** **************************************** ****************************
Void cstyleview: onmousemove (uint nflags, cpoint point)
{
// Todo: add your message handler code here and/or call default
Cstring STR;
Str. Format ("x = % d, y = % d", point. X, point. Y); // format the mouse coordinate information
// Display text on the far left of the status bar
// (Cmainframe *) getparent ()-> m_wndstatusbar.setwindowtext (STR); // method 1 m_wndstatusbar is a public Member
// (Cmainframe *) getparent ()-> setmessagetext (STR); // method 2 m_wndstatusbar does not need to be a public Member
// (Cmainframe *) getparent ()-> getmessagebar ()-> setwindowtext (STR); // method 3
Getparent ()-> getdescendantwindow (afx_idw_status_bar)-> setwindowtext (STR); // Method 4
Cview: onmousemove (nflags, point );
}
//************************************** **************************************** ****************************
4. progress bar Programming
(1) obtain the size of the progress bar in the status bar and place the progress bar in it. Here, you need to customize the message and obtain it in the message response; otherwise, the message fails.
(2) to avoid incorrect column position when the window changes, you need to re-obtain the progress bar position and reset the progress bar. (The persistent window and temporary window are mentioned here, which are to be learned)
(3) set the progress bar size and display
The experiment code is as follows:
//************************************** **************************************** ****************************
# Define um_progress wm_user + 1
Afx_msg void onprogress ();
On_message (um_progress, onprogress );
Void cmainframe: onprogress ()
{
// Create progress bar
Crect rect;
M_wndstatusbar.getitemrect (2, & rect );
M_ctlprs.create (ws_child | ws_visible | pbs_smooth, rect, & m_wndstatusbar, 1 );
M_ctlprs.setpos (50 );
}
In cmainframe: oncreate, execute postmessage (um_progress,). Note that after sendmessage is sent, the progress bar information in the status bar is still not obtained after the message is processed.
//************************************** **************************************** ****************************
// However, this method cannot solve the offset problem in the progress bar when the window size is adjusted, the solution is to re-obtain the position of the progress bar in the status bar in the wm_paint message and draw or move the progress bar.
Void cmainframe: onpaint ()
{
Cpaintdc DC (this); // device context for painting
 
// Todo: add your message handler code here
Crect rect;
M_wndstatusbar.getitemrect (2, & rect );
If (! M_ctlprs.m_hwnd) // create if the progress bar does not exist
{
M_ctlprs.create (ws_child | ws_visible | pbs_smooth, rect, & m_wndstatusbar, 1 );
Int nlower, nupper;
M_ctlprs.getrange (nlower, nupper );
M_ctlprs.setstep (nupper-nlower)/10); // you can specify the change size.
}
Else
M_ctlprs.movewindow (& rect); // If a progress bar exists, move it to the appropriate position.
M_ctlprs.setpos (50 );
// Do not call cframewnd: onpaint () for painting messages
}
// Make the progress bar change regularly
Void cmainframe: ontimer (uint nidevent)
{
// Todo: add your message handler code here and/or call default
// Progress bar changes
M_ctlprs.stepit ();
Cframewnd: ontimer (nidevent );
}
//************************************** **************************************** ****************************

Shows the program running effect:

5. Start the screen splashwnd
We will not go into details here using the method of inserting controls.
Add a method (Reprinted from the Internet): the MFC will display the window after the program startup screen is closed.
Method 1: Pause the program:
Add the sleep (...) Statement to the csplashwnd: showsplashscreen (...). Set the time to the time when the screen timer is disabled.
In this way, when the screen is displayed, the program will be paused. When the response timer closes the screen, the program will be restored.
Although this method achieves the goal, suspending the program before the program Initialization is a waste of resources. method 2 is described below.
Method 2

1. Respond to the wm_activate message in the cmainframeo class and change the display type of the window to hidden.

Void cmainframe: activateframe (INT ncmdshow)
{
// Todo: add your specialized code here and/or call the base class
Ncmdshow = sw_hide;
Cframewnd: activateframe (ncmdshow );
}

2. In bool cmyproapp: initinstance (), change the normal display statement in the window to hide.

M_pmainwnd-> showwindow (sw_hide/* sw_shownormal */);

3. display the window in hidden functions on the startup Interface

Void csplashwnd: hidesplashscreen ()
{
.......
Afxgetmainwnd ()-> showwindow (sw_shownormal );
}
You can use the above adjustments to implement this function.
//************************************** **************************************** ****************************
Summary:
1. use different methods to modify the appearance of a program in different locations
2. Programming the toolbar, Status Bar, and progress bar
3. In particular, we need to understand and master how to get the pointers we need in applications from different aspects, and actively use our brains in future development to find a solution in a timely manner.
In this lesson, three methods are used to obtain the application instance handle:
AfxGetInstanceHandle (), theapp. m_hinstance, afxgetapp ()-> m_hinstance
In this lesson, four methods are used to obtain the status bar pointer:
(Cmainframe *) getparent ()-> m_wndstatusbar.setwindowtext (STR); // method 1
(Cmainframe *) getparent ()-> setmessagetext (STR); // method 2
(Cmainframe *) getparent ()-> getmessagebar ()-> setwindowtext (STR); // method 3
Getparent ()-> getdescendantwindow (afx_idw_status_bar)-> setwindowtext (STR); // Method 4
This idea is worth learning.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.