问题描述
大家好,
我想把窗口放在c#中其他窗口的顶部。
I want to bring window at the top of other windows in c#.
我正在使用以下代码来做这个---
I m using following code to do this---
代码从这里开始----------------------------- --------------------------------------------------
Code starts here-------------------------------------------------------------------------------
    [DllImport(" User32.dll")]
[DllImport("User32.dll")]
    public static extern Int32 SetForegroundWindow(IntPtr hWnd);
public static extern Int32 SetForegroundWindow(IntPtr hWnd);
    [DllImport(" user32.dll")]
[DllImport("user32.dll")]
    public static extern IntPtr FindWindow(string lpClassName,string lpWindowName);
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
private void BringToFront(string className, string CaptionName)
private void BringToFront(string className, string CaptionName)
    {
{
        SetForegroundWindow(FindWindow(className,CaptionName));
SetForegroundWindow(FindWindow(className, CaptionName));
    }
}
private void Form1_Load(object sender,EventArgs e)
private void Form1_Load(object sender, EventArgs e)
       {
{
推荐答案
一般来说,你的方向是正确的。但是,有时您的解决方案无法正常工作。
In general, you are in right direction. However, sometimes your solution cannot work.
因此,您需要先检查 FindWindow("IEFrame","我的应用程序 - Windows Internet Explorer")返回的 句柄。 )为null或您预期的窗口句柄。
So you need to first check if the handle returned by FindWindow("IEFrame", "My Application - Windows Internet Explorer") is null or your expected window handle.
Spy ++ 适合查找和找到您期望的窗口句柄(窗口标题是"我的应用程序 - Windows Internet资源管理器" ;?)。此工具随Visual Studio一起提供。
Spy++ is suitable to find and locate your expected window handle (window caption is "My Application - Windows Internet Explorer"?). This tool is shipped with Visual Studio.
其次,您可以调用GetForegroundWindow()函数来识别实际的前景窗口。它 可能可以帮助您理解为什么您的预期窗口不在前台。
Second, you can call GetForegroundWindow() function to identify actual foreground window. It probably can help you understand why your expected window is not in foreground.
否则,您可以访问链接
http://social.msdn.microsoft.com/Forums/en-US/winforms/ thread / 58f8f2f2-3bac-4cd2-ba2e-ef068f31f6ec / 了解类似案例的解决方法。
Otherwise, you can visit the link http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/58f8f2f2-3bac-4cd2-ba2e-ef068f31f6ec/ to learn the workaround for similar case.
同时,您可以了解有关SetForegroundWindow()函数的更多详细信息 ;通过
。
Meanwhile, you can learn more details about SetForegroundWindow() function viahttp://msdn.microsoft.com/en-us/library/ms633539(VS.85).aspx.
这篇关于在c#中将窗口置于顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!