作者共發了2篇帖子。 內容轉換:不轉換▼
 
點擊 回復
563 1
SHGetFileInfo代码备份
一派護法 十九級
1樓 發表于:2016-7-30 20:34
#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;
}
一派護法 十九級
2樓 發表于:2016-7-30 20:35
编译环境:Visual Studio 2010 Express

回復帖子

內容:
用戶名: 您目前是匿名發表
驗證碼:
(快捷鍵:Ctrl+Enter)
 

本帖信息

點擊數:563 回複數:1
評論數: ?
作者:巨大八爪鱼
最後回復:巨大八爪鱼
最後回復時間:2016-7-30 20:35
 
©2010-2024 Arslanbar Ver2.0
除非另有聲明,本站採用創用CC姓名標示-相同方式分享 3.0 Unported許可協議進行許可。