Basic functions of windows32 and GetSystemMetrics

Source: Internet
Author: User

There is a bad message. The printf function cannot be used in windows programs.

The good news is that you can still use sptrinf and other functions of the series to display text.

The following describes the definitions of printf and sprintf.

[Cpp]
Int printf (const char * szFormat ,...);
 
Int sprintf (char * szBuffer, const char * szFormat ,...);

Int printf (const char * szFormat ,...);

Int sprintf (char * szBuffer, const char * szFormat,...); In the console, You can output:

[Cpp]
# Include <iostream>
# Include <string>
Using namespace std;
Int main (){
Printf ("the sum of % I and % I is % I \ n", 5, 3, 5 + 3 );
 
System ("pause ");
Return 0;
}

# Include <iostream>
# Include <string>
Using namespace std;
Int main (){
Printf ("the sum of % I and % I is % I \ n", 5, 3, 5 + 3 );

System ("pause ");
Return 0;
}
Sprintf is used below, which is equivalent to the above:

[Cpp]
# Include <iostream>
# Include <string>
Using namespace std;
Int main (){
Char buffer [100];
Sprintf (buffer, "the sum of % I and % I is % I", 5, 3, 5 + 3 );
Puts (buffer );
System ("pause ");
Return 0;
}

# Include <iostream>
# Include <string>
Using namespace std;
Int main (){
Char buffer [100];
Sprintf (buffer, "the sum of % I and % I is % I", 5, 3, 5 + 3 );
Puts (buffer );
System ("pause ");
Return 0;
}

Unicode:

If UNICODE is not defined, then:

Typedef char TCHAR

If UNICODE is defined:

Typedef wchar_t TCHAR

The biggest difference between wchar_t and char is that one occupies two bytes and the other occupies one byte:

[Cpp]
# Include <iostream>
# Include <string>
Using namespace std;
Int main (){
Wchar_t p [] = L "hello ";
Cout <wcslen (p) <endl;
Cout <sizeof (p) <endl; // OUTPUT 12
System ("pause ");
Return 0;
}

# Include <iostream>
# Include <string>
Using namespace std;
Int main (){
Wchar_t p [] = L "hello ";
Cout <wcslen (p) <endl;
Cout <sizeof (p) <endl; // OUTPUT 12
System ("pause ");
Return 0;
}
The following is an example of Output Using MessageBoxPrintf: (GetSystemMetric is very important)

[Cpp]
# Include <windows. h>
# Include <tchar. h>
# Include <stdio. h>
Int CDECL MessageBoxPrintf (TCHAR * szCaption, TCHAR * szFormat ,...){
TCHAR szBuffer [1024];
Va_list pArgList;
Va_start (pArgList, szFormat );
_ Vsntprintf (szBuffer, sizeof (szBuffer)/sizeof (TCHAR), szFormat, pArgList );
Va_end (pArgList );
Return MessageBox (NULL, szBuffer, szCaption, 0 );
}
Int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR sc1_line, int iCmdShow ){
Int cxScreen, cyScreen;
CxScreen = GetSystemMetrics (SM_CXSCREEN );
CyScreen = GetSystemMetrics (SM_CYSCREEN );
MessageBoxPrintf (TEXT ("ScrnSize"), TEXT ("The screen is % I pixels wide by % I pixels high"), cxScreen, cyScreen );
Return 0;
}

# Include <windows. h>
# Include <tchar. h>
# Include <stdio. h>
Int CDECL MessageBoxPrintf (TCHAR * szCaption, TCHAR * szFormat ,...){
TCHAR szBuffer [1024];
Va_list pArgList;
Va_start (pArgList, szFormat );
_ Vsntprintf (szBuffer, sizeof (szBuffer)/sizeof (TCHAR), szFormat, pArgList );
Va_end (pArgList );
Return MessageBox (NULL, szBuffer, szCaption, 0 );
}
Int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR sc1_line, int iCmdShow ){
Int cxScreen, cyScreen;
CxScreen = GetSystemMetrics (SM_CXSCREEN );
CyScreen = GetSystemMetrics (SM_CYSCREEN );
MessageBoxPrintf (TEXT ("ScrnSize"), TEXT ("The screen is % I pixels wide by % I pixels high"), cxScreen, cyScreen );
Return 0;
}
The result is as follows:

 
Author: zh634455283
 

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.