Windows程序设计使用CreatewWindow创建按钮不显示
#include<windows.h>#define NUM 5
#define GROUPBOX NUM+1
#define PUSHBUTTON1 NUM+2
#define PUSHBUTTON2 NUM+3
#define PUSHBUTTON3 NUM+4
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {
static TCHAR szAppName[] = TEXT("翻牌子");
HWND hwnd=0;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass))
{
MessageBox(hwnd, TEXT("抱歉,本程序需要在Windows NT上运行!"), szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(szAppName, TEXT("赛博选妃"),
WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HWND hwndButton;
static RECT rect;
const TCHAR* Name[] = { TEXT("王昭君"),TEXT("貂蝉"),TEXT("西施"),TEXT("杨玉环"),TEXT("AAA") };
static int cxChar, cyChar;
HDC hdc;
PAINTSTRUCT ps;
int i;
switch (message)
{
case WM_CREATE:
cxChar = LOWORD(GetDialogBaseUnits());
cyChar = HIWORD(GetDialogBaseUnits());
GetWindowRect(hwnd, &rect);
MoveWindow(hwnd, rect.left, rect.top, 27 * cxChar, 13 * cyChar, TRUE);
for ( i = 0; i < NUM-1; i++)
{
hwndButton= CreateWindow(TEXT("BUTTONS"),
Name,
BS_AUTORADIOBUTTON|WS_VISIBLE| WS_CHILD,
2*cxChar, cyChar * (2+2*i),
10* cxChar, 7 * cyChar/4,
hwnd, (HMENU)i,
((LPCREATESTRUCT)lParam)->hInstance, NULL);
}
//CreateWindow(TEXT("BUTTONS"),
// TEXT("美女们"),
// BS_GROUPBOX | WS_CHILD | WS_VISIBLE,
// cxChar, cyChar / 2,
// 12 * cxChar, 10 * cyChar,
// hwnd, (HMENU)GROUPBOX,
// ((LPCREATESTRUCT)lParam)->hInstance, NULL);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
代码如上图,理论上讲运行此代码应该会出现四个按钮,但是实际运行中并未出现,求助一下问题出现在哪里? https://img-mid.csdnimg.cn/release/static/image/mid/ask/756f8f80fa914b4eb5f9bd5626af5ec2.png?x-oss-process=image/auto-orient,1/resize,w_320,m_lfit
运行结果如图 看起来你在创建按钮的时候遇到了一些问题。让我们一起来看看并找到解决方案。
首先,你需要注意的是,在调用 `CreateWindow` 函数时,控件的类名应该是 `"BUTTON"`(字符串类型),而不是 `"BUTTONS"`。只要把 `hwndButton= CreateWindow(TEXT("BUTTONS"), ...` 中的 `"BUTTONS"` 替换成 `"BUTTON"`,你的按钮应该就能正常显示了。
以下是更正后的部分代码:
hwndButton = CreateWindow(TEXT("BUTTON"),
Name,
BS_AUTORADIOBUTTON | WS_VISIBLE | WS_CHILD,
2 * cxChar, cyChar * (2 + 2 * i),
10 * cxChar, 7 * cyChar / 4,
hwnd, (HMENU)i,
((LPCREATESTRUCT)lParam)->hInstance, NULL);
另外,你的 `Name` 数组中的按钮名称使用了 `TEXT`,这是正确的,但是在循环中你应该用 `Name` 来设置每个按钮的名字,不然所有按钮都会有相同的名字,这不是我们想要的。
最后,虽然你注释掉了 `GROUPBOX` 的创建,但如果你想在窗口中添加一个分组框只需确保它也用 `"BUTTON"` 作为类名。
以下是更新后的 `WM_CREATE` 部分的完整代码示例:
case WM_CREATE:
cxChar = LOWORD(GetDialogBaseUnits());
cyChar = HIWORD(GetDialogBaseUnits());
GetWindowRect(hwnd, &rect);
MoveWindow(hwnd, rect.left, rect.top, 27 * cxChar, 13 * cyChar, TRUE);
for (i = 0; i < NUM - 1; i++)
{
hwndButton = CreateWindow(TEXT("BUTTON"),
Name,
BS_AUTORADIOBUTTON | WS_VISIBLE | WS_CHILD,
2 * cxChar, cyChar * (2 + 2 * i),
10 * cxChar, 7 * cyChar / 4,
hwnd, (HMENU)i,
((LPCREATESTRUCT)lParam)->hInstance, NULL);
}
return 0;
希望这些修改能够帮助你顺利显示按钮,继续加油!如果按钮不显示,你可以考虑按 Ctrl+V 把代码粘贴到你的 IDE 中重新编译和运行,保证它们能像你的幽灵面具——显现出来!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 此贴终结,我以为CreateWindow函数第一个参数类名是可以随便起的,是我理解错了
页:
[1]