整理 MFC 開發中常見的系統操作、UI 控制、資源存取等實用代碼片段,適合快速查閱複製。
// 彈出
mciSendString(_T("set cdaudio door open wait"), NULL, 0, NULL);
// 關閉
mciSendString(_T("set cdaudio door closed wait"), NULL, 0, NULL);
// 判斷 OS 版本
OSVERSIONINFO osvi = { sizeof(OSVERSIONINFO) };
GetVersionEx(&osvi);
if (osvi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) { // Win9x
ExitWindowsEx(EWX_REBOOT | EWX_FORCE, 0); // 重啟
} else { // NT 系列
// 需啟用 SE_SHUTDOWN_NAME 權限
HANDLE hToken; OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken);
TOKEN_PRIVILEGES tkp = {1, {{SE_SHUTDOWN_NAME, 0, SE_PRIVILEGE_ENABLED}}};
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, NULL, NULL);
ExitWindowsEx(EWX_REBOOT | EWX_FORCE, 0);
}
HMODULE hLib = LoadLibrary(_T("shell32.dll"));
typedef int (CALLBACK *SHUTDOWNDLG)(int);
SHUTDOWNDLG fn = (SHUTDOWNDLG)GetProcAddress(hLib, (LPCSTR)60); // 序數 60
if (fn) fn(0);
FreeLibrary(hLib);
LOGFONT lf = {0}; lf.lfCharSet = DEFAULT_CHARSET;
CClientDC dc(this);
EnumFontFamiliesEx(dc.m_hDC, &lf, (FONTENUMPROC)[](LPENUMLOGFONT lpelf, LPNEWTEXTMETRIC, DWORD, LPARAM lParam) {
CListBox* pList = (CListBox*)lParam;
pList->AddString(lpelf->elfLogFont.lfFaceName);
return 1;
}, (LPARAM)&m_fontList, 0);
// 在 InitInstance 最前端
if (::FindWindow(NULL, _T("我的程式標題"))) return FALSE; // 已運行則退出
CPoint pt; GetCursorPos(&pt); // pt.x, pt.y 為螢幕座標
void CMyDlg::OnContextMenu(CWnd* pWnd, CPoint point) {
CMenu menu; menu.LoadMenu(IDR_MY_CONTEXT);
CMenu* pPopup = menu.GetSubMenu(0);
pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, this);
}
CWnd* pMain = AfxGetMainWnd();
if (bHide) {
pMain->SetMenu(NULL); pMain->DrawMenuBar(); bHide = FALSE;
} else {
CMenu menu; menu.LoadMenu(IDR_MAINFRAME);
pMain->SetMenu(&menu); pMain->DrawMenuBar(); bHide = TRUE;
menu.Detach();
}
HICON hIcon = ExtractIcon(AfxGetInstanceHandle(), _T("Notepad.exe"), 0);
if (hIcon && hIcon != (HICON)-1) {
pDC->DrawIcon(10, 10, hIcon);
DestroyIcon(hIcon);
}
BOOL AdjustPos(CRect* pRect) {
CRect rcWork; SystemParametersInfo(SPI_GETWORKAREA, 0, &rcWork, 0);
const int SNAP = 10; bool moved = false;
if (abs(pRect->left - rcWork.left) < SNAP) { pRect->OffsetRect(rcWork.left - pRect->left, 0); moved = true; }
if (abs(pRect->top - rcWork.top) < SNAP) { pRect->OffsetRect(0, rcWork.top - pRect->top); moved = true; }
if (abs(pRect->right - rcWork.right) < SNAP) { pRect->OffsetRect(rcWork.right - pRect->right, 0); moved = true; }
if (abs(pRect->bottom - rcWork.bottom) < SNAP) { pRect->OffsetRect(0, rcWork.bottom - pRect->bottom); moved = true; }
return moved;
}
// 在 OnMoving 中呼叫
void CMyWnd::OnMoving(UINT fwSide, LPRECT pRect) {
CRect r(pRect); if (AdjustPos(&r)) *pRect = r;
CWnd::OnMoving(fwSide, pRect);
}
// OnCreate 中
CMenu* pSys = GetSystemMenu(FALSE);
pSys->AppendMenu(MF_SEPARATOR);
pSys->AppendMenu(MF_STRING, IDM_MY_ABOUT, _T("關於我們"));
// 再在 ON_COMMAND(IDM_MY_ABOUT, OnMyAbout) 處理
ShellExecute(NULL, _T("open"), _T("mailto:test@example.com"), NULL, NULL, SW_SHOWNORMAL);
ShellExecute(NULL, _T("open"), _T("https://example.com"), NULL, NULL, SW_SHOWNORMAL);
WinExec(_T("notepad.exe"), SW_SHOW);
// 新增
CMenu* pMenu = AfxGetMainWnd()->GetMenu()->GetSubMenu(0);
pMenu->AppendMenu(MF_SEPARATOR);
pMenu->AppendMenu(MF_STRING, ID_MY_ITEM, _T("永遠置頂"));
DrawMenuBar();
// 刪除
for (int i = pMenu->GetMenuItemCount()-1; i >= 0; --i) {
CString str; pMenu->GetMenuString(i, str, MF_BYPOSITION);
if (str == _T("永遠置頂")) { pMenu->DeleteMenu(i, MF_BYPOSITION); break; }
}
// 靜態:替換資源 IDR_MAINFRAME (16x16 與 32x32 皆需換)
// 動態:發送 WM_SETICON
PostMessage(WM_SETICON, ICON_BIG, (LPARAM)hBigIcon);
PostMessage(WM_SETICON, ICON_SMALL, (LPARAM)hSmallIcon);