How to maximize a window in background?

前端 未结 3 569
天命终不由人
天命终不由人 2021-01-26 11:05

I need to maximize a window in background, meaning without activating (focusing) it. SetWindowPlacement function doesn\'t offer this.. Any ideas?

        WINDOWP         


        
3条回答
  •  [愿得一人]
    2021-01-26 12:05

    Try this using definitions from http://www.pinvoke.net:

    WINDOWPLACEMENT placement;
    if (GetWindowPlacement(hWnd, out placement))
    {
        if ((GetWindowLong(hWnd, GWL_EXSTYLE) & WS_EX_TOOLWINDOW) == 0)
        {
            var l = GetWindowLong(hWnd, GWL_STYLE);
            SetWindowLong(hWnd, GWL_STYLE, (l | WS_MAXIMIZE) & (~WS_MINIMIZE));
            var maxPos = placement.MaxPosition;
            SetWindowPos(hWnd, IntPtr.Zero, maxPos.X, maxPos.Y, 0, 0, SetWindowPosFlags.AsynchronousWindowPosition | SetWindowPosFlags.DoNotActivate | SetWindowPosFlags.FrameChanged | SetWindowPosFlags.IgnoreResize | SetWindowPosFlags.IgnoreZOrder);
        }
    }
    

    The trick is to change the window state with SetWindowLong and redraw it with SetWindowPosFlags.FrameChanged. And in your case with SetWindowPosFlags.DoNotActivate.

提交回复
热议问题