在c#里如何让一个程序只运行一次,再运行该程序则选中第一次运行界面

ljx0305 2006-07-23 10:14:34
这里,我可以实现程序只运行一次,但是没有办法做到再运行选中第一个运行界面,我想通过用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
...全文
813 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
qozms 2007-02-01
  • 打赏
  • 举报
回复
mark
ljx0305 2006-08-01
  • 打赏
  • 举报
回复
就是在findwindow的时候不写类名而用窗体标题来查
CmUpDate 2006-07-24
  • 打赏
  • 举报
回复
呼呼
RexZheng 2006-07-24
  • 打赏
  • 举报
回复
?
qqinxl 2006-07-24
  • 打赏
  • 举报
回复
请把解决方法告诉大家阿
我们这些新手都在等着呢

拜托!!
ljx0305 2006-07-24
  • 打赏
  • 举报
回复
这个问题以自己解决了,谢谢大家
yespie 2006-07-24
  • 打赏
  • 举报
回复

up
ljx0305 2006-07-23
  • 打赏
  • 举报
回复
我不是隐藏啊,我是一个程序只能运行一次,第二次的再运行这个程序的时候就不让这个程序继续运行,而是让第一个程序处理选中状态,就像千千静听这个播放听歌的程序那样
aSalt 2006-07-23
  • 打赏
  • 举报
回复
如果你的 窗体隐藏 this.hide(); 或 this.visible = false;

process.MainWindowHandle 或 FindWindow 都返回 0
Knight94 2006-07-23
  • 打赏
  • 举报
回复
to 我想用findwindow这个比较保险,也方便,能不能讲一下我用的时候怎么取的句柄是0啊

change
form1hwnd = FindWindow("Form1","window");

with
form1hwnd = FindWindow( null, yourWindowName );
ljx0305 2006-07-23
  • 打赏
  • 举报
回复
这个要是改一下名字又可以运行了,我想用findwindow这个比较保险,也方便,能不能讲一下我用的时候怎么取的句柄是0啊
Knight94 2006-07-23
  • 打赏
  • 举报
回复
ref:
http://blog.csdn.net/knight94/archive/2006/03/16/625809.aspx

你可以使用process方法,从中获得mainwindowhandler,在进行你所需要的操作。
ljx0305 2006-07-23
  • 打赏
  • 举报
回复
有没有人帮我啊
ljx0305 2006-07-23
  • 打赏
  • 举报
回复
但这里有个不足啊,要是按你那样做,我运行完程序把程序的名字一改不是又可以运行了吗,你试过这样的吗,不过还是谢谢你小草
socg 2006-07-23
  • 打赏
  • 举报
回复
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;//这是第一次运行
}
ljx0305 2006-07-23
  • 打赏
  • 举报
回复
怎么沉下去了顶起来,有没有人帮帮忙啊

111,119

社区成员

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

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

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