获取系统指标(SM_CYSCREEN); |给出错误的值|但是 GetSystemMetrics(SM_CXSCREEN);给出正确的值
我在使用 GetSystemMetrics(SM_CYSCREEN);
时遇到问题 - 每次运行程序时该函数返回的高度都是随机的,但宽度函数 GetSystemMetrics(SM_CXSCREEN);
给出正确的值。
这是我的代码:
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
int MessageBoxPrintf(const wchar_t * szCaption, const wchar_t * szFormat, ...) {
wchar_t buffer[1024];
va_list v1;
va_start(v1, szFormat);
wchar_t* c = va_arg(v1, wchar_t*);
wsprintf(buffer, szFormat, c); //gives formated output to buffer
va_end(v1);
return MessageBox(NULL, buffer, szCaption, 0);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {
int cxScreen, cyScreen;
cxScreen = GetSystemMetrics(SM_CXSCREEN);
cyScreen = GetSystemMetrics(SM_CYSCREEN);
MessageBoxPrintf(TEXT("ScreenRes"), TEXT("The screen is %i pixels wide by %i pixels high"), cxScreen, cyScreen);
return 0;
}
该程序基本上只是一个带有 WinAPI 和 C++ 的格式字符串消息框。
I have a problem with GetSystemMetrics(SM_CYSCREEN);
– the height this function returns is random each time I run the program but the width function, GetSystemMetrics(SM_CXSCREEN);
gives the correct value.
Here is my code:
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
int MessageBoxPrintf(const wchar_t * szCaption, const wchar_t * szFormat, ...) {
wchar_t buffer[1024];
va_list v1;
va_start(v1, szFormat);
wchar_t* c = va_arg(v1, wchar_t*);
wsprintf(buffer, szFormat, c); //gives formated output to buffer
va_end(v1);
return MessageBox(NULL, buffer, szCaption, 0);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {
int cxScreen, cyScreen;
cxScreen = GetSystemMetrics(SM_CXSCREEN);
cyScreen = GetSystemMetrics(SM_CYSCREEN);
MessageBoxPrintf(TEXT("ScreenRes"), TEXT("The screen is %i pixels wide by %i pixels high"), cxScreen, cyScreen);
return 0;
}
This program basically just is a Format string Message box with the WinAPI and C++.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您发布的代码显示(几乎肯定)未定义的行为,因为您在
MessageBoxPrintf
函数中处理可变参数列表的方式不正确。在
wchar_t* c = va_arg(v1, wchar_t*);
行中,您“假设”单个wchar_t
参数 - 但是,在您的main
中code> 函数,您正在传递两个int
参数。这可能会或不会产生有意义的结果; – 在您的情况下,似乎第一个int
参数被随后对wsprintf
的调用正确解释,但第二个参数在某种程度上“在翻译中丢失”(表明上述未定义)行为)。要正确处理该可变参数列表,您需要提取它,然后将其未处理传递给
vswprintf
函数,然后它可以正确解释该参数列表:The code you have posted shows (almost certainly) undefined behaviour, because of the incorrect way you handle the variadic argument list in your
MessageBoxPrintf
function.In the
wchar_t* c = va_arg(v1, wchar_t*);
line, you are 'assuming' a singlewchar_t
argument – but, in yourmain
function, you are passing twoint
arguments. This may or not produce meaningful results; – in your case, it seems that the firstint
argument is correctly interpreted by the subsequent call towsprintf
but the second is somewhat "lost in translation" (manifesting the aforementioned undefined behaviour).To correctly handle that variadic argument list, you need to extract it, then pass it unprocessed to the
vswprintf
function, which can then properly interpret that argument list: