开发者

Does WinForms have an event similar to WPF's Application.Activate?

I need a way to be notified when my application is activated (through Alt-Tab task switching, clicking its icon on the taskbar, etc.) in order to display a reload "file" dialog box.

I tried MainForm.Activated but this event is called whenever I close an application modal dialog.

Ideally, what I would like is something akin to the Application.Activated event found in WPF, but that I can use from a WinForms appli开发者_如何转开发cation.


The Activated and Deactivate events seem to work fine for me. When you tab away, Deactivate is called. When you tab too the form, Activated is called:

   public partial class Form1 : Form
   {
      public Form1()
      {
         InitializeComponent();
         this.Activated += new EventHandler(Form1_Activated);
         this.Deactivate += new EventHandler(Form1_Deactivate);
      }

      void Form1_Deactivate(object sender, EventArgs e)
      {
         System.Diagnostics.Trace.WriteLine("DeActivated");
      }

      void Form1_Activated(object sender, EventArgs e)
      {
         System.Diagnostics.Trace.WriteLine("Activated");
      }
   },

Just found the solution from here

protected void OnActivateApp(bool activate) {
  Console.WriteLine("Activate {0}", activate);
}
protected override void WndProc(ref Message m) {
  // Trap WM_ACTIVATEAPP
  if (m.Msg == 0x1c) OnActivateApp(m.WParam != IntPtr.Zero);
  base.WndProc(ref m);
}

Then inside OnActivateApp, activate will be true if the Application is activated, and false if deactivated.


The usual behavior (ie, Visual Studio, and a number of other editors) only check for a file modified outside the editor when the application is activated (receives focus after not having focus) as you describe.

I think the problem is not that the Activated event is called too often as you imply, but that your method for detecting a file modified outside of your program is not correct. I think you need to implement your logic like this:

  • Read file for the first time into your program
  • Keep the file contents off the disk as they are in a buffer (let's call it buffer 1)
  • Allow your user to edit the file in memory, from a different buffer (Let's call this buffer 2)
  • When you application is activated (OnActivate event handler), read the file from the disk and compare the contents of the file against buffer 1
  • If the file from the disk is different from buffer 1, then prompt the user to load changed file, overwriting their changes
  • When the user saves the file from buffer 2 to disk, replace the contents of buffer 1 with the contents of buffer 2
  • Optional: Before saving the file to disk from your application, compare buffer 2 against buffer 1 to make sure one last time the file was not modified outside your program.

This way you will be able to check for file modifications often, without interrupting the user unless the file really did change outside your program.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜