c正在运行的程序的焦点窗口
本文关键字:程序 窗口 焦点 运行 | 更新日期: 2023-09-27 18:20:06
我想从我的c#应用程序中集中一个程序。我搜索了很多,找到了一些示例。但我遇到了错误。我使用的是visual studio。ShowWindow(hWnd, SW_HIDE);
行给我一个错误"showwindow(system.IntPtr,int) has some invalid argument"
plz这个代码的问题在哪里
[DllImport("user32.dll")]
internal static extern IntPtr SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
internal static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
private void FocusProcess()
{
int hWnd;
Process[] processRunning = Process.GetProcesses();
foreach (Process pr in processRunning)
{
if (pr.ProcessName == "notepad")
{
hWnd = pr.MainWindowHandle.ToInt32();
ShowWindow(hWnd, 3);//error line
}
}
}
您将hWnd声明为int。但是ShowWindow函数需要IntPtr。因为pr.MainWindowHandle是IntPtr,所以只需要将其用作hWnd。顺便说一句,如果你想把这个窗口作为最上面的,你应该调用SetForegroundWindow。
[DllImport("user32.dll")]
internal static extern IntPtr SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
internal static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); //ShowWindow needs an IntPtr
private static void FocusProcess()
{
IntPtr hWnd; //change this to IntPtr
Process[] processRunning = Process.GetProcesses();
foreach (Process pr in processRunning)
{
if (pr.ProcessName == "notepad")
{
hWnd = pr.MainWindowHandle; //use it as IntPtr not int
ShowWindow(hWnd, 3);
SetForegroundWindow(hWnd); //set to topmost
}
}
}