c++ - 为什么 WinAPI 不创建具有配置大小的窗口?

标签 c++ winapi

我对 C++ 编码还很陌生,所以这可能是一个非常简单的修复。我最近创建了一个基本的 WinMain 窗口。当我从 IDE 或 .exe 文件运行程序时,应用程序无法以正确的大小打开。

enter image description here

我可以调整窗口大小,但我不确定为什么它不能以该大小设置打开。

#define WIN32_LEAN_AND_MEAN
#include <windows.h> 

// Function prototypes
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int);
bool CreateMainWindow(HINSTANCE, int);
LRESULT WINAPI WinProc(HWND, UINT, WPARAM, LPARAM);
// global variable
HINSTANCE hinst;

// Constants 
const char CLASS_NAME[] = "WinMain";
const char APP_TITLE[] = "Hello World";
const char WINDOW_WIDTH = 400;  
const char WINDOW_HEIGHT = 400;

//==================================
// Starting point for the windows application
// Parameters are
// hInstance. Handle to the current instance of the application 
// hPrevInstance. Always NULL, obsolete parameter
// lpCmdLine. Pointer to null-terminated string of command arguements 
// nCmdShow. Specifies how the window is to be shown 
//=================================
int WINAPI WinMain(HINSTANCE    hInstance,
    HINSTANCE   hPrevInstance,
    LPSTR       lpCmdLine,
    int         nCmdShow )

{
    MSG msg;
    // Create thw window 
    if (!CreateMainWindow(hInstance, nCmdShow))
        return false;
    // Main message loop 
    int done = 0;
    while (!done)
    {
        // PeekMessage is a non blocking message for Windows messages 
        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            // Look for quit message
            if (msg.message == WM_QUIT)
                done = 1;
            // Decode and pass messages on to WinProc
            TranslateMessage(&msg);
            DispatchMessage(&msg);

        }

    }
    return msg.wParam;

    }

// Window event callback function 
LRESULT WINAPI WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    switch (msg)
    {
    case WM_DESTROY:
            //Tell windows to kill this program 
            PostQuitMessage(0);
            return 0;
    }
    return DefWindowProc(hWnd, msg, wParam, lParam);

}

// Create the window, returns False on error
bool CreateMainWindow(HINSTANCE hInstance, int nCmdShow)
{
    WNDCLASSEX wcx;
    HWND hwnd;

    // Fill in the window class structure with parameters 
    // That describe the main window 
    wcx.cbSize = sizeof(wcx);               // Size of the structure 
    wcx.style = CS_HREDRAW | CS_VREDRAW;    // Redraw if the size changes 
    wcx.lpfnWndProc = WinProc;              // Points to windows procedure 
    wcx.cbClsExtra = 0;                     // No extra class memory 
    wcx.cbWndExtra = 0;                     // No extra window memory
    wcx.hInstance = hInstance;
    wcx.hIcon = NULL;
    wcx.hCursor = LoadCursor(NULL, IDC_ARROW);      // Predifined arrow
    // Background brush
    wcx.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
    wcx.lpszMenuName = NULL;    // Name of menu resource 
    wcx.lpszClassName = CLASS_NAME; // Name of window class
    wcx.hIconSm = NULL;

    // Register the window class
    // RegisterClassEx return 0 on error
    if (RegisterClassEx(&wcx) == 0) // if error 
        return false;
    // Create Window
    hwnd = CreateWindow(
        CLASS_NAME,                     // Name of window class
        APP_TITLE,                      // Title bar text
        WS_OVERLAPPEDWINDOW,            // Window style
        CW_USEDEFAULT,                  // Default horizontal postion of window
        CW_USEDEFAULT,                  // Default vertical postion of window
        WINDOW_WIDTH,                   // Width of window 
        WINDOW_HEIGHT,                  // Height of window 
        (HWND) NULL,                    // No parent window 
        (HMENU) NULL,                   // No menu
        hInstance,                      // Handle to application window
        (LPVOID) NULL);                 // No window parameters
    // If there was an error the window
    if (!hwnd)
        return false;
    // Show the window
    ShowWindow(hwnd, nCmdShow);
    // Send a WM_PAINT message to the window procedure 
    UpdateWindow(hwnd);
        return true;
}

最佳答案

将两个常量定义从char改为int:

const int WINDOW_WIDTH = 400;  
const int WINDOW_HEIGHT = 400;

假设一个带符号的字符是 8 位,那么 char x = 400 实际上将 x 设置为 16。

关于c++ - 为什么 WinAPI 不创建具有配置大小的窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25568415/

相关文章:

visual-studio - 调试时如何在 Visual Studio 中查看有关 HWND 的信息?

c++ - 如何在ATL/WTL对话框上注册自定义控件?

c++ - Win32 API 检查当前窗口是对话框还是普通窗口

c++ - 为什么在我的单例类中未定义对互斥锁的引用

c++ - Boost spirit 需要永远解析表达式

c++ - 为什么 Windows 10 中 kernel32.dll 上的 GetFileVersionInfo 返回版本 6.2?

windows - 获取主题编辑控件的非客户区的大小

c++ - WinHttpSendRequest : 2148074273 insufficient cache

c++ - 在 C++ 中使用指针访问和操作数组

c++ - 如何用逗号写数字?