windows_programming_notes.nbk: Home | Index | Next Page: Windows Style Constants | Previous Page: windows destruction


 windows message decoding

The operating system communicates and controls each window by sending it a barrage of messages.

If you create a window of your own, you implement it's functionality by responding to the appropriate messages. This starts with a message handling function that is just a giant switch statement.

LRESULT CALLBACK MainWndWndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
{

    switch (message) {
    case WM_COMMAND:        return HANDLE_WM_COMMAND(hwnd, wparam, lparam, MainWndOnCommand);
    case WM_CREATE:
        MainWndOnCreate(hwnd);
        return 0;
    case WM_DESTROY:
        PostQuitMessage(0);    //signal end of application
        return 0;
    case WM_SIZE:           return HANDLE_WM_SIZE(hwnd, wparam, lparam, MainWndOnSize);
    default:
        return DefWindowProc(hwnd,message,wparam,lparam);  //let system deal with msg
    }
}

Any messages you don't want to respond to should be sent to DefWindowProc, which supplies the default windows response to the message.

The windowsx.h header file provides "message crackers" that convert and translate the message arguments into the appropriate parts. In the example above, the HANDLE_WM_COMMAND message cracker is used to generate a call to a function which then handles the WM_COMMAND message:

void MainWndOnCommand(HWND hwnd, int id, HWND hwnd_control, UINT notify_code)
{
    if (hwnd_control) {
    }
    else {
        switch (id) {
        case IDM_ONE:   
            strcpy(buffer, "one ");
            CopyStuffIn(SF_TEXT, SFF_SELECTION);
            break;
        case IDM_TWO:   
            strcpy(buffer, "two ");
            CopyStuffIn(SF_TEXT, SFF_SELECTION);
            break;
        case IDM_THREE: 
            strcpy(buffer, "three ");
            CopyStuffIn(SF_TEXT, SFF_SELECTION);
            break;
        case IDM_RTF:
            strcpy(buffer, rtfPrefix);
            strcat(buffer, rtfPostfix);
            CopyStuffIn(SF_RTF, 0);
            break;
        case IDM_STUFF:
            strcpy(buffer, stuff);
            CopyStuffIn(SF_RTF, 0);
            break;
        case IDM_BLACK:
            SetColor(255, 255, 255);
            break;
        case IDM_BLUE:
            SetColor(0, 0, 255);
            break;
        case IDM_RED:
            SetColor(255, 0, 0);
            break;
        case IDM_NORMAL:
            SetFont(CFM_BOLD | CFM_ITALIC, 0);
            break;
        case IDM_BOLD:
            SetFont(CFM_BOLD, CFE_BOLD);
            break;
        case IDM_ITALIC:
            SetFont(CFM_ITALIC, CFE_ITALIC);
            break;
        case IDM_STRIKE:
            SetFont(CFM_STRIKEOUT, CFE_STRIKEOUT);
            break;
        case IDM_DISABLE:
            SetFont(CFM_DISABLED, CFE_DISABLED);
            break;
        case IDM_UNDER:
            SetFont(CFM_UNDERLINE, CFE_UNDERLINE);
            break;
        }
    }
}

windows_programming_notes.nbk: Home | Index | Next Page: Windows Style Constants | Previous Page: windows destruction


Notebook exported on Monday, 7 July 2008, 18:56:50 PM Eastern Daylight Time