如何让一个已经打开的系统获得焦点

gengxin_914 2009-04-16 10:03:07
A和B是两个独立的系统,如果这时B系统已经打开,但可能已经最小化了,这时在A系统中如何才能让B系统获得焦点显示到最前面来呢
...全文
122 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
simen_frankly 2009-04-16
  • 打赏
  • 举报
回复
学习了
csrwgs 2009-04-16
  • 打赏
  • 举报
回复
上面贴代码的好恐怖。

用FindWindows()函数获取窗口的Handle。

用GetWindowsLong和SetWindowsLong设置窗口的状态

用SetWindowsPos刷新窗口状态。

楼主请看:
http://hi.baidu.com/csrwgs/blog/item/d0f92912ca7012d8f7039e13.html

http://hi.baidu.com/csrwgs/blog/item/33f88d7204acc3148701b0fc.html
LemIST 2009-04-16
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wf5360308 的回复:]
完整的:

C# code
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;

public class MainForm : System.Windows.Forms.Form
{
private…
[/Quote]
学习
冷月孤峰 2009-04-16
  • 打赏
  • 举报
回复
完整的:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;

public class MainForm : System.Windows.Forms.Form
{
private const int WS_SHOWNORMAL = 1;
[DllImport("User32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);

public MainForm()
{
InitializeComponent();
}
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Process instance = GetRunningInstance();
if(instance == null)
{
Application.Run(new MainForm());
}
else
{
HandleRunningInstance(instance);
}
}
/// <summary>
/// 获取应用程序的实例,没有其它的例程,返回Null
/// </summary>
/// <returns></returns>
public static Process GetRunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName (current.ProcessName);
//遍历正在有相同名字运行的例程
foreach (Process process in processes)
{
//忽略现有的例程
if (process.Id != current.Id)
//确保例程从EXE文件运行
if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
//返回另一个例程实例
return process;
}
return null;
}
/// <summary>
/// 获取窗口句柄
/// </summary>
/// <param name="instance"></param>
public static void HandleRunningInstance(Process instance)
{
//确保窗口没有被最小化或最大化
ShowWindowAsync (instance.MainWindowHandle , WS_SHOWNORMAL);
//设置真实例程为foreground window
SetForegroundWindow (instance.MainWindowHandle);
}
}
zgke 2009-04-16
  • 打赏
  • 举报
回复
先要获取你想要窗体的句柄

你可以使用

[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

然后使用

[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
让窗体最大化

[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern bool SetForegroundWindow(IntPtr hWnd);
记活窗体..

[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SetFocus(IntPtr hWnd);
设置焦点
cuike519 2009-04-16
  • 打赏
  • 举报
回复
This function brings the specified window to the top of the z-order. If the window is a top-level window, it is activated. If the window is a child window, the top-level parent window associated with the child window is activated.

BOOL BringWindowToTop(
HWND hWnd
);

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧