开发者

Creating a minimized overlapped window (Win32)

I'd like 开发者_Go百科to create an overlappedwindow that starts out visible (so the taskbar button shows) but minimized. Creating the window with WS_MINIMZED (or WS_MAXIMIZE for that matter) does nothing. Using ShowWindow(hWnd,SW_SHOWMINIMIZED) gives a critical error. I suspect it has something to do with STARTUPINFO but I can't find any info on how to adjust/change/hijack it.

hWnd = CreateWindowA(
    (LPCSTR)atom, 
    "Window Name", 
    WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_MINIMZED,    // doesn't work
    CW_USEDEFAULT, 
    CW_USEDEFAULT, 
    CW_USEDEFAULT, 
    CW_USEDEFAULT, 
    0, 
    0, 
    hInstance, 
    0);

ShowWindow(hWnd,SW_SHOWMINIMIZED);    // gives critical error


@Kaisha, you are right: if you start an executable using CreateProcess, then the visibility of the window will be influenced by STARTUPINFO. To start an application with its window minimized, do this:

ZeroMemory(&startupInfo, sizeof(startupInfo));
startupInfo.cb = sizeof(startupInfo);
startupInfo.dwFlags = STARTF_USESHOWWINDOW;
startupInfo.wShowWindow = SW_SHOWMINNOACTIVE;

I used this approach in an application which repeatedly started the command-line version of WinZip, and it worked fine.


Citing the MSDN, function ShowWindow, parameter nCmdShow:

Controls how the window is to be shown. This parameter is ignored the first time an application calls ShowWindow, if the program that launched the application provides a STARTUPINFO structure.

So I'm guessing that your window is the first window created by the application, and as such, the parameter of ShowWindow is ignored.

Another little known fact is seen in the docs for CreateWindow, parameter y (yes, y):

If an overlapped window is created with the WS_VISIBLE style bit set and the x parameter is set to CW_USEDEFAULT, then the y parameter determines how the window is shown. If the y parameter is CW_USEDEFAULT, then the window manager calls ShowWindow with the SW_SHOW flag after the window has been created. If the y parameter is some other value, then the window manager calls ShowWindow with that value as the nCmdShow parameter.

Probably it is better if you create the window hidden (without WS_VISIBLE) and/or passing 0 as the y parameter of CreateWindow.

Other options would be to create a dummy window first, show it, and destroy it quickly, some like a splash-screen. That would consume the STARTUPINFO command.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜