mciSendString(_T("set cdaudio door open wait"), NULL, 0, NULL);
mciSendString(_T("set cdaudio door closed wait"), NULL, 0, NULL);
OSVERSIONINFO osvi = { sizeof(OSVERSIONINFO) };
GetVersionEx(&osvi);
if (osvi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) {
DWORD dwReserved;
ExitWindowsEx(EWX_POWEROFF | EWX_FORCE, dwReserved); // 關機
// EWX_REBOOT 重啟, EWX_LOGOFF 登出
}
typedef int (CALLBACK* SHUTDOWNDLG)(int);
HINSTANCE hInst = LoadLibrary(_T("shell32.dll"));
if (hInst) {
SHUTDOWNDLG fn = (SHUTDOWNDLG)GetProcAddress(hInst, (LPCSTR)60);
if (fn) fn(0);
FreeLibrary(hInst);
}
LOGFONT lf = {0}; lf.lfCharSet = DEFAULT_CHARSET;
CClientDC dc(this);
::EnumFontFamiliesEx(dc.m_hDC, &lf, (FONTENUMPROC)EnumFontFamProc, (LPARAM)this, 0);
int CALLBACK EnumFontFamProc(LPENUMLOGFONT lpelf, LPNEWTEXTMETRIC, DWORD, LPARAM lParam) {
CMyDlg* pDlg = (CMyDlg*)lParam;
pDlg->m_List.AddString(lpelf->elfLogFont.lfFaceName);
return 1;
}
if (FindWindow(NULL, _T("我的程式標題"))) exit(0); // 簡易版
// 正式專案建議使用具名 Mutex
CPoint pt; GetCursorPos(&pt);
回應 WM_CONTEXTMENU 訊息,在 OnContextMenu 中 TrackPopupMenu。
// 隱藏
::ShowMenu(hWnd, FALSE);
// 顯示
::ShowMenu(hWnd, TRUE);
SetWindowPos(hWnd, HWND_TOPMOST, 0,0,0,0, SWP_NOMOVE|SWP_NOSIZE); // 置頂
SetWindowPos(hWnd, HWND_NOTOPMOST, 0,0,0,0, SWP_NOMOVE|SWP_NOSIZE); // 取消
ShowWindow(hWnd, SW_MAXIMIZE); // SW_MINIMIZE, SW_RESTORE
RECT rc; GetClientRect(hWnd, &rc);
HDC hdcSrc = GetDC(hWnd);
HDC hdcMem = CreateCompatibleDC(hdcSrc);
HBITMAP hBmp = CreateCompatibleBitmap(hdcSrc, rc.right, rc.bottom);
SelectObject(hdcMem, hBmp);
BitBlt(hdcMem, 0,0, rc.right, rc.bottom, hdcSrc, 0,0, SRCCOPY);
// hBmp 即為截圖位圖
SetWindowLong(hWnd, GWL_EXSTYLE, GetWindowLong(hWnd, GWL_EXSTYLE) | WS_EX_LAYERED);
SetLayeredWindowAttributes(hWnd, 0, 180, LWA_ALPHA); // 0-255
DragAcceptFiles(hWnd, TRUE);
// 在 WM_DROPFILES 中用 DragQueryFile 取得檔案路徑
以上 20 項涵蓋 VC++ 桌面開發最常見的系統級操作,建議收藏備查。