2
votes

I'm just starting to learn winapi, and i want to make a simple form with two buttons and a textbox. When i press one button, i want the textbox to say one thing, and when i press the other button i want it to say something else. To differentiate between the two buttons i must know their control ids which are in the loword of wParam on a WM_COMMAND message. I think I am supposed to specify these IDs myself (or does windows do it by default? If so how do i get the ids?) in the hMenu parameter of CreateWindowEx() function. However I cannot find any documentation on the HMENU object or any explanation of how to use it. What is HMENU?

1
It's a handle to a menu. You put whatever id you want as that argument in CreateWindow(Ex).chris

1 Answers

6
votes

HMENU is a handle to a menu, e.g. as created by LoadMenu (which creates a menu from a specification in a resource).

But, the CreateWindow function re-uses the same argument for two different purposes. With a top-level window it's a menu handle, but with a child window it's the child window id, which should be in 16-bit integer range (I'm not sure exactly what range is supported, check the docs).

When creating a child window, just cast the id to HMENU.

A common convention is to use -1 for a "don't care" id. You should better not use 0 for that purpose, because 0 is the id of an OK button, with symbolic name IDOK.

EDIT: The IDOK value is documented (correctly) as 1, in the MessageBox documentation. I'm not sure where the info I relayed above, came from. Still it might be a good idea to avoid 0 as id.


Here's some code illustrating the basic approach. It won't compile on your system because the headers used here are my own. Also, while the code works it's just a kind of skeleton to support other code, to be replaced by more proper code later, so don't place too much weight on design choices etc.

#pragma once
// Copyright (c) 2013 Alf P. Steinbach

#include <rfc/winapi/Atomlike_id.h>                 // Atomlike_id, pseudopointer_from
#include <rfc/winapi/geometry/Rect.h>               // winapi::geometry::Rect
#include <rfc/winapi/gui/general_windowclass.h>     // general_windowclass_atom
#include <rfc/winapi/gui/windowclass_names.h>       // richedit_classname
#include <rfc/winapi/gui/Window_handle_.h>          // Window_handle, Toplevel_window_handle

namespace winapi{ namespace gui{

    inline
    auto new_toplevel_window(
        Toplevel_window_handle const owner = Toplevel_window_handle( nullptr )
        )
        -> Toplevel_window_handle
    {
        HWND const handle   = ::CreateWindow(
            general_windowclass_atom().raw(),
            L"",                // Title
            WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
            CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
            owner,
            HMENU(),
            ::GetModuleHandle( nullptr ),
            nullptr             // param
            );
        hopefully( handle != 0 )
            || fail( "::CreateWindow", ::GetLastError() );
        return Toplevel_window_handle( handle );
    }

    inline
    auto new_child_window(
        Atomlike_id const       windowclass_id,
        Window_handle const     parent,
        geometry::Rect const&   placement       = geometry::Rect(),
        DWORD const             stylebits       = 0,
        int const               id              = -1
        )
        -> Child_window_handle
    {
        if( windowclass_id == Atomlike_id( richedit_classname ) )
        {
            static auto const richedit_dll_name = L"Msftedit.dll";
            if( !GetModuleHandle( richedit_dll_name ) )
            {
                ::LoadLibrary( richedit_dll_name )
                    || fail( "LoadLibrary(\"Msftedit.dll\")", ::GetLastError() );
            }
        }
        auto const& r = placement;
        HWND const handle   = ::CreateWindow(
            windowclass_id.raw(),
            L"",                    // Text
            stylebits | WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS,
            r.x(), r.y(), r.w(), r.h(),         // CW_USEDEFAULT is not valid for child window.
            parent,
            reinterpret_cast<HMENU>( id ),
            ::GetModuleHandle( nullptr ),
            nullptr                 // param
            );
        hopefully( handle != 0 )
            || fail( "::CreateWindow", ::GetLastError() );
        return Child_window_handle( handle );
    }

} }  // namespace winapi::gui