How does one hide a win32 app window?

2019-01-26 12:28发布

I want to run the app in silent mode by passing in a parameter, otherwise I will show the window.

标签: c++ mfc winapi
5条回答
做自己的国王
2楼-- · 2019-01-26 12:53

Well, for one you could just decide not to create a window at all if this parameter is passed in, otherwise you can try calling ShowWindow, with the handle to your window and with the SW_HIDE parameter, and see if that does what you need.

Another way of hiding the window and never having it show up, but still create it, is to chose to never call ShowWindow with SW_HIDE on it, and create it with CreateWindow/CreateWindowEx, and not set the WS_VISIBLE flag in the dwStyle parameter.

查看更多
聊天终结者
3楼-- · 2019-01-26 12:55

ShowWindow(... SW_HIDE ...) doesn't work?

The best practice here is to not create the window in the first place. Nothing forces you to actually create a window in InitInstance. Though if you're working with MFC it's likely a lot of your application/domain/business logic is sitting there, tightly coupled to those MFC message handlers and so forth. In which case the window will need to exist.

查看更多
Ridiculous、
4楼-- · 2019-01-26 13:07

I think a better solution will be not creating the window if not needed. Take a look at the main function and you will see the code that creates the window. Call it only if you want to launch the window.

查看更多
闹够了就滚
5楼-- · 2019-01-26 13:12

create the window, omit the WS_VISIBLE flag and don't call ShowWindow.

or

When you call showWindow(), add SW_HIDE parameter.

ShowWindow(hWnd, SW_HIDE);
查看更多
干净又极端
6楼-- · 2019-01-26 13:13

If you have an MFC CWnd based display then CWnd::ShowWindow(SW_HIDE);
If you are using just win32 then ShowWindow(hWnd, SW_HIDE);

Other things people do depending on your goals

  • make the window very small
  • move the window off the visible desktop area
查看更多
登录 后发表回答