问题2:
http://www.syncfusion.com/FAQ/WinForms/FAQ_c40c.asp#q550q

解决方案 »

  1.   

    问题1:
    你可以定义一个bool变量,在Closing中判断是否执行e.Cancel=true
    同时,你重载Winform的WndProc(Message m)
    在其中改变bool变量
      

  2.   

    问题3:
    设置Form的Visable属性为False
      

  3.   

    1。你可以做一个全局变量来监测某项任务的完成,如果以完成,或者出现异常则改变该变量的值
    如果该值=true,则可以关闭
    2。这个问题在ArLi2003 的常用代码里有
    3.可以在OnPaint里写,但会闪
      

  4.   

    to lx1920,
    问题1中如何重载WndProc(Message m)?可否给段代码例子,谢谢。问题2中如果没有打开WINDOWS的性能计数服务那么就不能用那个FAQ上的方法,我试过了。
    这样做因为环境问题出错率很高,不知道还有没有更好的方法?问题3中我要根据条件动态判断是否让Form的Visable属性为True或False,但是我试了在Form的构造函数、Form_Load事件,或者其他初始化方法中设置form1.Visable=false都没有用,因为窗口都还没显示出来,你不让它显示当然是没有用的。你可以试一下,看有什么好的解决方法没有。
      

  5.   

    问题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;
      

  6.   

    1、我们都知道在窗口的Closing事件中可以用e.Cancel=true取消窗口关闭,可是这样做了以后关闭电脑时就必须要先关闭程序,否则电脑也也不能关闭和注销了,请问有什么方法可以解决这个问题?》这个方法本来是系统会发消息给程序,要求强制退出。如果客户机上设置了注册表中的HKEY_CURRENT_USER\Control Panel\Desktop\AutoEndTasks 为1 那么你的cancel 是无效的,都被强制关闭,如果没有设置为1 那么参照realplayer 9 的做法,做延时,如果用户在10 秒内没有按cancel 按钮就执行快速结束方案并强制退出(WaitToKillAppTimeout 默认是20 秒)。2、如何知道程序已经运行,运行了就不让程序再次运行,而只是激活程序窗口。》我的常用类中有 http://expert.csdn.net/Expert/TopicView1.asp?id=17052003、如何让起始窗口在没有显示的时候就已经永远Hide(),在哪个事件或者方法中写代码?》这个问题我也在考虑,因为我在做一个远程控制类木马的东东,我全部用class 而象你这样,必须窗体又可能无窗体似乎只有一个办法就是直接用class,在class 中决定是否application.run(new form1()) 我曾试过最小化、在InitializeComponent() 中强制hide 都没有用,因为学过windows 程序设计的人都知道windows 的程序其实在form 显示时是使用api 的CreateWindowEx (不必奇怪,虽然是你在编程其实只是编译器将你的程序转化成api 而已,感觉被微软玩了吧?)并且它create 第一个将被认为是主窗是不能不可见的,就是说不管如何,都会显示一下才会没掉,因为CreateWindowEx 之后是UpdateWindow 来更新客户区这期间是必然会显示一下的,所以用正常的窗体类是肯定会闪一下,所以最好就直接将程序做在class 中,在该class 引用window.form 然后也可以使用messagebox 之类的和窗体一样,如果要使用或显示窗体可以再create 一个总之,就是恢复成原始的C 语言建窗体的方式才可以
      

  7.   

    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
      

  8.   

    其实要想不要闪要可以,首先From.ShowTaskBar = false;再
    this.Location.X =-1000;
    this.Location.Y =-1000;
      

  9.   

    lx1920 的重载 WM_SYSCOMMAND 消息是个好方法,但和form_close 是一样的,因为WM_SYSCOMMAND 消息中的close 用alt+f4 和点击右上的关闭按钮都是一样会激发的,和系统是否退出没有关联
      

  10.   

    在构造函数里面
    this.ShowTaskBar = false;this.Location = new Point(-1000,-1000);
    在 Activate 事件里面 把  form的visavle 属性设为 false
    这样窗体就不会闪,一启动就隐藏了
    一般用这种方法 在 托盘 只显示一个图标 ,这时候必须用 NotifyIcon
      

  11.   

    第二个问题中在没有起用系统监视器的情况下使用下列代码就会报错:
    Process current = Process.GetCurrentProcess();
    Process[] processes = Process.GetProcessesByName (current.ProcessName);我也不知道如何使用系统监视器,反正在我的电脑上使用上面的代码就报系统性能计数器没有使用之类的错误.不知道还有较好的解决方法没有?
      

  12.   

    关于性能监视器的问题,你可以这样:
    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数组释放