开发者

Tabular view in Win32 API

开发者 https://www.devze.com 2023-04-09 20:28 出处:网络
I am a newbie in win32 API and I\'m trying to write an app that will calculate the md5sum of a file selected through the menu or dragged and dropped in the window. I want the window to have 3 columns

I am a newbie in win32 API and I'm trying to write an app that will calculate the md5sum of a file selected through the menu or dragged and dropped in the window. I want the window to have 3 columns so that it can display the filename, path and md5sum respectively. Can someone please give me a sample code as to how to make my window look like this?

Also can someone please add the code to create the window as listbox while the menu and drag-drop options are retained?

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.cbWndE开发者_Python百科xtra    = 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;

}


you should search google for "listview in winapi" btw,here is a code http://www.win32apicode.com/listview.php

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号