获取系统指标(SM_CYSCREEN); |给出错误的值|但是 GetSystemMetrics(SM_CXSCREEN);给出正确的值

发布于 2025-01-13 20:49:32 字数 998 浏览 6 评论 0原文

我在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一桥轻雨一伞开 2025-01-20 20:49:32

您发布的代码显示(几乎肯定)未定义的行为,因为您在 MessageBoxPrintf 函数中处理可变参数列表的方式不正确。

wchar_t* c = va_arg(v1, wchar_t*); 行中,您“假设”单个 wchar_t 参数 - 但是,在您的 main 中code> 函数,您正在传递两个 int 参数。这可能会或不会产生有意义的结果; – 在您的情况下,似乎第一个 int 参数被随后对 wsprintf 的调用正确解释,但第二个参数在某种程度上“在翻译中丢失”(表明上述未定义)行为)。

要正确处理该可变参数列表,您需要提取它,然后将其未处理传递给vswprintf 函数,然后它可以正确解释该参数列表:

int MessageBoxPrintf(const wchar_t* szCaption, const wchar_t* szFormat, ...)
{
    wchar_t buffer[1024];
    va_list v1;
    va_start(v1, szFormat);
    vswprintf(buffer, 1024, szFormat, v1);
    va_end(v1);
    return MessageBox(NULL, buffer, szCaption, 0);
}

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 single wchar_t argument – but, in your main function, you are passing two int arguments. This may or not produce meaningful results; – in your case, it seems that the first int argument is correctly interpreted by the subsequent call to wsprintf 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:

int MessageBoxPrintf(const wchar_t* szCaption, const wchar_t* szFormat, ...)
{
    wchar_t buffer[1024];
    va_list v1;
    va_start(v1, szFormat);
    vswprintf(buffer, 1024, szFormat, v1);
    va_end(v1);
    return MessageBox(NULL, buffer, szCaption, 0);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文