Develop WM application series with C ++ (7)-view memory and power usage

Source: Internet
Author: User

Develop WM application series Article indexes using C ++

Introduction:

After the preparations in the early stage, I believe that you should be familiar with the development of MFC. Before that, I planned to talk about the use of controls and found that many books have already said that, and better than me. In order not to miss people, haha, let's look at some features that are highly relevant to the device. For example, we are talking about getting the device memory status and power usage.

Body:

Before starting, we need to learn two API functions and their struct:

1. Get device memory informationGlobalmemorystatus,Microsoft's msdn is very detailed, including those used with himMemorystatusStruct is also introduced. Note:"ThisGlobalmemorystatusCan Return incorrect information. UseGlobalmemorystatusexFunction instead.", Of course, in our study, we did not adopt the advice of Ms ~ Khan!

2. Obtain device power informationGetsystempowerstatusexIn the same way, we can findSystem_power_status_exStruct is also very simple.

Next, let's take a look. In the introduction to msdn, we mention:GetsystempowerstatusexA bool value is returned to indicate whether the method is successfully called: This function returns true if successful; otherwise, it returns false.

WhileGetsystempowerstatusexThere is a bool parameter in the instantiation method, which is used to indicate which battery information this method obtains. If it is true, the latest information is obtained from the device driver, otherwise, obtain information that may expire several seconds from the cache: If this Boolean is set to true,GetsystempowerstatusexGets the latest information from the device driver, otherwise it retrieves cached information that may be out-of-date by several seconds.

On the other handGlobalmemorystatusMethod, it is very simple, there is no return value, only struct parameters, so we will not introduce it too much here, but I haven't found any problem about the MS sentence, I hope you will share your experience in the case of "errors.

After learning the above two functions, we startCodeFirst of all, let's build an MFC smart device application under C ++.ProgramProject, wm5 platform, based on the dialog box, other default.

Then, our interface is as follows:

A static text control (power) and a button (getbettery) Add the control variable txtlife and OK to the static control!

Below, because the two functions we want to use need to reference the library, we right-click the project name and open the Properties window. In the window, we click the serial number, in the additional dependency, enter "coredll. lib ":

Click, OK, OK!

Next, we will add an event handler for the button:

 

Button code

  Void  Cmfcpowerstatudlg: onbnclickedbutton1 ()
{
System_power_status_ex spse; // Defined power struct
Memorystatus MMS; // Memory information struct defined

If (Getsystempowerstatusex ( & Spse, True )) // If the call is successful, run the following code:
{
Cstring betterlife;
Betterlife. Format (L " % D " , Spse. batterylifepercent ); // Only the remaining power is obtained here.
Txtlife. setwindowtext (betterlife );
Globalmemorystatus ( & MMS ); // Directly call the method to obtain memory information

Cstring meminfo;
Meminfo. Format (L " Memory load: % LD \ n totalphys: % LD \ n availphys: % LD \ n totalvirtual: % LD \ n availvirtual: % LD \ n " , MMs. dwmemoryload, MMs. dwtotalphys / 1024 , MMs. dwavailphys / 1024 , MMs. dwtotalvirtual / 1024 , MMs. dwavailvirtual / 1024 );
Afxmessagebox (meminfo );
}
Else
{
Afxmessagebox (L " Error " );
}
}

 

It's that simple.However, here we need to add that the struct includes the byte type and the DWORD type. The byte type uses % d for conversion, and the DWORD type uses % s for conversion, that day I was stuck on this small problem! The importance of the foundation can be seen!

The following figure shows the effect I have performed on the simulator. Check whether the result is successful! Here I would like to mention,How to set the power of the simulator? Click the form menu of the simulator-file-configuration-jumpPeripheral DeviceTab, then you can see ~ :>

This is the current power I set for the simulator: 69%

The following figure shows the simulator memory:

The following figure shows the running status:

 

Summary:

1. Today, we have learned about two commonly used functions and the use of their supporting structures.

2. We should pay attention to some small details, such as byte type conversion and cstring conversion)

We plan to study deep problems in the next blog ~~~~ To be found ~~~~

Related RecommendationsArticle:

Http://www.cnblogs.com/procoder/archive/2009/04/01/1427187.html

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.