我需要在 C 中創建一個 WinAPI 視窗。不是 C 。在 C 中,當我制作視窗時,調整大小有問題。當我將它調整得更大時,它會變成一個黑色背景,里面有奇怪的白斑。解決此問題的唯一方法是將其設為原始大小。C 不會發生這種情況。我怎樣才能解決這個問題?它編譯沒有錯誤。
正常尺寸: 顯示正確
最大化: 它會產生奇怪的效果。
代碼:
wmain.h
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
const wchar_t* szWndClassName = L"WindowClass"; const wchar_t* szWndName = L"Notepad";
int width = 600, height = 400;
HINSTANCE hInst; HWND hWnd;
WNDCLASS wc;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
RECT rect;
int CenterWindow(HWND parent_window, int width, int height)
{
GetClientRect(parent_window, &rect);
rect.left = (rect.right / 2) - (width / 2);
rect.top = (rect.bottom / 2) - (height / 2);
return 0;
}
主檔案
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include "wmain.h"
#pragma warning (disable: 28251)
int WINAPI WinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst, LPSTR lpszCMDArgs, int nCMDShow)
{
hInst = hThisInst;
wc.lpszClassName = szWndClassName;
wc.lpfnWndProc = WndProc;
wc.hInstance = hInst;
wc.hCursor = LoadCursor(wc.hInstance, L"IDC_ARROW");
wc.hIcon = LoadIcon(wc.hInstance, L"Resource Files/Images/Notepad.ico");
if (!RegisterClass(&wc))
{
MessageBox(NULL, L"RegisterClassW failed!", L"Error", MB_ICONERROR);
return 1;
}
CenterWindow(GetDesktopWindow(), width, height);
hWnd = CreateWindow(szWndClassName, szWndName, WS_OVERLAPPEDWINDOW, rect.left, rect.top, width, height, NULL, NULL, hInst, NULL);
if (!hWnd)
{
MessageBox(NULL, L"CreateWindowW failed!", L"Error", MB_ICONERROR);
return 2;
}
ShowWindow(hWnd, nCMDShow);
UpdateWindow(hWnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
{
switch (msg)
{
case WM_CREATE:
break;
case WM_COMMAND:
switch (wp)
{
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, msg, wp, lp);
}
}
編輯
要解決此問題,請將其添加到您的視窗程序中:case WM_ERASEBKGND:
wc.hbrBacground = (HBRUSH)(COLOR_WINDOW 1);
break;
uj5u.com熱心網友回復:
要么在 中設定視窗背景WNDCLASS,要么實作WM_PAINT訊息重繪視窗。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/393252.html
