Win32 API

 Generic.h

BOOL InitApplication(HANDLE);

BOOL InitInstance(HANDLE, int);

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);


 

Generic.c

#include 
#include "resource.h"
#include "Generic.h"

HINSTANCE _hInst;
HWND _hWnd;

char _szAppName[] = "Generic";
char _szTitle[] = "Generic Sample Application";

int CALLBACK WinMain( __in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in_opt LPSTR lpCmdLine, __in int nShowCmd )
{
    MSG msg;
    UNREFERENCED_PARAMETER(lpCmdLine);

    if (!hPrevInstance)
    {
        if (!InitApplication(hInstance))
        {
            return FALSE;
        }
    }

    if (!InitInstance(hInstance, nShowCmd))
    {
        return FALSE;
    }

    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam;
}

BOOL InitApplication(HINSTANCE hInstance)
{
    WNDCLASS wc;
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = (WNDPROC)WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon(hInstance, "GeminiIcon");
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = GetStockObject(WHITE_BRUSH);
    wc.lpszMenuName = GenericMenu;
    wc.lpszClassName = _szAppName;

    return RegisterClass(&wc);
}

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
    _hInst = hInstance;
    _hWnd = CreateWindow(
        _szAppName,
        _szTitle,
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        NULL,
        NULL,
        hInstance,
        NULL
        );
    if (!_hWnd)
    {
        return FALSE;
    }

    ShowWindow(_hWnd, nCmdShow);
    UpdateWindow(_hWnd);
    return TRUE;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int wmId, wmEvent;
    switch (message)
    {
    case WM_COMMAND:
        wmId = LOWORD(wParam);
        wmEvent = HIWORD(wParam);

        switch (wmId)
        {
        case IDM_ABOUT:
            DialogBox(_hInst, "AboutBox", hWnd, (DLGPROC)About);
            break;
        case IDM_EXIT:
            DestroyWindow(hWnd);
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
        }
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return TRUE;
}

LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);
    switch (message)
    {
    case WM_INITDIALOG:
        return TRUE;
    case WM_COMMAND:
        if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
        {
            EndDialog(hDlg, TRUE);
            return TRUE;
        }
        break;
    }
    return FALSE;
}


 

Generic.rc

#include 
#include "resource.h"
GeminiIcon ICON DISCARDABLE "Gemini.ico"

GenericMenu MENU DISCARDABLE
BEGIN
    POPUP "&File"
    BEGIN
        MENUITEM    "&New",      IDM_NEW, GRAYED
        MENUITEM    "&Open",     IDM_OPEN, GRAYED
        MENUITEM    "&Save",      IDM_SAVE, GRAYED
        MENUITEM    SEPARATOR
        MENUITEM    "E&xit",        IDM_EXIT
    END
    POPUP "&Help"
    BEGIN
        MENUITEM    "&Contents",               IDM_HELPCONTENTS,  GRAYED    
        MENUITEM    SEPARATOR
        MENUITEM    "&About Generic...",     IDM_LINKS,  GRAYED
    END
END

AboutBox DIALOG DISCARDABLE 22,17,144,75
STYLE DS_MODALFRAME | WS_CAPTION | WS_SYSMENU
CAPTION "About Generic"
BEGIN
    CTEXT                      "Window XP",       -1, 0, 5, 144, 8
    CTEXT                      "Generic Application"    -1, 0, 34, 144, 8
    DEFPUSHBUTTON     "OK",                    IDOK, 53,59,32,14,WS_GROUP
END


resource.h

#define    IDM_NEW    101
#define    IDM_OPEN   102
#define    IDM_SAVE    103
#define    IDM_EXIT     104
#define    IDM_ABOUT 105

#define    IDM_HELPCONTENTS 106
#define    IDM_LINKS 107

#define    GeminiIcon   1001
#define    GenericMenu    1002
#define    AboutBox     1003


 

你可能感兴趣的:(MFC,api,callback,null,command,application,dialog)