Skip to content
This repository has been archived by the owner on Aug 12, 2018. It is now read-only.

Commit

Permalink
center MessageBox to the window of the program.
Browse files Browse the repository at this point in the history
  • Loading branch information
zufuliu committed Aug 16, 2017
1 parent 30364fb commit efa89db
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/Dialogs.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,10 @@ int MsgBox(int iType,UINT uIdMsg,...)
case MBOKCANCEL: iIcon = MB_ICONEXCLAMATION | MB_OKCANCEL; break;
}

if (!(hwnd = GetFocus()))
if (!(hwnd = GetActiveWindow()))
hwnd = hwndMain;

PostMessage(hwndMain, APPM_CENTER_MESSAGE_BOX, (WPARAM)hwnd, 0);
return MessageBoxEx(hwnd,
szText,szTitle,
MB_SETFOREGROUND | iIcon,
Expand Down Expand Up @@ -138,6 +139,9 @@ void DisplayCmdLineHelp(HWND hwnd)
mbp.lpfnMsgBoxCallback = NULL;
mbp.dwLanguageId = MAKELANGID(LANG_NEUTRAL,SUBLANG_NEUTRAL);

if (hwnd != NULL) {
PostMessage(hwndMain, APPM_CENTER_MESSAGE_BOX, (WPARAM)hwnd, 0);
}
MessageBoxIndirect(&mbp);
}

Expand Down
6 changes: 6 additions & 0 deletions src/Dialogs.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
#define MBYESNOCANCEL 4
#define MBOKCANCEL 8

/**
* App message used to center MessageBox to the window of the program.
* https://stackoverflow.com/questions/6299797/c-how-to-center-messagebox
*/
#define APPM_CENTER_MESSAGE_BOX (WM_APP + 1)

int MsgBox(int,UINT,...);
void DisplayCmdLineHelp(HWND hwnd);
BOOL GetDirectory(HWND,int,LPWSTR,LPCWSTR,BOOL);
Expand Down
7 changes: 4 additions & 3 deletions src/Helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -474,18 +474,19 @@ void SetWindowTransparentMode(HWND hwnd,BOOL bTransparentMode)
//
void CenterDlgInParent(HWND hDlg)
{
CenterDlgInParentEx(hDlg, GetParent(hDlg));
}

void CenterDlgInParentEx(HWND hDlg, HWND hParent)
{
RECT rcDlg;
HWND hParent;
RECT rcParent;
MONITORINFO mi;
HMONITOR hMonitor;

int xMin, yMin, xMax, yMax, x, y;

GetWindowRect(hDlg,&rcDlg);

hParent = GetParent(hDlg);
GetWindowRect(hParent,&rcParent);

hMonitor = MonitorFromRect(&rcParent,MONITOR_DEFAULTTONEAREST);
Expand Down
1 change: 1 addition & 0 deletions src/Helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ BOOL SetWindowTitle(HWND,UINT,BOOL,UINT,LPCWSTR,int,BOOL,UINT,BOOL,LPCWSTR);
void SetWindowTransparentMode(HWND,BOOL);


void CenterDlgInParentEx(HWND hDlg, HWND hParent);
void CenterDlgInParent(HWND);
void GetDlgPos(HWND,LPINT,LPINT);
void SetDlgPos(HWND,int,int);
Expand Down
9 changes: 9 additions & 0 deletions src/Notepad2.c
Original file line number Diff line number Diff line change
Expand Up @@ -1520,6 +1520,15 @@ LRESULT CALLBACK MainWndProc(HWND hwnd,UINT umsg,WPARAM wParam,LPARAM lParam)
}
break;

case APPM_CENTER_MESSAGE_BOX: {
HWND box = FindWindow(L"#32770", NULL);
HWND parent = GetParent(box);
// MessageBox belongs to us.
if (parent == (HWND)wParam || parent == hwndMain) {
CenterDlgInParentEx(box, parent);
}
}
break;

default:

Expand Down

0 comments on commit efa89db

Please sign in to comment.