Delphi - How do you generate an event when a user clicks outside modal dialog?

前端 未结 3 508
渐次进展
渐次进展 2020-12-29 11:22

Is it possible to fire an event when the user clicks outside a modal dialog?

OK, Windows provides it\'s own clues when you do this by making the \"bonk\" sound, or

3条回答
  •  南方客
    南方客 (楼主)
    2020-12-29 12:15

    I'm not sure how to do this in delphi but using C++ you could do something like this:

     // The message loop for our modal dialogbox
     BOOL CALLBACK DialogProc(HWND hwndDlg,
                              UINT uMsg,
                              WPARAM wParam,
                              LPARAM lParam) {
          switch(uMsg) {
            case WM_INITDIALOG:
              return TRUE;
              break;
            case WM_COMMAND:
              switch(wParam) {
                case IDOK:
                  EndDialog(hwndDlg, 0);
                  return TRUE;
                  break;
              }
              break;
            case WM_ACTIVATE:
              // message sent when the window if being activated/deactivated
              if(wParam == WA_INACTIVE) {
                // the window is being inactivated so beep once
                Beep(750, 300);
                // bring dialog to the foreground
                SetForegroundWindow(hwndDlg);
              }
              break;
          }
          return FALSE;
     }
    
     int main(int argc,char** argv) {
         // create a modal dialog
         DialogBox(GetModuleHandle(NULL),
                   MAKEINTRESOURCE(IDD_MYDIALOG),
                   HWND_DESKTOP,
                   DialogProc);
         return 0;
     }
    

    You could also have a look at SetWindowsHookEx() and perhaps Subclassing Controls that may point you in the right direction.

提交回复
热议问题