在 Windows 中隐藏控件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6275718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 17:01:02  来源:igfitidea点击:

Hiding a control in Windows

windowswinapicontrolshideshowwindow

提问by stelonix

I can't figure out how to hide a child window (a control), more specifically a GroupBox and a PushButton. I thought ShowWindow()with SW_HIDEas the second parameter would do the job, but it simply doesn't work. Yet SW_SHOWworks just fine. I have the correct window handle for both controls, so that's not the issue.

我不知道如何隐藏子窗口(控件),更具体地说是 GroupBox 和 PushButton。我认为ShowWindow()SW_HIDE作为第二个参数可以完成这项工作,但它根本不起作用。然而SW_SHOW工作得很好。我有两个控件的正确窗口句柄,所以这不是问题。

I googled and all I could find was people asking how to hide dialogs, not controls. Either that or MFC-based applications, which doesn't apply here. I'm using pure Windows API, no MFC.

我用谷歌搜索,我能找到的只是人们问如何隐藏对话框,而不是控件。要么是那个,要么是基于 MFC 的应用程序,这里不适用。我使用的是纯 Windows API,没有 MFC

What am I getting wrong?

我怎么了?

EDIT: More info: I'm writing some simple class wrappers for WinApi controls. The WindowsControl class has, along other methods, the following methods for showing and hiding the Control:

编辑:更多信息:我正在为 WinApi 控件编写一些简单的类包装器。WindowsControl 类与其他方法一起,具有以下用于显示和隐藏控件的方法:

void Show() {
    ShowWindow(this->_hWnd,SW_SHOWNOACTIVATE);
}

void Hide() {
    ShowWindow(this->_hWnd,SW_HIDE);
}

Every control inherits from WindowsControl.

每个控件都继承自 WindowsControl。

This image has the window layout so you understand exactly what I'm doing: http://i.stack.imgur.com/PHQnH.png

此图像具有窗口布局,因此您完全了解我在做什么:http: //i.stack.imgur.com/PHQnH.png

When the user clicks inside the "Chipset" Static control, it'll load information for a given Tile (which is stored in an array, but that's irrelevant). Depending on the setting, it'll hide the "Edit bitwall" button on the left and show the empty GroupBox behind it or viceversa. Just to be clear this isn't something wrong with my windows api wrappers, I am getting the correct HWND. Though ShowWindow might not be able to be called from a Window Procedure that isn't the parent's (that'd be weird).

当用户在“Chipset”静态控件内部单击时,它将加载给定 Tile 的信息(存储在数组中,但这无关紧要)。根据设置,它会隐藏左侧的“编辑位墙”按钮并在其后面显示空的 GroupBox,反之亦然。只是要清楚这不是我的windows api包装器有问题,我得到了正确的HWND。尽管 ShowWindow 可能无法从不是父级的窗口过程调用(这很奇怪)。

EDIT2: Using C++ with Visual Studio 2008, no MFC, no WTL, no CLR, no .NET

EDIT2:在 Visual Studio 2008 中使用 C++,没有 MFC,没有 WTL,没有 CLR,没有 .NET

EDIT3: I'll post even more code so it's easier

EDIT3:我会发布更多代码,这样更容易

Inside the static's window procedure, I handle WN_LBUTTONDOWN like this:

在静态窗口过程中,我像这样处理 WN_LBUTTONDOWN:

case WM_LBUTTONDOWN: {
  ...
  update_tiledata(c, l)


void update_tiledata(GroupBox * c, ListView* l ) {
    ...

   if (chp_copy.Tiles[selectedTile].Pass() == PT_BITWALL) {
          c->Controls(CTL_BTNEDITBIT)->Show();
          c->Controls(CTL_FRPHOLD)->Hide();
   } else {

          c->Controls(CTL_FRPHOLD)->Show();
          c->Controls(CTL_BTNEDITBIT)->Hide();
   }
   update_edits();
}

The ommited code does nothing to affect the classes, as I said before, ShowWindow with SW_HIDE IS getting called, with the correct HWND, but nothing is happening.

省略的代码对类没有任何影响,正如我之前所说,使用正确的 HWND 调用带有 SW_HIDE 的 ShowWindow,但什么也没发生。

回答by RED SOFT ADAIR

A control in a Window or dialog can be hidden using

可以使用隐藏窗口或对话框中的控件

ShowWindow(hControlWin, SW_HIDE);

ShowWindow(hControlWin, SW_HIDE);

In a dialog you can retrive the controls window handle by calling

在对话框中,您可以通过调用来检索控件窗口句柄

GetDlgItem(hDlg, < CtrlID >);

GetDlgItem(hDlg, < CtrlID >);

Typically you write something like:

通常你会写这样的东西:

ShowWindow(GetDlgItem(hDlg, 2), SW_HIDE);

ShowWindow(GetDlgItem(hDlg, 2), SW_HIDE);

It would be helpful if you give more information and some code: How did you create the controls? What language, compile and framework did you use?

如果您提供更多信息和一些代码,将会有所帮助:您是如何创建控件的?你用的是什么语言、编译和框架?

回答by Matt Wilko

I think the function call you want is EnableWindowI have used this before to disable a button on a form. You will need to get an handle to the Window (object) first though so you might want to use EnumChildWindowsto iterate through all the controls to find the one you want.

我认为您想要的函数调用是EnableWindow我之前使用过它来禁用表单上的按钮。不过,您首先需要获得 Window(对象)的句柄,因此您可能希望使用EnumChildWindows遍历所有控件以找到所需的控件。