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

解决方案 »

  1.   

    ref:
    http://blog.csdn.net/knight94/archive/2006/03/16/625809.aspx你可以使用process方法,从中获得mainwindowhandler,在进行你所需要的操作。
      

  2.   

    这个要是改一下名字又可以运行了,我想用findwindow这个比较保险,也方便,能不能讲一下我用的时候怎么取的句柄是0啊
      

  3.   

    to 我想用findwindow这个比较保险,也方便,能不能讲一下我用的时候怎么取的句柄是0啊change
    form1hwnd = FindWindow("Form1","window");with
    form1hwnd = FindWindow( null, yourWindowName );
      

  4.   

    如果你的 窗体隐藏 this.hide(); 或 this.visible = false; process.MainWindowHandle 或 FindWindow 都返回 0
      

  5.   

    public static Process RunningInstance()
    {
    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;//这是第一次运行

      

  6.   

    就是在findwindow的时候不写类名而用窗体标题来查