在主窗口中显示按钮时出现问题

在主窗口中显示按钮时出现问题

问题描述:

先生,我在主窗口中创建一些按钮时遇到问题。

创建按钮时我没有错误也没有警告,但我在主窗口看不到我的按钮,

Sir, I have a problem in creating some buttons in my main window.
I have no errors and no warnings in creating a button but I cannot see my button on my main window,

//My code in initializing the instance function [ BOOL  InitInstance(HINSTANCE hInstance, int nCmdShow) ]

HWND button1;

button1 = CreateWindow(L"button", L"Run Timer", WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON | BS_FLAT, 0, 180, 197, 90, button1, (HMENU)IDC_VS_TIME, hInst, NULL);
ShowWindow(button1, nCmdShow);
UpdateWindow(button1);





我尝试过:



由于我在学习,我不确定是否发现了这个bug,

我看到我的主窗口显示所以我试着把它的HWND变量hWnd复制到我的按钮1上这个,





What I have tried:

Since I am learning I am not sure of spotting the bug,
I see my main window displaying so I tried copying its HWND variable hWnd to my button 1 like this,

button1 = CreateWindow(L"button", L"Run Timer", WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON | BS_FLAT, 0, 180, 197, 90, hWnd, (HMENU)IDC_VS_TIME, hInst, NULL);
//changed button1 to hWnd

ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);



我将button1更改为hWnd并显示我的按钮我也比较了两个代码但我找不到任何内容。


I changed button1 to hWnd and my button gets displayed I did also compared both the code but I can't find anything.

第一次调用中的父级是一个错误,因为它是button1而不是父窗口。



The parent in your first call is a bug, because it is the button1 and NOT a parent windows.

HWND button1;
 
button1 = CreateWindow(L"button", L"Run Timer", WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON | BS_FLAT, 0, 180, 197, 90, button1, (HMENU)IDC_VS_TIME, hInst, NULL);





理查德的回答是正确的:您必须在第二个代码显示时提供有效的父窗口。



The answer from Richard is right: You must provide a valid parent window as your second code shows .