一派护法 十九级 |
#include <tchar.h> #include <Windows.h> #include <CommCtrl.h> #include <ShellAPI.h> #define IDM_OPEN 1 #define IDM_EXIT 2 #pragma comment(lib, "comctl32.lib") #pragma comment(linker, "\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' language='*' publicKeyToken='6595b64144ccf1df'\"") HICON hIcon; SHFILEINFO info; void GetIcon(void) { LPTSTR szFileName = TEXT("F:\\我的资料\\2016\\演示文稿4.pptx"); SHGetFileInfo(szFileName, NULL, &info, sizeof(info), SHGFI_ICON | SHGFI_LARGEICON); hIcon = info.hIcon; SHGetFileInfo(szFileName, NULL, &info, sizeof(info), SHGFI_ICON | SHGFI_SMALLICON | SHGFI_DISPLAYNAME | SHGFI_TYPENAME); } LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { HDC hdc; int wmId; PAINTSTRUCT ps; RECT rect; switch (uMsg) { case WM_COMMAND: wmId = LOWORD(wParam); switch (wmId) { case IDM_OPEN: MessageBox(hWnd, TEXT("打开文件失败"), TEXT("错误"), MB_ICONWARNING); break; case IDM_EXIT: DestroyWindow(hWnd); break; } break; case WM_CREATE: CreateCaret(hWnd, NULL, NULL, 16); SetCaretPos(22, 21); ShowCaret(hWnd); break; case WM_DESTROY: DestroyIcon(hIcon); DestroyIcon(info.hIcon); DestroyCaret(); PostQuitMessage(0); break; case WM_PAINT: hdc = BeginPaint(hWnd, &ps); SetDCBrushColor(hdc, RGB(255, 0, 0)); SetRect(&rect, 20, 20, 120, 100); FillRect(hdc, &rect, (HBRUSH)GetStockObject(DC_BRUSH)); DrawIcon(hdc, 20, 120, hIcon); SelectObject(hdc, GetStockObject(DEFAULT_GUI_FONT)); TextOut(hdc, 60, 130, info.szDisplayName, lstrlen(info.szDisplayName)); EndPaint(hWnd, &ps); break; default: return DefWindowProc(hWnd, uMsg, wParam, lParam); } return FALSE; } int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { InitCommonControls(); HMENU hMenu = CreateMenu(); HMENU hSubMenu = CreatePopupMenu(); AppendMenu(hMenu, MF_POPUP | MF_STRING, (UINT_PTR)hSubMenu, TEXT("文件(&F)")); AppendMenu(hSubMenu, MF_STRING, IDM_OPEN, TEXT("打开(&O)")); AppendMenu(hSubMenu, MF_SEPARATOR, NULL, NULL); AppendMenu(hSubMenu, MF_STRING, IDM_EXIT, TEXT("退出(&X)")); GetIcon(); WNDCLASSEX wcex; ZeroMemory(&wcex, sizeof(wcex)); wcex.cbSize = sizeof(WNDCLASSEX); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hInstance = hInstance; wcex.hIcon = hIcon; wcex.hIconSm = info.hIcon; wcex.lpfnWndProc = WndProc; wcex.lpszClassName = TEXT("Example Window"); RegisterClassEx(&wcex); HWND hWnd = CreateWindow(wcex.lpszClassName, info.szTypeName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, hMenu, hInstance, NULL); if (!hWnd) { DestroyIcon(hIcon); DestroyIcon(info.hIcon); return 0; } ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; }
|