Win32 API中的表格视图

时间:2011-09-30 09:58:11

标签: winapi

我是win32 API的新手,我正在尝试编写一个应用程序来计算通过菜单选择的文件的md5sum,或者在窗口中拖放。我希望窗口有3列,以便它可以分别显示文件名,路径和md5sum。有人可以给我一个示例代码,说明如何使我的窗口看起来像这样吗?

也可以有人在保留菜单和拖放选项的同时添加代码以创建窗口作为列表框吗?

LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{   
    OPENFILENAME ofn;
    char szFileName[MAX_PATH] = "";

    ZeroMemory(&ofn, sizeof(ofn));

    ofn.lStructSize = sizeof(OPENFILENAME); // SEE NOTE BELOW
    ofn.hwndOwner = hwnd;
    ofn.lpstrFilter = L"Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
    wchar_t dest[255];
    mbstowcs(dest,szFileName,255);
    ofn.lpstrFile = dest;
    ofn.nMaxFile = MAX_PATH;
    ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
    ofn.lpstrDefExt = L"txt";
    SendDlgItemMessage(hwnd, IDC_LIST, LB_ADDSTRING, 0, (LPARAM)"Hi there!");
    if(Message == WM_DROPFILES)
    {
        HDROP hDropInfo = (HDROP)wParam;
    char sItem[MAX_PATH];
    for(int i = 0; DragQueryFileA(hDropInfo, i, (LPSTR)sItem, sizeof(sItem)); i++)
    {   
        if(GetFileAttributesA(sItem) &FILE_ATTRIBUTE_DIRECTORY)
        {
            MessageBox(hwnd, L"Attemt to calculate md5 of a directory!", L"Error",
            MB_OK | MB_ICONINFORMATION);
        }
        else
        {
        mbstowcs(dest,sItem,260);
        LoadFileToGetMD5(hwnd,dest);
        }
    }
    }

    switch(Message)
    {

    case WM_COMMAND:
        switch(LOWORD(wParam))
        {
        case ID_FILE_OPEN:
        {
        if(GetOpenFileName(&ofn))
        {
                MessageBox(hwnd, dest, L"Notice",
            MB_OK | MB_ICONINFORMATION);
            LoadFileToGetMD5(hwnd,dest);
        }
        else
        {
            MessageBoxA(hwnd, "File Cannot be opened!", "Error",
            MB_OK | MB_ICONINFORMATION);        
        }
        }
        break;
        case ID_FILE_EXIT:
                PostMessageA(hwnd, WM_CLOSE, 0, 0);
            break;
            case ID_STUFF_ABOUT:
        {
        DialogBox(GetModuleHandle(NULL), 
        MAKEINTRESOURCE(IDD_ABOUT), hwnd, AboutDlgProc);
        }
        break;
    }
        break;
        case WM_CLOSE:
            DestroyWindow(hwnd);
        break;
        case WM_DESTROY:
        PostQuitMessage(0);
        break;
        default:
            return DefWindowProcA(hwnd, Message, wParam, lParam);
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;

    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = 0;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = NULL;
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName  = MAKEINTRESOURCE(ID_MENU);
    wchar_t dest[25];
    mbstowcs(dest,g_szClassName,25);
    wc.lpszClassName = dest;
    wc.hIconSm       = NULL;
    char name[10]="md5sum";
    if(!RegisterClassEx(&wc))
    {
        MessageBoxA(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    hwnd = CreateWindowExA(
        WS_EX_ACCEPTFILES,
    g_szClassName,
        "md5sum",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
        NULL, NULL ,hInstance, NULL);

    if(hwnd == NULL)
    {
        MessageBoxA(NULL, "Window Creation Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        if(!IsDialogMessage(g_hToolbar, &Msg))
    {
            TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    }

    return Msg.wParam;

}

1 个答案:

答案 0 :(得分:0)

你应该在google搜索“winapi中的listview” 顺便说一句,这是代码http://www.win32apicode.com/listview.php