2010-06-16 18 views
8

Scusa per il titolo strano, attualmente sto giocando con WinForms e mi chiedo se c'è un modo per farlo in modo che tu non 'Devo fare doppio clic' sulla finestra per attivare un oggetto in un menustrip quando la finestra è sfocata?Disabilita il "requisito" per fare doppio clic su una finestra sfocata quando fai clic su un menustrip

Attualmente se la finestra non è focalizzata, devo prima fare clic sulla finestra per focalizzarla e quindi fare nuovamente clic sull'oggetto menustrip anche se il mio mouse si trovava sopra l'oggetto menustrip dall'inizio.

Grazie in anticipo!

risposta

8

provare a mettere questa funzione nella classe Forma:

protected override void WndProc(ref Message m) { 
    int WM_PARENTNOTIFY = 0x0210; 
    if (!this.Focused && m.Msg == WM_PARENTNOTIFY) { 
     // Make this form auto-grab the focus when menu/controls are clicked 
     this.Activate(); 
    } 
    base.WndProc(ref m); 
} 
+0

Ha funzionato perfettamente, grazie mille! – Gustav

0

Il metodo @ risposta di Detmar si concentrerà la finestra quando la finestra viene distrutto (vedi https://msdn.microsoft.com/en-us/library/windows/desktop/hh454920(v=vs.85).aspx). Ciò può causare problemi se nella tua applicazione sono presenti più finestre e stai uscendo. Ecco uno che non si innesca quando si dispone di Windows:

protected override void WndProc(ref Message m) 
    { 
     const int WM_PARENTNOTIFY = 0x0210; 
     if (!this.Focused && m.Msg == WM_PARENTNOTIFY) 
     { 
      const int WM_CREATE = 0x0001; 
      const int WM_DESTROY = 0x0002; 
      const int WM_LBUTTONDOWN = 0x0201; 
      const int WM_MBUTTONDOWN = 0x0207; 
      const int WM_RBUTTONDOWN = 0x0204; 
      const int WM_XBUTTONDOWN = 0x020B; 
      const int WM_POINTERDOWN = 0x0246; 

      int type = (int)(0xFFFF & (long)m.WParam); 
      switch (type) 
      { 
       case WM_LBUTTONDOWN: 
       case WM_MBUTTONDOWN: 
       case WM_RBUTTONDOWN: 
       case WM_XBUTTONDOWN: 
       case WM_POINTERDOWN: 
        // Make this form auto-grab the focus when menu/controls are clicked 
        this.Activate(); 
        break; 
       case WM_DESTROY: 
       case WM_CREATE: 
        //do nothing 
        break; 
      } 
     } 
     base.WndProc(ref m); 
    } 
Problemi correlati