to lx1920, 问题1中如何重载WndProc(Message m)?可否给段代码例子,谢谢。问题2中如果没有打开WINDOWS的性能计数服务那么就不能用那个FAQ上的方法,我试过了。 这样做因为环境问题出错率很高,不知道还有没有更好的方法?问题3中我要根据条件动态判断是否让Form的Visable属性为True或False,但是我试了在Form的构造函数、Form_Load事件,或者其他初始化方法中设置form1.Visable=false都没有用,因为窗口都还没显示出来,你不让它显示当然是没有用的。你可以试一下,看有什么好的解决方法没有。
问题2: Process instance = RunningInstance(); if (instance == null) { //There isn't another instance, show our form. Application.Run (new MainForm()); } else { MessageBox.Show("已经运行一个程序"); //There is another instance of this process. HandleRunningInstance(instance); } 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;
Public Const SC_CLOSE As Integer = 61536 Public Const WM_SYSCOMMAND As Integer = 274 Protected Overloads Overrides Sub WndProc(ByRef m As Message) If m.Msg = WM_SYSCOMMAND AndAlso m.WParam.ToInt32 = SC_CLOSE Then '在这里改变Bool变量 End If MyBase.WndProc(m)End Sub
第二个问题中在没有起用系统监视器的情况下使用下列代码就会报错: Process current = Process.GetCurrentProcess(); Process[] processes = Process.GetProcessesByName (current.ProcessName);我也不知道如何使用系统监视器,反正在我的电脑上使用上面的代码就报系统性能计数器没有使用之类的错误.不知道还有较好的解决方法没有?
关于性能监视器的问题,你可以这样: dim aaa As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SYSTEM", True).OpenSubKey("CurrentControlSet", True).OpenSubKey("Services", True).OpenSubKey("PerfProc", True).OpenSubKey("Performance", True) If Not aaa.GetValue("Disable Performance Counters") Is Nothing Then If aaa.GetValue("Disable Performance Counters").ToString = "1" Then aaa.SetValue("Disable Performance Counters", 0) End If End If同时,在 Process current = Process.GetCurrentProcess(); Process[] processes = Process.GetProcessesByName (current.ProcessName); 执行后,要将process数组释放
你可以定义一个bool变量,在Closing中判断是否执行e.Cancel=true
同时,你重载Winform的WndProc(Message m)
在其中改变bool变量
设置Form的Visable属性为False
如果该值=true,则可以关闭
2。这个问题在ArLi2003 的常用代码里有
3.可以在OnPaint里写,但会闪
问题1中如何重载WndProc(Message m)?可否给段代码例子,谢谢。问题2中如果没有打开WINDOWS的性能计数服务那么就不能用那个FAQ上的方法,我试过了。
这样做因为环境问题出错率很高,不知道还有没有更好的方法?问题3中我要根据条件动态判断是否让Form的Visable属性为True或False,但是我试了在Form的构造函数、Form_Load事件,或者其他初始化方法中设置form1.Visable=false都没有用,因为窗口都还没显示出来,你不让它显示当然是没有用的。你可以试一下,看有什么好的解决方法没有。
Process instance = RunningInstance();
if (instance == null)
{
//There isn't another instance, show our form.
Application.Run (new MainForm());
}
else
{
MessageBox.Show("已经运行一个程序");
//There is another instance of this process.
HandleRunningInstance(instance);
}
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;
Public Const WM_SYSCOMMAND As Integer = 274
Protected Overloads Overrides Sub WndProc(ByRef m As Message) If m.Msg = WM_SYSCOMMAND AndAlso m.WParam.ToInt32 = SC_CLOSE Then
'在这里改变Bool变量
End If
MyBase.WndProc(m)End Sub
this.Location.X =-1000;
this.Location.Y =-1000;
this.ShowTaskBar = false;this.Location = new Point(-1000,-1000);
在 Activate 事件里面 把 form的visavle 属性设为 false
这样窗体就不会闪,一启动就隐藏了
一般用这种方法 在 托盘 只显示一个图标 ,这时候必须用 NotifyIcon
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName (current.ProcessName);我也不知道如何使用系统监视器,反正在我的电脑上使用上面的代码就报系统性能计数器没有使用之类的错误.不知道还有较好的解决方法没有?
dim aaa As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SYSTEM", True).OpenSubKey("CurrentControlSet", True).OpenSubKey("Services", True).OpenSubKey("PerfProc", True).OpenSubKey("Performance", True)
If Not aaa.GetValue("Disable Performance Counters") Is Nothing Then
If aaa.GetValue("Disable Performance Counters").ToString = "1" Then
aaa.SetValue("Disable Performance Counters", 0)
End If
End If同时,在
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName (current.ProcessName);
执行后,要将process数组释放