Set location of MessageBox?

前端 未结 4 1289
情歌与酒
情歌与酒 2021-02-14 11:31

I want to print out a message using MessageBox (or similar). I would also like control over where exactly on the screen the box appears but can find nothing in the description o

4条回答
  •  感动是毒
    2021-02-14 11:55

    Step 1: Create a CBT hook to trap the creation of the message box:

    // global hook procedure
    HHOOK hhookCBTProc = 0;
    
    LRESULT CALLBACK pfnCBTMsgBoxHook(int nCode, WPARAM wParam, LPARAM lParam)
    {
      if (nCode == HCBT_CREATEWND)
      {
        CREATESTRUCT *pcs = ((CBT_CREATEWND *)lParam)->lpcs;
    
        if ((pcs->style & WS_DLGFRAME) || (pcs->style & WS_POPUP))
        {
          HWND hwnd = (HWND)wParam;
    
          // At this point you have the hwnd of the newly created 
          // message box that so you can position it at will
          SetWindowPos(hwnd, ...);
        }
      }
    
      return (CallNextHookEx(hhookCBTProc, nCode, wParam, lParam));
    }
    

    Step 2: Install/remove the hook before and after showing the message box:

    // set hook to center the message box that follows
    hhookCBTProc = SetWindowsHookEx(WH_CBT, 
                                    pfnCBTMsgBoxHook, 
                                    0, GetCurrentThreadId());
    
    int sResult = MessageBox(hwndParent, pszMsg, pszTitle, usStyle);
    
    // remove the hook
    UnhookWindowsHookEx(hhookCBTProc);
    

提交回复
热议问题