在c#里如何让一个程序只运行一次,再运行该程序则选中第一次运行界面
这里,我可以实现程序只运行一次,但是没有办法做到再运行选中第一个运行界面,我想通过用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