[WINAPI] WM_COMMAND, unterscheiden von Menu, Accelerator und Control Command

FBIagent

Erfahrenes Mitglied
Guten tag,

ich habe ein kleines Problem mit der WM_COMMAND window Message. Und zwar
habe ich sonst immer geprüft ob der LPARAM der WndProc NULL ist damit ich
weis das entweder bei NULL ein Menu oder Accelerator Command vorliegt und bei
!= NULL ein Control Command. Nun habe ich mir allerdings nocheinmal die MSDN
durchgelesen und bin zu dem Schluss gekommen, dass ich dies mittels des
Notification Codes( HIWORD( wParam ) ) prüfen möchte. Nur wie hänge ich ein
notification code an einen Beispielsweise Button der mit CreateWidnowEx erstellt wird?

C++:
class MyWindow
: public GoGUI::Window {
public:
.............................................
    bool OnCreate( HWND hWnd, LPCREATESTRUCT CreateStruct, LRESULT &ProcRet ) {
        HWND Btn = CreateWindowEx( 0, L"BUTTON", L"MyButton", WS_CHILD | WS_VISIBLE | BS_FLAT, 50, 50, 100, 25, hWnd, NULL, GetWindowClassEx()->hInstance, NULL );

        if ( Btn == NULL ) {
            GoGUI::Exception e( GoGUI::ET_WINAPI, GetLastError(), L"", L"GoGUI.cpp" );
            MessageBox( GetWindowHandle(), e.GetErrorText().c_str(), L"Button Creation Failed", MB_OK );
        }

        return false;
    }

    bool OnCommand( DWORD CommandType, DWORD Identifier, HWND CommandWindow ) {
        switch ( CommandType ) {
        case 0:
            MessageBox( GetWindowHandle(), L"Menu", L"OnCommand", MB_OK );

            if ( Identifier == 4 ) {
                MessageBox( GetWindowHandle(), L"Quit", L"Menu", MB_OK );
                PostQuitMessage( 0 );
            }
            break;
        case 1:
            MessageBox( GetWindowHandle(), L"Accelerator", L"OnCommand", MB_OK );
            break;
        default:
            MessageBox( GetWindowHandle(), L"Control", L"OnCommand", MB_OK );
            break;
        }

        return false;
    }
private:
...............................................
};


LRESULT CALLBACK Window::GoGUIWindowProc( HWND hWnd, UINT WndMsg, WPARAM wParam, LPARAM lParam ) {
        bool MessageProcessed = false;
        LRESULT ProcRet = 0;

        switch ( WndMsg ) {
...........................................
        case WM_CREATE:
            MessageProcessed = g_Windows[ hWnd ]->OnCreate( hWnd, ( LPCREATESTRUCT )lParam, ProcRet );
            break;
        case WM_COMMAND:
            MessageProcessed = g_Windows[ hWnd ]->OnCommand( HIWORD( wParam ), LOWORD( wParam ), ( HWND )lParam );
            break;
.....................................................................
        case WM_NCCREATE:
            g_Windows[ hWnd ] = ( GoGUI::Window* )( ( LPCREATESTRUCT )lParam )->lpCreateParams;
            MessageProcessed = g_Windows[ hWnd ]->OnNcCreate( ( LPCREATESTRUCT )lParam, ProcRet );
            break;
.....................................................................
        }

        if ( MessageProcessed ) {
            return ProcRet;
        }

        return DefWindowProc( hWnd, WndMsg, wParam, lParam );
    }

__________ main ____________
INT WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd ) {
    try {
        MyWindow mw( hInstance );

        HMENU hMenu = CreateMenu();
        HMENU hMenuFile = CreatePopupMenu();
        HMENU hMenuHelp = CreatePopupMenu();

        AppendMenu( hMenu, MF_STRING | MF_POPUP, ( UINT_PTR )hMenuFile, L"File" );
        AppendMenu( hMenu, MF_STRING | MF_POPUP, ( UINT_PTR )hMenuHelp, L"Help" );

        AppendMenu( hMenuFile, MF_STRING, 1, L"Open" );
        AppendMenu( hMenuFile, MF_STRING, 2, L"Close" );
        AppendMenu( hMenuFile, MF_SEPARATOR, 3, L"" );
        AppendMenu( hMenuFile, MF_STRING, 4, L"Quit" );
        AppendMenu( hMenuHelp, MF_STRING, 5, L"About..." );

        mw.Create( 0, L"GoGUI", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 55, 55, 800, 600, NULL, hMenu );

        DWORD dwRet = 0;

        if ( ( dwRet = MessageLoop() ) != 0UL ) {
            throw GoGUI::Exception( GoGUI::ET_WINAPI, dwRet, L"", L"main.cpp" );
        }
    } catch ( GoGUI::Exception &e ) {
        MessageBox( NULL, e.GetErrorText().c_str(), L"GoGUI::Exception", MB_OK );
        return FALSE;
    }

    return TRUE;
}

Ich hoffe ich hab nicht zu viel abgeschnitten :)

Best wishes
FBIagent
 

Neue Beiträge

Zurück