问题描述
(问题在底部)
我正在尝试编写一个只有一个按钮的程序.如果按下这个按钮,它应该创建一个新按钮,并删除另一个,你会在这里看到:
I am trying to write a program, which has one button. If this button is pressed, it should create a new button, and delete the other one, you will see here:
constexpr unsigned int button1=111;
constexpr unsigned int button2=112;
bool buttonIsPressed = false;
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static RECT rect;
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_CREATE:
{
GetClientRect(hWnd, &rect);
CreateWindowEx(NULL,
L"BUTTON",
L"create new button",
WS_TABSTOP | WS_VISIBLE |
WS_CHILD | BS_DEFPUSHBUTTON,
/*windowWidth-15,*/
400,
rect.bottom - 40,
100,
28,
hWnd,
(HMENU)button1,
GetModuleHandle(NULL),
NULL);
if (buttonIsPressed == true)
{
CreateWindowEx(NULL,
L"BUTTON",
L"Text of the button",
WS_TABSTOP | WS_VISIBLE |
WS_CHILD | BS_DEFPUSHBUTTON,
/*windowWidth-15,*/
400,
rect.bottom - 40,
100,
28,
hWnd,
(HMENU)button2,
GetModuleHandle(NULL),
NULL);
}
}
case WM_PAINT:
{
return 0;
}
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
case WM_COMMAND:
{
switch (LOWORD(wParam))
{
case button1:
{
buttonIsPressed = true;
break;
}
}
break;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
我也尝试使用 UpdateWindow
和 InvalidateRect
.
I also tried to use UpdateWindow
and InvalidateRect
.
问题 1:如何在按下另一个按钮时创建一个新按钮?
Question 1: How can i create a new button when the other button is pressed?
问题 2: 我怎样才能停止绘制另一个按钮.如果我使用 InvalidateRect,按钮会变得不可见,但只要我按下窗口,它就会再次可见.
Question 2: How can i stop drawing the other button. If I use InvalidateRect, the button becomes invisible, but as soon as i press on the window, it is visible again.
感谢您的关注和您的时间.
推荐答案
你已经接近了.您已经知道创建单个按钮 (CreateWindowEx()
).在您显示的代码中,您只需将 WM_CREATE
处理程序中的第二个 CreateWindowEx()
移动到 WM_COMMAND
处理程序中,确保检查报告的 ID/HWND 以确保它是您感兴趣的第一个按钮,然后您可以根据需要 ShowWindow()
/DestroyWindow()
该按钮并创建新按钮,例如:
You are close. You already know to create individual buttons (CreateWindowEx()
). In the code you have shown, you just need to move the 2nd CreateWindowEx()
from the WM_CREATE
handler into the WM_COMMAND
handler, being sure to check the reported ID/HWND to make sure it is the 1st button you are interested in, and then you can ShowWindow()
/DestroyWindow()
that button as needed and create the new button, eg:
constexpr unsigned int button1=111;
constexpr unsigned int button2=112;
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static RECT rect;
//...
switch (message)
{
case WM_CREATE:
{
GetClientRect(hWnd, &rect);
CreateWindowEx(NULL,
L"BUTTON",
L"create new button",
WS_TABSTOP | WS_VISIBLE |
WS_CHILD | BS_DEFPUSHBUTTON,
/*windowWidth-15,*/
400,
rect.bottom - 40,
100,
28,
hWnd,
(HMENU)button1,
GetModuleHandle(NULL),
NULL);
break;
}
//...
case WM_COMMAND:
{
if (HIWORD(wParam) == BN_CLICKED)
{
switch (LOWORD(wParam))
{
case button1:
{
HWND hwndBtn = (HWND)lParam;
// use ShowWindow(hwndBtn, SW_HIDE) or DestroyWindow(hwndBtn) as needed...
GetClientRect(hWnd, &rect);
CreateWindowEx(NULL,
L"BUTTON",
L"Text of the button",
WS_TABSTOP | WS_VISIBLE |
WS_CHILD | BS_DEFPUSHBUTTON,
/*windowWidth-15,*/
400,
rect.bottom - 40,
100,
28,
hWnd,
(HMENU)button2,
GetModuleHandle(NULL),
NULL);
break;
}
case button2:
{
//...
break;
}
}
return 0;
}
break;
}
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
这篇关于如何在按下另一个按钮时创建一个按钮并隐藏另一个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!