📄 messagebox.cpp
字号:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES.
//
#include "MessageBox.hpp"
#include "Common.hpp"
#include "Controls.hpp"
#include "Debug.hpp"
#include "Layout.hpp"
#include "Resource.h"
#include <windowsx.h>
#include <commctrl.h>
EXTERN_C
BOOL
WINAPI
PHMessageBox(
PH_MESSAGE_BOX_PARAMETERS* pParameters
)
{
if (!pParameters ||
(pParameters->StructSize != sizeof(PH_MESSAGE_BOX_PARAMETERS)) ||
(!pParameters->Instance && (
(0 == HIWORD(pParameters->pTitle)) ||
(0 == HIWORD(pParameters->pText)) ||
(pParameters->IconId) ||
(pParameters->MenuId))
)
)
{
ASSERT(0);
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
MessageBoxImpl_t MessageBox;
HRESULT hr;
hr = MessageBox.Create(
MAKEINTRESOURCE(IDD_MESSAGE_BOX),
pParameters->Owner,
(pParameters->Flags & VDF_TYPE_MODELESS) ?
MessageBoxImpl_t::dtModelessDialog :
MessageBoxImpl_t::dtModalDialog,
pParameters,
&pParameters->result.Dialog
);
if (FAILED(hr))
{
return FALSE;
}
return TRUE;
}
//constants
const UINT MessageBoxImpl_t::sc_AnimateTime = 125;
/*------------------------------------------------------------------------------
MessageBoxImpl_t::MessageBoxImpl_t
Constructor
------------------------------------------------------------------------------*/
MessageBoxImpl_t::MessageBoxImpl_t(
)
{
TRACE(ZONE_COMMON_CTOR);
m_FirstTime = true;
}
/*------------------------------------------------------------------------------
MessageBoxImpl_t::~MessageBoxImpl_t
Destructor
------------------------------------------------------------------------------*/
MessageBoxImpl_t::~MessageBoxImpl_t(
)
{
TRACE(ZONE_COMMON_CTOR);
}
/*------------------------------------------------------------------------------
MessageBoxImpl_t::GetWindowClassName
WndClassName for this dialog
------------------------------------------------------------------------------*/
const WCHAR*
MessageBoxImpl_t::GetWindowClassName(
void
)
{
return WNDCLASS_MESSAGEBOX;
}
/*------------------------------------------------------------------------------
MessageBoxImpl_t::DialogWindowProc
WndProc for this dialog
------------------------------------------------------------------------------*/
LRESULT
MessageBoxImpl_t::DialogWindowProc(
UINT Message,
WPARAM wParam,
LPARAM lParam,
bool& Handled
)
{
//by default assume we handled the message
Handled = true;
switch (Message)
{
case WM_COMMAND:
return OnCommand(
LOWORD(wParam),
HIWORD(wParam),
reinterpret_cast<HWND>(lParam)
);
#ifdef AUTOMATION
case WM_GETTEXT:
return OnGetText(reinterpret_cast<WCHAR*>(lParam), wParam);
case WM_GETTEXTLENGTH:
return OnGetTextLen();
#endif
case WM_ERASEBKGND:
//pretend that the background was erased
return 1;
case WM_INITDIALOG:
return OnInitDialog(
reinterpret_cast<HWND>(wParam),
reinterpret_cast<PH_MESSAGE_BOX_PARAMETERS*>(lParam)
);
case WM_PAINT:
return OnPaint(reinterpret_cast<HDC>(wParam));
default:
Handled = false;
return 0;
}
}
HRESULT
MessageBoxImpl_t::AnimateMessageBox(
HBITMAP Bitmap
)
{
ANIMATERECTSINFO AnimateInfo = {0};
PaintHelper_t Buffer;
RECT WindowRect;
HRESULT hr;
GetWindowRect(m_hwnd, &WindowRect);
if (!Bitmap)
{
hr = Buffer.CreateCanvas(
NULL,
RECTWIDTH(WindowRect),
RECTHEIGHT(WindowRect)
);
if (FAILED(hr))
{
return hr;
}
OnPaint(Buffer);
Bitmap = Buffer.SetBitmap(NULL);
}
ASSERT(IsWindowVisible(m_hwnd));
AnimateInfo.hwnd = m_hwnd;
//animation grows up
SetRect(
&AnimateInfo.rcStart,
0,
WindowRect.bottom,
WindowRect.right,
WindowRect.bottom
);
SetRect(
&AnimateInfo.rcFinish,
WindowRect.left,
WindowRect.top,
WindowRect.right,
WindowRect.bottom
);
AnimateInfo.dwFlags = ARF_SYNCHRONOUS |
ARF_BITMAP |
ARF_ANIMATETIME |
ARF_INCLUDELAST |
ARF_BMPGROW |
ARF_USEPARENT;
AnimateInfo.dwAnimateTime = sc_AnimateTime;
AnimateInfo.hbmAnimate = Bitmap;
hr = AnimateRects(&AnimateInfo);
return hr;
}
LRESULT
MessageBoxImpl_t::OnCommand(
WORD CommandId,
WORD NotifyCode,
HWND Control
)
{
if ((m_Type == dtModalDialog) &&
(CommonUtilities_t::ControlTypeMenuButton == CommonUtilities_t::GetControlType(Control)))
{
End(CommandId);
return 0;
}
NMHDR nmhdr;
nmhdr.hwndFrom = Control;
nmhdr.idFrom = GetWindowLong(Control, GWL_ID);
nmhdr.code = CommandId;
SendMessage(m_Owner, WM_NOTIFY, CommandId, (LPARAM)&nmhdr);
return 0;
}
#ifdef AUTOMATION
LRESULT
MessageBoxImpl_t::OnGetText(
__out_ecount(cchBuffer) WCHAR* pBuffer,
int cchBuffer
)
{
if (!pBuffer || (cchBuffer <= 0))
{
return 0;
}
// Determine length of the message box title
int cchLength = DefWindowProc(
m_hwnd,
WM_GETTEXT,
cchBuffer,
reinterpret_cast<LPARAM>(pBuffer)
);
size_t cchRemaining = cchBuffer - cchLength;
pBuffer += cchLength;
const WCHAR c_Delimiter = STRING_DELIMITER;
StringCchCopyNExW(
pBuffer,
cchBuffer,
&c_Delimiter,
1,
&pBuffer,
&cchRemaining,
STRSAFE_NULL_ON_FAILURE
);
StringCchCopyExW(
pBuffer,
cchRemaining,
m_Text,
&pBuffer,
&cchRemaining,
0
);
StringCchCopyNExW(
pBuffer,
cchRemaining,
&c_Delimiter,
1,
NULL,
&cchRemaining,
0
);
return cchBuffer - cchRemaining;
}
LRESULT
MessageBoxImpl_t::OnGetTextLen(
void
)
{
// Determine length of the message box title
int Result = DefWindowProc(m_hwnd, WM_GETTEXTLENGTH, 0, 0);
// Add length of the message box text plus 2 delimiters
Result += m_Text.length() + 2;
return Result;
}
#endif
LRESULT
MessageBoxImpl_t::OnInitDialog(
HWND Focus,
PH_MESSAGE_BOX_PARAMETERS* pParameters
)
{
if (!pParameters || !pParameters->pText)
{
ASSERT(0);
SetLastError(ERROR_INVALID_PARAMETER);
return End(ERROR_CANCELLED);
}
m_pParameters = pParameters;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -