| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 7322 人关注过本帖
标题:[求助]打开窗体时判断是否已打开
只看楼主 加入收藏
bygg
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:乖乖的心中
等 级:版主
威 望:241
帖 子:13555
专家分:3076
注 册:2006-10-23
收藏
得分:0 
楼上的方法是不是有点太复杂了。呵……

飘过~~
2007-02-24 13:02
jacklee
Rank: 7Rank: 7Rank: 7
来 自:XAplus
等 级:贵宾
威 望:32
帖 子:1769
专家分:104
注 册:2006-11-3
收藏
得分:0 
楼上的楼上只是说的MDI窗口形式吧?

XAplus!
讨论群:51090447
删吧删吧,把我的号给删了!
2007-02-26 18:43
zhangbo0817
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2007-2-23
收藏
得分:0 
是的,上面的代码是MDI中判断窗体是否已经存在的方法,如果不是MDI窗体,则调用窗体设全局变量(Flag),并将该全局变量做为参数传递到被调用窗体,被调用窗体初始化时设置Flag为true,关闭设置为false

2007-03-05 19:24
IT浪子
Rank: 4
来 自:Nyist
等 级:贵宾
威 望:13
帖 子:402
专家分:20
注 册:2006-11-24
收藏
得分:0 

//只允许打开一个应用程序实例
//需要添加的命名空间
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Reflection;
public static Process RunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName (current.ProcessName);

//Loop through the running processes in with the same name
foreach (Process process in processes)
{
//Ignore the current process
if (process.Id != current.Id)
{
//Make sure that the process is running from the exe file.
if (Assembly.GetExecutingAssembly().Location.Replace("/","\\") ==
current.MainModule.FileName)
{
//Return the other process instance.
return process;
}
}
}

//No other instance was found, return null.
return null;
}


public static void HandleRunningInstance(Process instance)
{
//Make sure the window is not minimized or maximized
ShowWindowAsync (instance.MainWindowHandle , WS_SHOWNORMAL);

//Set the real intance to foreground window
SetForegroundWindow (instance.MainWindowHandle);
}

[DllImport("User32.dll")]

private static extern bool ShowWindowAsync(
IntPtr hWnd, int cmdShow);
[DllImport("User32.dll")] private static extern bool
SetForegroundWindow(IntPtr hWnd);
private const int WS_SHOWNORMAL = 1;
}

static void Main()
{
//Get the running instance.
Process instance = RunningInstance();
if (instance == null)
{
//There isn't another instance, show our form.
frmMediaPlayer objFromMediaPlayer=new frmMediaPlayer();
Application.Run (objFromMediaPlayer);
}
else
{
//There is another instance of this process.
HandleRunningInstance(instance);
}

}
这个是通用的,我感觉看明白不难,不过代码倒是挺多的
不知道楼主要的是不是这个


你不能让所有人都满意,因为不是所有人都是人!
2007-03-05 22:43
liuminghui
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:2882
专家分:0
注 册:2007-1-26
收藏
得分:0 
鼎鼎

海鸽 is My Lover!!
2007-03-06 09:00
CrazyWeed0907
Rank: 2
等 级:新手上路
威 望:5
帖 子:1385
专家分:0
注 册:2006-5-30
收藏
得分:0 

用个单件模式就行了
比如
class Form2 : form
{
private static bool _isExisted = false;
public static FormShow()
{
if( ! _isExisted)
{
_isExisted = true ;
new Form2().show();

}
}
}


“十步杀一人,千里不留行。事了拂衣去,深藏身与名。”
2007-03-06 13:30
YSKING
Rank: 5Rank: 5
来 自:中国绿城
等 级:贵宾
威 望:16
帖 子:1380
专家分:25
注 册:2006-11-11
收藏
得分:0 

打开新窗体时,设置打开的窗体为对话框模式不就可以避免了?


仍然自由自我,永远高唱我歌,走遍千里...
2007-03-06 13:38
liwanxi
Rank: 1
等 级:新手上路
帖 子:35
专家分:0
注 册:2007-3-1
收藏
得分:0 
我也遇到这种问题,各位大侠出手帮忙一下,谢谢。
上面的代码好像都可以,可能与我定义的窗体有关吧
我是想打开普通窗体不是子窗体啊。
谢谢。

刚学C#,希望各位大侠指点,谢谢。
2007-11-19 08:43
快速回复:[求助]打开窗体时判断是否已打开
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019724 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved