这里,我可以实现程序只运行一次,但是没有办法做到再运行选中第一个运行界面,我想通过用Findwindow这个api函数,但是却没有办法获取第一次运行的窗体的句柄,请大家帮帮忙谢谢了
这是我的代码,我是在VS2005下来写的
sing System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;namespace test_csharp
{
public partial class Form1 : Form
{
[DllImport("User32.dll")]
public static extern int MessageBox(int h, string m, string c, int type);
[DllImport("User32.dll")]
public static extern IntPtr FindWindow(string classname,string windowname);
[DllImport("User32.dll")]
public static extern IntPtr GetForegroundWindow();
[DllImport("User32.dll")]
public static extern long SetForegroundWindow(IntPtr hwnd);
[DllImport("User32.dll")]
public static extern long GetWindowText(IntPtr hwnd,string lpString, long cch);
//private string windowtitle="";
public IntPtr hwnd;
public IntPtr form1hwnd;
public long state;
//private long abc=10;
public Form1()
{
//hwnd = FindWindow("Form1", "Form1");
//if (hwnd > 0)
// MessageBox(0, "这个程序以运行,句柄为" + hwnd.ToString(), "消息框", 0); InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
hwnd = this.Handle;
label2.Text = hwnd.ToString();
} private void label2_Click(object sender, EventArgs e)
{ label1.Text=GetForegroundWindow().ToString();
} private void button1_Click(object sender, EventArgs e)
{
form1hwnd = FindWindow("Form1","window");
Form nf = new Form();
nf.Show();
nf.Click += new System.EventHandler(nf_Click);
nf.Owner = this;
}
private void nf_Click(object sender, EventArgs e)
{
this.label1.Text = GetForegroundWindow().ToString();
MessageBox(0,GetForegroundWindow().ToString(),"",0);
state =SetForegroundWindow(form1hwnd);
}
}
}真不明白为什么我的findwindow怎么取也取不到值每次都是0
这是我的代码,我是在VS2005下来写的
sing System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;namespace test_csharp
{
public partial class Form1 : Form
{
[DllImport("User32.dll")]
public static extern int MessageBox(int h, string m, string c, int type);
[DllImport("User32.dll")]
public static extern IntPtr FindWindow(string classname,string windowname);
[DllImport("User32.dll")]
public static extern IntPtr GetForegroundWindow();
[DllImport("User32.dll")]
public static extern long SetForegroundWindow(IntPtr hwnd);
[DllImport("User32.dll")]
public static extern long GetWindowText(IntPtr hwnd,string lpString, long cch);
//private string windowtitle="";
public IntPtr hwnd;
public IntPtr form1hwnd;
public long state;
//private long abc=10;
public Form1()
{
//hwnd = FindWindow("Form1", "Form1");
//if (hwnd > 0)
// MessageBox(0, "这个程序以运行,句柄为" + hwnd.ToString(), "消息框", 0); InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
hwnd = this.Handle;
label2.Text = hwnd.ToString();
} private void label2_Click(object sender, EventArgs e)
{ label1.Text=GetForegroundWindow().ToString();
} private void button1_Click(object sender, EventArgs e)
{
form1hwnd = FindWindow("Form1","window");
Form nf = new Form();
nf.Show();
nf.Click += new System.EventHandler(nf_Click);
nf.Owner = this;
}
private void nf_Click(object sender, EventArgs e)
{
this.label1.Text = GetForegroundWindow().ToString();
MessageBox(0,GetForegroundWindow().ToString(),"",0);
state =SetForegroundWindow(form1hwnd);
}
}
}真不明白为什么我的findwindow怎么取也取不到值每次都是0
解决方案 »
- 实现PhotoShop背景和图像拖动
- c#程序自动更新
- 求一正则表达式!
- 没分了,Excel.Application oExcel=new Excel.Application()无法创建抽象类或接口“Excel.Application”的实例
- 谁能帮我写个播放器啊 object参数写的
- 求正则表达式
- 监控软件,有做过的来给点经验
- HttpContext.Current.Request.Form.Get(key) == null
- 咨询:请问与web server,soap,Asp.net 方面经典的书籍是那些??或者电子书籍。如找到合适书籍定加分答谢
- 如何将COM控件中绘制的图形保存为GIF文件?
- 类型“DataGridLinkButton”的控件“DataGrid1__ctl2__ctl0”必须放在具有 runat=server 的窗体标记内?
- VC#2005中如何加入一个。DLL 文件(动态连接库)
http://blog.csdn.net/knight94/archive/2006/03/16/625809.aspx你可以使用process方法,从中获得mainwindowhandler,在进行你所需要的操作。
form1hwnd = FindWindow("Form1","window");with
form1hwnd = FindWindow( null, yourWindowName );
{
Process currentProcess=Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(currentProcess.ProcessName);//与当前进程名相同的所有进程
MessageBox.Show(currentProcess.ProcessName+"\n"+processes.Length.ToString());
foreach(Process process in processes)
{
if(process.Id != currentProcess.Id)
{
MessageBox.Show(Assembly.GetExecutingAssembly().Location+"\n"+currentProcess.MainModule.FileName);
//也许有相同的进程名,这时应该比较是否是同一个文件
if(process.MainModule.FileName == currentProcess.MainModule.FileName)
{
return process;//该进程已经运行过了,process是之前的实例
}
}
}
return null;//这是第一次运行
}