Develop WM application series Article indexes using C ++
Introduction:
This year, I was very depressed and felt like I was back in the Great Recession ~~~~ It's cool, but it's easy to write a blog ~ Hey. Today, we will use a small example of screen rotation to review the use of MFC and other basic operations.
Body:
Review previous articlesArticleWe have basically understood the MFC ApplicationProgramThe usefulness of the Wizard. Today, we are no exception. Open Vs and create a new one.C ++UnderSmart devicesProject, selectMFC smart device applicationTemplate, our project name is "screenmng", then I chose the SDK for wm5 for PPC, and then"Based on the dialog box", Finally confirm, the entire project framework is ready!
Then, openResource ViewThe dialog box is designed as follows:
It is also very simple. Each button corresponds to an angle and will be rotated for them later.CodeIt is easy to recognize.
For buttonAdd an event handlerFor example:
This is the wizard interface for handling 90 degrees of rotation. I believe everyone is familiar with it! We add their "bn_clicked" events for the four buttons respectively. In the following content, we need to understand two important knowledge:
1. devmode struct. What is this? He is very important. He can guess from the name that it is related to a certain mode of the device. Here, this mode can also be understood as a display mode. Of course, this interpretation is not completely correct, for more information, see the link to msdn http://msdn.microsoft.com/en-us/library/dd183565 (vs.85 ). the role and usage of each attribute in this struct have been described in detail in Aspx. As a result, we have solved most of the main difficulties, since we can define a struct to describe the display mode of a device, the next step is to tell the device to switch the display mode according to the defined struct ~
2. changedisplaysettingsex function, which is a new thing we have learned. In fact, if you have seen this article on msdn, you will probably understand it! I will not say much about the Chinese content. This function is easy to use. In combination with the above struct, The devmode struct and several other "nullable" parameters are passed in, we can switch the device to the display mode we want! The msdn address of this function is as follows: http://msdn.microsoft.com/en-us/library/aa923082.aspx, which is actually very simple ~~~~
OK. After preparing these two knowledge points, the rest is simple. First, define a specific struct for each button function and specify the button to be clicked, the display mode that the device needs to switch. Second, use the function to change the current display mode.
The main function code is as follows. Take the 90-degree button as an example:
Rotate 90-degree code
Void Cscreenmngdlg: onbnclickedbutton90 ()
{
// Todo: add the control notification handler code here
Devmode = { 0 };
Devmode. dmsize = Sizeof (Devmode );
Devmode. dmdisplayorientation = Dmdo_90; // Rotate 90 degrees
Devmode. dmfields = Dm_displayorientation;
Changedisplaysettingsex (null, & Devmode, null, 0 , Null );
Prepscreen ();
}
As you can see, there is also a prepscreen () method, which is actually a UI update method, mainly in a static control to display the current display mode. The Code is as follows, which is very simple:
Prepscreen code
// Prepare screen Public Functions
Void Cscreenmngdlg: prepscreen ( Void )
{
Cstring B;
// Get Current Mode
Devmode = { 0 };
Devmode. dmsize = Sizeof (Devmode );
Devmode. dmfields = Dm_displayorientation;
Changedisplaysettingsex (null, & Devmode, 0 , Cds_test, null );
Switch (Devmode. dmdisplayorientation)
{
Case Dmdo_0:
B = _ T ( " 0 degree/Default Mode " );
Break ;
Case Dmdo_90:
B = _ T ( " 90 degree/horizontal mode " );
Break ;
Case Dmdo_180:
B = _ T ( " 180 degrees/vertical mode " );
Break ;
Case Dmdo_270:
B = _ T ( " 270 degrees/inverted Mode " );
Break ;
}
State. setwindowtext (B );
}
It should be noted that the state variable is a control variable added to the static1 control. You don't know how to add it ~~~ Khan ~~~, In this function, we also learned how to get the current display mode, right!
So far, the entire example is coming to an end. After I click the 270 button, the horizontal screen of the simulator proves that the program is correct. However, when I close the program, the device remains in the 270 display mode and is dizzy ~ This tail is big! So I thought of the onclose function. Naturally, I added the onclose message handler for the dialog box by clicking any blank area in the resource view, in the Properties window that appears on the right, select the message icon, find "wm_onclose", and select Add onclose function, as shown in:
Then, return to the code implementation file and add the following code. I think you are similar to me:
Onclose code
Void Cscreenmngdlg: onclose ()
{
// Todo: add the message processing program code and/or call the default value here
Devmode = { 0 };
Devmode. dmsize = Sizeof (Devmode );
Devmode. dmdisplayorientation = Dmdo_0; // Back to default status
Devmode. dmfields = Dm_displayorientation;
Changedisplaysettingsex (null, & Devmode, null, 0 , Null );
Prepscreen ();
Cdialog: onclose ();
}
However, the reality is cruel. I tested it. This Code does not work when I close the form, and the device is still horizontal. So I searched for information online and found an interesting thing: onclose and ondestroy, so far I have gained a lot. After reading this, I added the ondestroy message processing function to the dialog box form and copied the same code from onclose to ondestroy, this allows the device to resume normal display after the program is disabled:
Ondestroy code
Void Cscreenmngdlg: ondestroy ()
{
Cdialog: ondestroy ();
// Todo: add the message processing program code here
Devmode = { 0 };
Devmode. dmsize = Sizeof (Devmode );
Devmode. dmdisplayorientation = Dmdo_0; // Back to default status
Devmode. dmfields = Dm_displayorientation;
Changedisplaysettingsex (null, & Devmode, null, 0 , Null );
Prepscreen ();
}
Summary:
1. In today's article, we reviewed the steps for creating an MFC program, adding controls, adding handlers for controls, and adding variables!
2. We also learned the devmode struct and the simple use of the changedisplaysettingsex function.
3. We learned to add a message handler for the form, such as onclose and ondestroy.
4. Finally, we also learned about the differences and connections between onclose and ondestroy.
The project code for this example is attached: click here to view