BOOL Win32PopupDialog::ShowMessageBox(HWND hwnd) { BOOL fSuccess = FALSE; HDC hdc = GetDC(hwnd); if (hdc) { NONCLIENTMETRICSW ncm = { sizeof(ncm) }; if (SystemParametersInfoW(SPI_GETNONCLIENTMETRICS, 0, &ncm, 0)) { DialogTemplate tmp; std::wstring ws; int controlCount = 2; // at minimum, static label and OK button if(this->showCancelButton) controlCount++; if(this->showInputText) controlCount++; int labelHeight = 14; int width = 300; int height = 90; int margin = 10; int buttonWidth = 50; int buttonHeight = 14; int inputHeight = 14; if(! this->showInputText) { height -= (inputHeight + margin); } // Write out the extended dialog template header tmp.Write<WORD>(1); // dialog version tmp.Write<WORD>(0xFFFF); // extended dialog template tmp.Write<DWORD>(0); // help ID tmp.Write<DWORD>(0); // extended style tmp.Write<DWORD>(WS_CAPTION | DS_FIXEDSYS | DS_SETFONT | DS_MODALFRAME); // DS_FIXEDSYS removes the close decoration tmp.Write<WORD>(controlCount); // number of controls tmp.Write<WORD>(32); // X tmp.Write<WORD>(32); // Y tmp.Write<WORD>(width); // width tmp.Write<WORD>(height); // height tmp.WriteString(L""); // no menu tmp.WriteString(L""); // default dialog class //tmp.WriteString(pszTitle); // title tmp.WriteString(ws.assign(title.begin(), title.end()).c_str()); // title // Next comes the font description. // See text for discussion of fancy formula. if (ncm.lfMessageFont.lfHeight < 0) { ncm.lfMessageFont.lfHeight = -MulDiv(ncm.lfMessageFont.lfHeight, 72, GetDeviceCaps(hdc, LOGPIXELSY)); } tmp.Write<WORD>((WORD)ncm.lfMessageFont.lfHeight); // point tmp.Write<WORD>((WORD)ncm.lfMessageFont.lfWeight); // weight tmp.Write<BYTE>(ncm.lfMessageFont.lfItalic); // Italic tmp.Write<BYTE>(ncm.lfMessageFont.lfCharSet); // CharSet tmp.WriteString(ncm.lfMessageFont.lfFaceName); // First control - static label tmp.AlignToDword(); tmp.Write<DWORD>(0); // help id tmp.Write<DWORD>(0); // window extended style tmp.Write<DWORD>(WS_CHILD | WS_VISIBLE); // style tmp.Write<WORD>(margin); // x tmp.Write<WORD>(margin); // y tmp.Write<WORD>(width - (2 * margin)); // width tmp.Write<WORD>(labelHeight); // height tmp.Write<DWORD>(-1); // control ID tmp.Write<DWORD>(0x0082FFFF); // static tmp.WriteString(ws.assign(message.begin(), message.end()).c_str()); // text tmp.Write<WORD>(0); // no extra data // Second control - the OK button. tmp.AlignToDword(); tmp.Write<DWORD>(0); // help id tmp.Write<DWORD>(0); // window extended style tmp.Write<DWORD>(WS_CHILD | WS_VISIBLE | WS_GROUP | WS_TABSTOP | BS_DEFPUSHBUTTON); // style tmp.Write<WORD>(width - margin - buttonWidth); // x tmp.Write<WORD>(height - margin - buttonHeight); // y tmp.Write<WORD>(buttonWidth); // width tmp.Write<WORD>(buttonHeight); // height tmp.Write<DWORD>(IDOK); // control ID tmp.Write<DWORD>(0x0080FFFF); // button class atom tmp.WriteString(L"OK"); // text tmp.Write<WORD>(0); // no extra data if(this->showCancelButton) { // The Cancel button tmp.AlignToDword(); tmp.Write<DWORD>(0); // help id tmp.Write<DWORD>(0); // window extended style tmp.Write<DWORD>(WS_CHILD | WS_VISIBLE | WS_GROUP | WS_TABSTOP | BS_DEFPUSHBUTTON); // style tmp.Write<WORD>(width - 2 * margin - 2 * buttonWidth); // x tmp.Write<WORD>(height - margin - buttonHeight); // y tmp.Write<WORD>(buttonWidth); // width tmp.Write<WORD>(buttonHeight); // height tmp.Write<DWORD>(IDCANCEL); // control ID tmp.Write<DWORD>(0x0080FFFF); // button class atom tmp.WriteString(L"Cancel"); // text tmp.Write<WORD>(0); // no extra data } if(this->showInputText) { // The input field tmp.AlignToDword(); tmp.Write<DWORD>(0); // help id tmp.Write<DWORD>(0); // window extended style tmp.Write<DWORD>(ES_LEFT | WS_BORDER | WS_TABSTOP | WS_CHILD | WS_VISIBLE); // style tmp.Write<WORD>(margin); // x tmp.Write<WORD>(margin + labelHeight + margin); // y tmp.Write<WORD>(width - (2 * margin)); // width tmp.Write<WORD>(inputHeight); // height tmp.Write<DWORD>(ID_INPUT_FIELD); // control ID tmp.Write<DWORD>(0x0081FFFF); // edit class atom tmp.WriteString(ws.assign(inputText.begin(), inputText.end()).c_str()); // text tmp.Write<WORD>(0); // no extra data } // Template is ready - go display it. fSuccess = DialogBoxIndirect(GetModuleHandle(NULL), tmp.Template(), hwnd, &Win32PopupDialog::Callback) >= 0; } ReleaseDC(NULL, hdc); // fixed 11 May } return fSuccess; }
BOOL Win32PopupDialog::ShowMessageBox(HWND hwnd) { BOOL fSuccess = FALSE; HDC hdc = GetDC(hwnd); if (hdc) { NONCLIENTMETRICSW nonClientMetrics = { sizeof(NONCLIENTMETRICSW) }; if (SystemParametersInfoW( SPI_GETNONCLIENTMETRICS, 0, &nonClientMetrics, 0)) { DialogTemplate tmp; std::wstring wideTitle(::UTF8ToWide(title)); std::wstring wideInputText; if (this->showInputText) wideInputText.assign(::UTF8ToWide(inputText)); int controlCount = 2; // at minimum, static label and OK button if (this->showCancelButton) controlCount++; if (this->showInputText) controlCount++; int messageLines = 0; int newlines = CountMatches(message, "\n"); int tabs = CountMatches(message, "\t"); int spaces = CountMatches(message, " "); if (newlines == 0 && tabs == 0 && spaces == 0) { std::string tempMessage(message); if (tempMessage.length() != 0) { int insertAt = 60; int count = 0; std::string::iterator it = tempMessage.begin(); for (; it < tempMessage.end(); it++) { count++; if (count == 60) { count = 0; message.insert(insertAt, "\n"); insertAt += 60; } } } } std::wstring wideMessage(::UTF8ToWide(message)); messageLines = message.length() / 60; messageLines += ((int) ceil((double)tabs / 14)); messageLines += newlines; if (tabs == 0 || newlines == 0) messageLines++; int labelHeight = 14; int width = 200; int margin = 10; int buttonWidth = 50; int buttonHeight = 14; int inputHeight = 14; int messageHeight = (messageLines * 12) + (messageLines * margin); int height = messageHeight + 56; if (!this->showInputText) { height -= (inputHeight + margin); } // Write out the extended dialog template header tmp.Write<WORD>(1); // dialog version tmp.Write<WORD>(0xFFFF); // extended dialog template tmp.Write<DWORD>(0); // help ID tmp.Write<DWORD>(0); // extended style tmp.Write<DWORD>(WS_CAPTION | WS_BORDER | DS_ABSALIGN | DS_SETFONT); tmp.Write<WORD>(controlCount); // number of controls tmp.Write<WORD>(32); // X tmp.Write<WORD>(32); // Y tmp.Write<WORD>(width); // width tmp.Write<WORD>(height); // height tmp.WriteString(L""); // no menu tmp.WriteString(L""); // default dialog class //tmp.WriteString(pszTitle); // title tmp.WriteString(wideTitle.c_str()); // title // Next comes the font description. // See text for discussion of fancy formula. if (nonClientMetrics.lfMessageFont.lfHeight < 0) { int dpi = GetDeviceCaps(hdc, LOGPIXELSY); nonClientMetrics.lfMessageFont.lfHeight = -MulDiv(nonClientMetrics.lfMessageFont.lfHeight, 72, dpi); } tmp.Write<WORD>((WORD)nonClientMetrics.lfMessageFont.lfHeight); // point tmp.Write<WORD>((WORD)nonClientMetrics.lfMessageFont.lfWeight); // weight tmp.Write<BYTE>(nonClientMetrics.lfMessageFont.lfItalic); // Italic tmp.Write<BYTE>(nonClientMetrics.lfMessageFont.lfCharSet); // CharSet tmp.WriteString(nonClientMetrics.lfMessageFont.lfFaceName); // First control - static label tmp.AlignToDword(); tmp.Write<DWORD>(0); // help id tmp.Write<DWORD>(0); // window extended style tmp.Write<DWORD>(WS_CHILD | WS_VISIBLE | SS_LEFT); // style tmp.Write<WORD>(margin); // x tmp.Write<WORD>(margin); // y tmp.Write<WORD>(width - (2 * margin)); // width //tmp.Write<WORD>(labelHeight); // height tmp.Write<WORD>(messageHeight); // height tmp.Write<DWORD>(-1); // control ID tmp.Write<DWORD>(0x0082FFFF); // static //tmp.Write<DWORD>(SS_LEFT); tmp.WriteString(wideMessage.c_str()); // text tmp.Write<WORD>(0); // no extra data // Second control - the OK button. tmp.AlignToDword(); tmp.Write<DWORD>(0); // help id tmp.Write<DWORD>(0); // window extended style tmp.Write<DWORD>(WS_CHILD | WS_VISIBLE | WS_GROUP | WS_TABSTOP | BS_DEFPUSHBUTTON); // style tmp.Write<WORD>(width - margin - buttonWidth); // x tmp.Write<WORD>(height - margin - buttonHeight); // y tmp.Write<WORD>(buttonWidth); // width tmp.Write<WORD>(buttonHeight); // height tmp.Write<DWORD>(IDCANCEL); // control ID tmp.Write<DWORD>(0x0080FFFF); // button class atom tmp.WriteString(L"Cancel"); // text tmp.Write<WORD>(0); // no extra data if (this->showCancelButton) { // The Cancel button tmp.AlignToDword(); tmp.Write<DWORD>(0); // help id tmp.Write<DWORD>(0); // window extended style tmp.Write<DWORD>(WS_CHILD | WS_VISIBLE | WS_GROUP | WS_TABSTOP | BS_DEFPUSHBUTTON); // style tmp.Write<WORD>(width - 2 * margin - 2 * buttonWidth); // x tmp.Write<WORD>(height - margin - buttonHeight); // y tmp.Write<WORD>(buttonWidth); // width tmp.Write<WORD>(buttonHeight); // height tmp.Write<DWORD>(IDOK); // control ID tmp.Write<DWORD>(0x0080FFFF); // button class atom tmp.WriteString(L"OK"); // text tmp.Write<WORD>(0); // no extra data } if (this->showInputText) { // The input field tmp.AlignToDword(); tmp.Write<DWORD>(0); // help id tmp.Write<DWORD>(0); // window extended style tmp.Write<DWORD>(ES_LEFT | WS_BORDER | WS_TABSTOP | WS_CHILD | WS_VISIBLE); // style tmp.Write<WORD>(margin); // x tmp.Write<WORD>(margin + labelHeight + margin); // y tmp.Write<WORD>(width - (2 * margin)); // width tmp.Write<WORD>(inputHeight); // height tmp.Write<DWORD>(ID_INPUT_FIELD); // control ID tmp.Write<DWORD>(0x0081FFFF); // edit class atom tmp.WriteString(wideInputText.c_str()); // text tmp.Write<WORD>(0); // no extra data } // Template is ready - go display it. fSuccess = DialogBoxIndirect( GetModuleHandle(NULL), tmp.Template(), hwnd, &Win32PopupDialog::Callback) >= 0; } ReleaseDC(NULL, hdc); // fixed 11 May } return fSuccess; }