为什么设置外部应用程序的位置只能与断点一起正常工作?

时间:2018-01-17 07:25:49

标签: c#

我正在尝试重新定位外部应用程序的位置,即屏幕键盘。只有放置断点才能正常工作。如果我从没有断点的VS或通过运行可执行文件运行它,它将无法运行。为什么呢?

[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("user32.dll", SetLastError = true)]
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);

const uint SWP_NOSIZE = 0x0001;

public void SetWindowPosition(string txtAppTitle, string txtWidth, string txtHeight, string txtX, string txtY)
{
    IntPtr hWnd = FindWindow(null, txtAppTitle);

    // If found, position it.
    if (hWnd != IntPtr.Zero)
    {
        // Move the window to (txtX, txtY) without changing its size.
        int x = int.Parse(txtX);
        int y = int.Parse(txtY);
        SetWindowPos(hWnd, IntPtr.Zero, x, y, 0, 0, SWP_NOSIZE);
    }
}

private void btnOnScreenKeyboard_Click(object sender, EventArgs e)
{
    SetWindowPosition("On-Screen Keyboard", "0", "0", "155", "545");
}

0 个答案:

没有答案