萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> 程式語言綜合

VC++ 開發實用小技巧大全

VC++ (MFC) 開發常用技巧速查

整理 MFC 開發中常見的系統操作、UI 控制、資源存取等實用代碼片段,適合快速查閱複製。

1. 光碟機控制

// 彈出
mciSendString(_T("set cdaudio door open wait"), NULL, 0, NULL);
// 關閉
mciSendString(_T("set cdaudio door closed wait"), NULL, 0, NULL);

2. 關機/重啟/登出

// 判斷 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);
}

3. 顯示系統關機對話框

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);

4. 列舉系統字體

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);

5. 單一實例應用程式

// 在 InitInstance 最前端
if (::FindWindow(NULL, _T("我的程式標題"))) return FALSE; // 已運行則退出

6. 取得滑鼠螢幕座標

CPoint pt; GetCursorPos(&pt); // pt.x, pt.y 為螢幕座標

7. 右鍵選單 (OnContextMenu)

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);
}

8. 動態顯示/隱藏主選單

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();
}

9. 擷取可執行檔圖示

HICON hIcon = ExtractIcon(AfxGetInstanceHandle(), _T("Notepad.exe"), 0);
if (hIcon && hIcon != (HICON)-1) {
    pDC->DrawIcon(10, 10, hIcon);
    DestroyIcon(hIcon);
}

10. 視窗自動吸附螢幕邊緣

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);
}

11. 系統選單加入自訂項目

// OnCreate 中
CMenu* pSys = GetSystemMenu(FALSE);
pSys->AppendMenu(MF_SEPARATOR);
pSys->AppendMenu(MF_STRING, IDM_MY_ABOUT, _T("關於我們"));
// 再在 ON_COMMAND(IDM_MY_ABOUT, OnMyAbout) 處理

12. 執行外部程式/開啟網址/郵件

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);

13. 動態增刪選單項目

// 新增
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; }
}

14. 動態變更應用程式圖示

// 靜態:替換資源 IDR_MAINFRAME (16x16 與 32x32 皆需換)
// 動態:發送 WM_SETICON
PostMessage(WM_SETICON, ICON_BIG, (LPARAM)hBigIcon);
PostMessage(WM_SETICON, ICON_SMALL, (LPARAM)hSmallIcon);
copyright © 萬盛學電腦網 all rights reserved