目前共有2篇帖子。 内容转换:不转换▼
 
点击 回复
562 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)
 

本帖信息

点击数:562 回复数:1
评论数: ?
作者:巨大八爪鱼
最后回复:巨大八爪鱼
最后回复时间:2016-7-30 20:35
 
©2010-2024 Arslanbar Ver2.0
除非另有声明,本站采用知识共享署名-相同方式共享 3.0 Unported许可协议进行许可。