How can I resize the desktop work area using the SPI_SETWORKAREA flag?

浪尽此生 提交于 2019-12-28 12:44:22

问题


I have been trying for quite a while now to resize the desktop work area (the area where windows get maximized). I have found the required API, but I can't seem to resize the work area. It does just nothing.

I use Windows 7 Ultimate x64 so I also tried compiling it in x64 'mode', and still no luck. Could someone give me a push in the right direction?

Here is what I got so far:

[DllImport("user32.dll", EntryPoint = "SystemParametersInfoA")]
private static extern Int32 SystemParametersInfo(Int32 uAction, Int32 uParam, IntPtr lpvParam, Int32 fuWinIni);

private const Int32 SPIF_SENDWININICHANGE = 2;
private const Int32 SPIF_UPDATEINIFILE = 1;
private const Int32 SPIF_change = SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE;
private const Int32 SPI_SETWORKAREA = 47;
private const Int32 SPI_GETWORKAREA = 48;

public struct RECT
{
    public Int32 Left;
    public Int32 Right;
    public Int32 Top;
    public Int32 Bottom;
}

private static int SetWorkspace(RECT oRECT)
{
    IntPtr ptr = IntPtr.Zero;
    ptr = Marshal.AllocHGlobal(Marshal.SizeOf(oRECT));
    Marshal.StructureToPtr(oRECT, ptr, true);
    return SystemParametersInfo(SPI_SETWORKAREA, Marshal.SizeOf(oRECT), ptr, SPIF_change);
}

回答1:


The following code should work just fine:

// 1. Change the function to call the Unicode variant, where applicable.
// 2. Ask the marshaller to alert you to any errors that occur.
// 3. Change the parameter types to make marshaling easier. 
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SystemParametersInfo(
                                                int uiAction,
                                                int uiParam,
                                                ref RECT pvParam,
                                                int fWinIni);

private const Int32 SPIF_SENDWININICHANGE = 2;
private const Int32 SPIF_UPDATEINIFILE = 1;
private const Int32 SPIF_change = SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE;
private const Int32 SPI_SETWORKAREA = 47;
private const Int32 SPI_GETWORKAREA = 48;

[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
    public Int32 Left;
    public Int32 Top;   // top is before right in the native struct
    public Int32 Right;
    public Int32 Bottom;
}

private static bool SetWorkspace(RECT rect)
{
   // Since you've declared the P/Invoke function correctly, you don't need to
   // do the marshaling yourself manually. The .NET FW will take care of it.

   bool result = SystemParametersInfo(SPI_SETWORKAREA,
                                      IntPtr.Zero,
                                      ref RECT,
                                      SPIF_change);
   if (!result)
   {
       // Find out the error code
       MessageBox.Show("The last error was: " +
                       Marshal.GetLastWin32Error().ToString());
   }

   return result;
}

But I'm not really sure what you're trying to do. By default, the work area is the portion of the screen not obscured by the system taskbar or by application desktop toolbars. You're not going to be able to make it any larger than the area that's available on your screen (neat trick if you could, though!). Do your windows not already fill the entire screen when you maximize them?

And even on machines with multiple monitors, you can't set the working area to span multiple monitors. The MSDN documentation says that it's constrained to setting the working area of the monitor that contains the specified rectangle:

In a system with multiple display monitors, the function sets the work area of the monitor that contains the specified rectangle.



来源:https://stackoverflow.com/questions/6267206/how-can-i-resize-the-desktop-work-area-using-the-spi-setworkarea-flag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!