WinAPI MoveWindow 功能不适用于某些窗口

v.c*_*jen 4 c# pinvoke winapi user32 winforms-interop

我想从我的应用程序调整大小和/或移动一些外部窗口,主要是屏幕键盘窗口。这是代码:

     [DllImport("user32.dll", SetLastError = true)]
    internal static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);

    [DllImport("user32.dll", EntryPoint = "SetWindowPos")]
    public static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);


    //assorted constants needed
    public static uint MF_BYPOSITION = 0x400;
    public static uint MF_REMOVE = 0x1000;
    public static int GWL_STYLE = -16;
    public static int WS_CHILD = 0x40000000; //child window
    public static int WS_BORDER = 0x00800000; //window with border
    public static int WS_DLGFRAME = 0x00400000; //window with double border but no title
    public static int WS_CAPTION = WS_BORDER | WS_DLGFRAME; //window with a title bar 
    public static int WS_SYSMENU = 0x00080000; //window menu  

    public const byte KEYEVENTF_EXTENDEDKEY = 0x0001; //Key down flag
    public const byte KEYEVENTF_KEYUP = 0x0002; //Key up flag
    public const byte VK_RCONTROL = 0xA3; //Top Control key code
    public const byte VK_CONTROL = 0x80; //Left Control key code

    const short SWP_NOMOVE = 0X2;
    const short SWP_NOSIZE = 1;
    const short SWP_NOZORDER = 0X4;
    const int SWP_SHOWWINDOW = 0x0040;

    #endregion

    public static void WindowsReStyle()
    {

        Process p = Process.GetProcesses().Where(d => d.ProcessName.Contains("osk")).DefaultIfEmpty(null).FirstOrDefault();

        IntPtr hWndOSK = p.MainWindowHandle;

        string title = p != null ? p.MainWindowTitle : "";

        bool b = MoveWindow(hWndOSK, 600, 600, 600, 600, true);

        int i = System.Runtime.InteropServices.Marshal.GetLastWin32Error();

        SetWindowPos(hWndOSK, 0, 0, 0, 0, 0, SWP_NOZORDER | SWP_NOSIZE | SWP_SHOWWINDOW);

        i = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
    }
Run Code Online (Sandbox Code Playgroud)

但问题是正确找到了 IntPtr 句柄,但窗口既没有移动也没有调整大小。我尝试了 bot MoveWindow 和 SetWindowPos 函数,但它们不起作用。

GetLastWin32Error() 
Run Code Online (Sandbox Code Playgroud)

函数有时返回代码

1400 (wrong hanlde), 
Run Code Online (Sandbox Code Playgroud)

有时

5 (Access denied).
Run Code Online (Sandbox Code Playgroud)

我该如何解决?

Dav*_*nan 6

屏幕键盘以高完整性级别运行。这意味着您还需要您的流程以该完整性级别运行。首先要做的是以管理员身份执行您的程序。这将以高完整性级别运行您的流程。

当然,您不太可能发现运行您的应用程序的前景提升,因此向用户展示 UAC 对话框非常有吸引力。但这只是系统设计的结果。

请注意,您的错误处理不正确。仅在 API 功能失败时检查错误代码。这意味着检查函数的返回值。你的 p/invoke forSetWindowPos是有缺陷的。设置失败SetLastError。这些细节确实很重要,您必须非常小心。编译器无法检查互操作代码的正确性。