Office 等微軟產品的「關於」對話框常隱藏彩蛋(開發人員名單、小遊戲等),需特定熱鍵觸發。本文實作一個具備 XP 風格介面的「關於」對話框,支援 Ctrl+Alt+F3 鍵盤熱鍵與 Ctrl+滑鼠右鍵雙擊 Logo 兩種觸發方式,並呈現平滑的視窗高度摺疊/展開動畫。

圖 1:預設狀態(隱藏彩蛋區)

圖 2:觸發後展開顯示隱藏資訊
OnInitDialog 計算隱藏區高度,MoveWindow 縮減視窗高度。RegisterHotKey(hWnd, ID, MOD_CONTROL|MOD_ALT, VK_F3) 註冊系統級熱鍵。SizeWindow() 以迴圈逐像素調整 CRect.bottom 並 MoveWindow,形成滑動效果。OnRButtonDblClk 檢查 Ctrl 鍵狀態與游標是否在 Logo 區域,符合則觸發。DestroyWindow 中 UnregisterHotKey 釋放熱鍵。#define WM_SHOWHOTKEY WM_USER + 101
BOOL CAboutDlg::OnInitDialog() {
CDialog::OnInitDialog();
// 計算隱藏區高度
CRect rc1, rc2;
GetDlgItem(IDC_DYCREDITS)->GetWindowRect(&rc1);
GetDlgItem(IDC_COPYRIGHT)->GetWindowRect(&rc2);
m_nReducedHeight = rc1.Height() + (rc1.top - rc2.bottom) / 2;
// 縮減視窗
CRect dlgRect; GetWindowRect(&dlgRect);
dlgRect.bottom -= m_nReducedHeight;
MoveWindow(&dlgRect);
m_bVertical = FALSE; // 預設摺疊
// 註冊系統熱鍵 Ctrl+Alt+F3
RegisterHotKey(GetSafeHwnd(), WM_SHOWHOTKEY, MOD_CONTROL | MOD_ALT, VK_F3);
return TRUE;
}
void CAboutDlg::OnMore() {
m_bVertical = !m_bVertical;
if (!m_bVertical) { // 摺疊
SetDlgItemText(ID_MORE, _T("更多>>"));
SizeWindow(m_nReducedHeight, true);
} else { // 展開
SetDlgItemText(ID_MORE, _T("<<隱藏"));
SizeWindow(m_nReducedHeight, false);
}
UpdateWindow();
}
void CAboutDlg::SizeWindow(int h, bool bShrink) {
CRect rc; GetWindowRect(&rc);
for (int i = 0; i < h; i++) {
bShrink ? rc.bottom-- : rc.bottom++;
MoveWindow(&rc);
}
}
// 鍵盤熱鍵 WM_HOTKEY
LRESULT CAboutDlg::OnHotKey(WPARAM wParam, LPARAM lParam) {
if (wParam == WM_SHOWHOTKEY) OnMore();
return 0;
}
// 滑鼠右鍵雙擊 + Ctrl
void CAboutDlg::OnRButtonDblClk(UINT nFlags, CPoint point) {
if (nFlags == (MK_CONTROL | MK_RBUTTON)) {
CWnd* pLogo = GetDlgItem(IDC_FLAG);
CRect rc; pLogo->GetWindowRect(&rc);
CPoint pt; GetCursorPos(&pt);
if (rc.PtInRect(pt)) OnMore();
}
CDialog::OnRButtonDblClk(nFlags, point);
}
BOOL CAboutDlg::DestroyWindow() {
UnregisterHotKey(GetSafeHwnd(), WM_SHOWHOTKEY);
return CDialog::DestroyWindow();
}
SetWindowPos + SWP_ASYNCWINDOWPOS 或 AnimateWindow API 獲得更平滑效果。RegisterHotKey 失敗時的 fallback 機制。本範例展示了如何以極少代碼實現專業級的「關於」對話框彩蛋功能:系統熱鍵註冊、滑鼠手勢偵測、視窗動畫摺疊、資源正確釋放。技術皆為標準 Win32/MFC API,相容性極佳,可直接移植至現有專案。