【发布时间】:2014-10-24 01:22:43
【问题描述】:
我想从我的 c# 应用程序中集中一个程序。我搜索了很多并找到了一些示例。但是我得到了错误。我正在使用 Visual Studio。ShowWindow(hWnd, SW_HIDE); 行给了我一个错误"showwindow(system.IntPtr,int) has some invalid argument"
请问这段代码的问题在哪里
[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
}
}
}
【问题讨论】:
-
你为什么打电话给
ToInt32()?MainWindowHandle已经为您提供了正确类型的值。类型转换是您收到错误的原因:它是不兼容的类型。