我正试图用c#设置一个进程窗口,指向前台/焦点(来自在执行时没有焦点的应用程序),因此我使用user32.dll static extern bool SetForegroundWindow(IntPtr hWnd)方法:
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);
public void setFocus()
{
SetForegroundWindow(process.MainWindowHandle);
}每件事情都很好,但只有当我打开visual 2008时,我甚至不需要从VS08启动应用程序,就足以打开这个项目了。当我关闭项目时,我的应用程序无法将另一个窗口设置为前台。唯一的结果是任务栏中的另一个窗口是蓝色高亮显示的。当我再次用VS08打开我的项目的时候,它就正常工作了。
我一点也不知道为什么.我认为问题可能是他不能导入dll,但是这样就不会突出显示,甚至当项目关闭时,像static extern bool ShowWindow(IntPtr hWnd, IntPtr status);这样的其他static extern bool ShowWindow(IntPtr hWnd, IntPtr status);函数也能工作。
对于这个问题有什么解决办法或暗示吗?
编辑:,我读了这个函数的备注,我认为我的应用程序没有焦点,所以我尝试了这个:
[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll")]
static extern bool AllowSetForegroundWindow(int procID);
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", SetLastError = true)]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
public void setAUTFocus()
{
IntPtr hWnd = GetForegroundWindow();
uint processID = 0;
uint threadID = GetWindowThreadProcessId(hWnd, out processID);
int intID = (int)processID;
AllowSetForegroundWindow(intID);
SetForegroundWindow(process.MainWindowHandle);
} 现在,我正在搜索当前具有焦点的窗口进程,并为该窗口设置AllowSetForegroundWindow,并尝试将焦点设置在我的窗口上。但是同样的问题,当我在VS中打开项目的时候,它就开始工作了,如果不是的话,我只会在任务栏中得到蓝色的高光。
在我的应用程序运行期间,我可以打开/关闭vs项目,当它打开的时候,一切都在工作,关闭的时候就不能工作了,我在运行应用程序时没有与VS项目的交互。说真的我不明白。
发布于 2012-12-14 15:31:38
在网上搜索了几天之后,我找到了一个简单的解决方案,可以让SetForegroundWindow在windows 7上工作:在调用SetForegroundWindow之前按Alt键。
public static void ActivateWindow(IntPtr mainWindowHandle)
{
//check if already has focus
if (mainWindowHandle == GetForegroundWindow()) return;
//check if window is minimized
if (IsIconic(mainWindowHandle))
{
ShowWindow(mainWindowHandle, Restore);
}
// Simulate a key press
keybd_event((byte)ALT, 0x45, EXTENDEDKEY | 0, 0);
//SetForegroundWindow(mainWindowHandle);
// Simulate a key release
keybd_event((byte)ALT, 0x45, EXTENDEDKEY | KEYUP, 0);
SetForegroundWindow(mainWindowHandle);
}以及win32api的进口
private const int ALT = 0xA4;
private const int EXTENDEDKEY = 0x1;
private const int KEYUP = 0x2;
private const uint Restore = 9;
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool IsIconic(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern int ShowWindow(IntPtr hWnd, uint Msg);
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();发布于 2015-06-01 11:53:45
当我选择enter (而不是我想要的OK按钮)时,我在发送Alt键时遇到了问题,因为它迫使窗口菜单打开。
这对我起了作用:
public static void ActivateWindow(IntPtr mainWindowHandle)
{
//check if already has focus
if (mainWindowHandle == GetForegroundWindow()) return;
//check if window is minimized
if (IsIconic(mainWindowHandle))
{
ShowWindow(mainWindowHandle, Restore);
}
// Simulate a key press
keybd_event(0, 0, 0, 0);
SetForegroundWindow(mainWindowHandle);
}https://stackoverflow.com/questions/10740346
复制相似问题