关于C#:显示/隐藏控件后如何更新窗口的当前状态?

How to update the current state for window after showing/hiding controls?

我是一个初学者,今天是学习创建Windows应用程序的第一天。
我有两个按钮。

1
2
3
4
5
6
#define BUTTON_SW 1
#define BUTTON_SW2 2
HWND Button1;
HWND Button2;
Button1 = CreateWindow("button","Enter", WS_VISIBLE | WS_CHILD, 215, 10, 40, 25, hwnd, HMENU(BUTTON_SW), NULL, NULL);  
Button2 = CreateWindow("button","You'll be gone", WS_VISIBLE | WS_CHILD, 260, 10, 95, 25, hwnd, HMENU(BUTTON_SW2), NULL, NULL);

单击Button1时,如何隐藏Button2或使其失去其WS_VISIBLE标志并像这样正确反映当前情况?

1
2
3
LONG style = GetWindowLong(Button2,GWL_STYLE);
style = style | WS_VISIBLE; // style = style & ~WS_VISIBLE
SetWindowLong(Button2,GWL_STYLE,style);

这很好。但是,一旦分配了WS_VISIBLE标志,在第一次鼠标单击它之前,按钮仍然保持不可见。
反之亦然,当我删除WS_VISIBLE标志后使用style = style & ~WS_VISIBLE;时,按钮变为被动(不可单击)但保持可见。

该如何解决?尝试了许多我在网上找到的东西,但无法解决。另外,请不要建议我买一本像样的书,因为我暂时没有钱。

(PS:ShowWindowSW_HIDE/SW_SHOW的功能对我不起作用,也许我使用的是错误的。您能帮我如何正确隐藏此Button2吗?我正在尝试以下命令,但没有任何反应。)

1
ShowWindow(GetDlgItem(Button2, 2), SW_HIDE);

@Edit: I've noticed that when I minimize the app and maximize it
again, the state of button updates. But how will it automatically
update the state?


这应该工作

1
ShowWindow(Button2, SW_HIDE);

1
ShowWindow(GetDlgItem(DialogHWND, BUTTON_SW2), SW_HIDE);

GetDlgItem需要父窗口(dialog)的HWND作为第一个参数。


为了在窗口中反映更改,您必须要求操作系统执行该操作。

了解RedrawWindow和无效区域。

请注意,某些操作(例如调整大小或从最小化还原)会自动使操作系统使区域无效并重新绘制区域。

使用:

1
RedrawWindow(Button2,NULL,NULL,RDW_INVALIDATE | RDW_INTERNALPAINT);