How to maximize a window in background?

前端 未结 3 570
天命终不由人
天命终不由人 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 11:54

    Please use the below code at the time of windows load

        private void Form1_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
    
        }
    

    This will maximise your window

    0 讨论(0)
  • 2021-01-26 11:59

    The best way I could find to do this was (VB sorry!);

    longForeHWnd = GetForegroundWindow
    Call ShowWindow(longBackHWnd, SW_SHOWMAXIMIZED)
    SetForegroundWindow (longForeHWnd)
    

    This is only a work-around because the Background Window is (briefly) activated and so gets promoted up the Z-Order

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题