Jam*_*mes 23 c# window window-position
如何使用C#获取和设置另一个应用程序的位置?
例如,我想得到记事本的左上角坐标(让我们说它漂浮在100,400处),此窗口的位置为0,0.
实现这一目标的最简单方法是什么?
大卫的有用答案提供了关键的指针和有用的链接.
为了把它们实现在问题的示例场景一个自包含的例子使用,使用通过P/Invoke的(Windows的API System.Windows.Forms是不涉及):
using System;
using System.Runtime.InteropServices; // For the P/Invoke signatures.
public static class PositionWindowDemo
{
// P/Invoke declarations.
[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;
const uint SWP_NOZORDER = 0x0004;
public static void Main()
{
// Find (the first-in-Z-order) Notepad window.
IntPtr hWnd = FindWindow("Notepad", null);
// If found, position it.
if (hWnd != IntPtr.Zero)
{
// Move the window to (0,0) without changing its size or position
// in the Z order.
SetWindowPos(hWnd, IntPtr.Zero, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
34513 次 |
| 最近记录: |