0

I'm wondering if anyone can help me on this since I've been looking around google and chances are I found it but just don't understand how to do it. Basically what this is the routine I would like to perform. I have button 'a' and button 'b', if button 'a' is pressed then open a messagebox. If button 'b' is pressed then open a new window and display content for that window.

Sorry to be so vague so basically, I have 2 buttons on my application. 1 is labelled 'a' and another 'b'. How do I tell my application to run through some kind of loop to check if those buttons are pressed? If the first button is clicked open a message box, if the second is then open another window class. Then following that how would I add new properties to that window class that is opened in the same application?

I was thinking of taking the WM_COMMAND approach in the switch statement, but what are the id's of the CASES and how would they correspond to each button? -- Any help I can get on this problem here is very appreciated, a code example would be EVEN more appreciated!

Cheers

user3251225
  • 147
  • 3
  • 11

1 Answers1

1

In Win32 every button has a numeric ID. It is a good practice to give all your buttons different ids. It is your duty to select the values and assign these numbers to buttons.

Depending on the environment: pure Win32, MFC, something else, the details of defining values and assigning them will be different. You can specify id while creating the window:

HWND WINAPI CreateWindow
(
  _In_opt_  LPCTSTR lpClassName,
  _In_opt_  LPCTSTR lpWindowName,
  _In_      DWORD dwStyle,
  _In_      int x,
  _In_      int y,
  _In_      int nWidth,
  _In_      int nHeight,
  _In_opt_  HWND hWndParent,
  _In_opt_  HMENU hMenu,
  _In_opt_  HINSTANCE hInstance,
  _In_opt_  LPVOID lpParam
);

The id is passed in the hMenu parameter. Look for details in http://msdn.microsoft.com/en-us/library/windows/desktop/ms632679(v=vs.85).aspx. In Windows window with an id cannot have an menu. Sounds funny, but this is how they implemented this in mid 80-ties.

Once you assigned the ids you can and should use them in the WM_COMMAND handler.

Kirill Kobelev
  • 10,252
  • 6
  • 30
  • 51
  • There is no difference between assigning resource IDs using the Windows API and MFC. They are either passed to a call to `CreateWindow` when dynamically creating controls, or specified in the .rc script when using dialog templates, usually aliased by a symbolic preprocessor constant. – IInspectable Jun 08 '14 at 11:46
  • So by assigning the id, do you mean like for example HWND button1 = creatwindow() ... and the id is button1? The variable instance? – user3251225 Jun 08 '14 at 21:59
  • Id is a number, not a name of an identifier. I modified the answer. – Kirill Kobelev Jun 08 '14 at 22:11
  • *"window with an id cannot have an menu"*. You have the logic all backwards. A child window (i.e. a window with the `WS_CHILD` window style) cannot have a menu, and the `hMenu` parameter is reused to mean *child-window identifier* instead. It's not like assigning an ID will prevent a window from having a menu, as you implied. It's the window style that controls how the `hMenu` parameter is interpreted. – IInspectable Jun 09 '14 at 11:36