如何使用C#获取和设置另一个应用程序的位置?
例如,我想得到记事本的左上角坐标(让我们说它漂浮在100,400处),此窗口的位置为0,0.
实现这一目标的最简单方法是什么?
我无法解决这个问题.我收到一个错误:
The name 'hWnd' does not exist in the current context
Run Code Online (Sandbox Code Playgroud)
这听起来很容易,可能是...对于提出如此明显的问题感到抱歉.
这是我的代码:
public static IntPtr WinGetHandle(string wName)
{
foreach (Process pList in Process.GetProcesses())
{
if (pList.MainWindowTitle.Contains(wName))
{
IntPtr hWnd = pList.MainWindowHandle;
}
}
return hWnd;
}
Run Code Online (Sandbox Code Playgroud)
我尝试了许多不同的方法,每个都失败了.提前致谢.
IntPtr handle = process.MainWindowHandle;
if (handle != IntPtr.Zero)
{
SetWindowPos(handle, 0, 0, 0, 0, 0, SWP_NOZORDER | SWP_NOSIZE | SWP_SHOWWINDOW);
}
Run Code Online (Sandbox Code Playgroud)
然后,当我在构造函数中调用例如SetWindowPos时,应该给它什么?处理很好,我知道应该是什么。但是所有的resr 0,0,0,0,0,0以及SWP_NOZORDER和SWP_NOSIZE的值应该是多少?
我要做的是将手柄置于屏幕的正面和中央。把它放到最前面,我知道我正在使用的方法SetForegroundWindow(IntPtr hWnd);,并且工作正常。但是,如何使用SetWindowPos强制将其置于屏幕中央?