English 中文(简体)
设定前景窗口仅在视觉工作室打开时工作
原标题:SetForegroundWindow only working while visual studio is open

我试图用 c# 设置一个进程窗口到前景/ 焦点( 来自于一个在进行时没有焦点的应用程序), 因此我使用 us322. dll < code> static Extern bookl SetForegroundow (IntPtr hWnd) 方法 :

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd); 
public void setFocus()
{
    SetForegroundWindow(process.MainWindowHandle);       
}

每件事都很好, 但只有当我拥有2008年的视觉工作室时, 我甚至不需要从 VS08 开始应用程序, 它足以让工程打开。 当我关闭此项目时, 我的程序无法将另一个窗口设置在前景上。 唯一的结果是在任务栏中, 另一个窗口被突出显示为蓝色。 当我要用 VS08 重新打开我的工程时, 它就运作正常 。

我不知道为什么.我认为问题可能是他无法导入该圆层,但不会被突出显示,还有其他的双点32函数,如static Extern bookl ShowWindow (IntPtr hWnd, IntPtr status);即使在项目关闭时也在起作用。

这个问题有什么解决办法或暗示吗?

Edit: I read the remarks for the function and I had the idea that my application has not the focus, so I tried this one:

[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); 
}        

现在我正在寻找当前焦点的窗口进程, 并为此窗口设置 < code> AllowSetFororegroundWindow , 并试图现在将焦点设置在我的窗口上。 但同样的问题, 我一在 VS 中工程打开它就起作用了, 如果我没有在任务栏中只获得蓝色亮点 。

在我申请运行期间,我可以打开/关闭 vs 工程, 当它打开了一切工作, 当它关闭了它不起作用, 我运行我的申请时没有与 VS 工程互动。 说真的我不明白它。

问题回答

在上网搜索了几天后, 我找到了一个简单可行的解决方案, 让SetForeforloorWindow在7号窗口上工作: 按 Alt 键, 再拨打SetForeforloorWindow 。

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);
    }

以及Wond32api进口品

  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();

我对发送 Alt- key 表示异议, 因为它迫使窗口菜单在我选择输入时打开( 而不是我想要的“ 确定” 按钮 ) 。

这对我管用:

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);
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签