How to add more than 1 button ? C++

I'm starting in C ++ and I am trying to create a button .. so far so good I created a smooth button but adding more than one? I created one with single hmenu and another with another single hmenu but ends up not working .. only works if I create another CreateWindow ...

Ex:
hButton = CreateWindow(
lpClassName,
caption,
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
x, y,
width, height,
hWnd,
(HMENU) BUTTON_ID,
(HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
NULL
);

I did a 'sub' to create buttons with parameters but when i set the button_id and compile only create one button.. i have checked the button_id used with messagebox and seems to be all right... it is experiencing the unique id normally but the button is not created

Sub Ex:
case WM_CREATE:
AddButton(1, hWnd, L"BUTTON", L"Label", 50, 20, 100, 100);
AddButton(2, hWnd, L"ASDASD", L"123", 50, 20, 100, 100);
AddButton(3, hWnd, L"QWEQWE", L"123", 50, 20, 100, 100);

the first number is the unique id.. but dont work with HMENU

And when I do more than one CreateWindow normally adds
Ex:
hButton = CreateWindow(
"Button1",
"Label",
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
100, 120,
50, 50,
hWnd,
(HMENU) 1,
(HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
NULL
);
hButton = CreateWindow(
"Button2",
"Label",
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
100, 200,
50, 50,
hWnd,
(HMENU) 2,
(HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
NULL
);

Not to say I'm 'leeching', I've looked at various forums and just think tutorial to create only one button.. and what is difficult for everything to be in English

Sorry for the english, i'm brazilian...
Last edited on
closed account (E0p9LyTq)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
   static int cxChar;
   static int cyChar;

   switch (message)
   {
   case WM_CREATE:
      cxChar = LOWORD(GetDialogBaseUnits());
      cyChar = HIWORD(GetDialogBaseUnits());

      CreateWindow("BUTTON", "Button 1",
                   WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
                   cxChar, cyChar * (1 + 2 * 0),
                   20 * cxChar, 7 * cyChar / 4,
                   hwnd, (HMENU) 1,
                   (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE),
                   NULL);

      CreateWindow("BUTTON", "Button 2",
                   WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
                   cxChar, cyChar * (1 + 2 * 1),
                   20 * cxChar, 7 * cyChar / 4,
                   hwnd, (HMENU) 2,
                   (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE),
                   NULL);
      break;

   case WM_COMMAND:
      if (LOWORD(wParam) == 1)
      {
         MessageBox(NULL, "You pressed button 1!", "Button 1", MB_OK);
      }

      if (LOWORD(wParam) == 2)
      {
         MessageBox(NULL, "You pressed button 2!", "Button 2", MB_OK);
      }

      break;

   case WM_DESTROY:
      PostQuitMessage(0);
      return 0;
   }

   return DefWindowProc(hwnd, message, wParam, lParam);
}
Last edited on
I do not think I explained well, what I really is and avoid using 2 create windows but 2 'subs' to call the createwindows to leave the smallest code, sorry.. google translate

I've done the sub the problem is that it only makes the first button and the rest do not

Oh, thx for the WM_COMMAND ! :)

I want to make a system to simplify the creation of the buttons to not use so many createwindows

This is a Example in VB.NET the ..... is the rest of the createwindow abbreviated

Sub Load()
CreateWindow("BUTTON", "Button 1".....)
msgbox("button 1 created!")
CreateWindow("BUTTON", "Button 2".....)
msgbox("button 2 created!")
CreateWindow("BUTTON", "Button 3".....)
msgbox("button 3 created!")
' Etc..
End Sub

####################################
Now the simplified system

Function CreateButton(Byval ButtonID as integer)
if CreateWindow("BUTTON", "Button " & ButtonID &".....) then
msgbox("button " & ButtonID & " created!")
else
msgbox("Error!")
return false
end if
End Function

Sub Load()
CreateButton(1)
CreateButton(2)
CreateButton(3)
' Etc..
End Sub
Last edited on
You simply need to auto-generate control ids. Every time you call CreateWindow() you get a different HWND. You can either save it or disgard it. But you need a unique control id to tell the buttons apart.
Yes, the unique id I know... the problem is how to automatically renew or generate this hWnd
This is completely ridiculous! The Windows Api is C based object oriented programming. In C based object oriented programming there are no constructors like in C++. What you have are functions which create an object, and such an object creation function usually returns a pointer to the object, very similiar to what is returned by C++'s new[]. That is EXACTLY what CreateWindow() or CreateWindowEx() does. It constructs an object and returns a pointer to the object, which to be exact and using correct terminology is an 'opaque' pointer. The Windows Api is handle based.

So when you are talking creating multiple objects with one CreateWindow() or CreateWindowEx() call you are talking utter nonsense. FurryGuy's code is the way it is done, like it or not.
Last edited on
closed account (E0p9LyTq)
Yes, the unique id I know... the problem is how to automatically renew or generate this hWnd

With user created controls you don't need to use or "remember" the CreateWindow() generated handle. You use a unique control ID, cast to an HMENU when creating the control. And that control ID is used to determine which custom control was accessed.

google traduzido:
Com controles de usuário criado você não precisa usar ou "lembrar" o CreateWindow () gerado manusear. Você usa um ID de controle único, convertida em uma HMENU ao criar o controle. E esse ID de controle é usado para determinar qual controle personalizado foi acessado.
I think this might be what you want...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//Main.cpp
#ifndef UNICODE
    #define  UNICODE
#endif
#ifndef _UNICODE
    #define  _UNICODE
#endif
#include <windows.h>
#define IDC_BUTTONS  1500


LRESULT CALLBACK fnWndProc(HWND hwnd, unsigned int msg, WPARAM wParam, LPARAM lParam)
{
 switch(msg)
 {
   case WM_CREATE:
    {
       HINSTANCE hIns=((LPCREATESTRUCT)lParam)->hInstance;
       wchar_t szBuffer[64], szTmp[16];
       for(size_t i=0; i<10; i++)
       {
           wcscpy(szBuffer,L"Program Activity #");
           wsprintf(szTmp,L"%u",i+1),  wcscat(szBuffer,szTmp);
           CreateWindowEx(0,L"button",L"",WS_CHILD|WS_VISIBLE,25,15+30*i,40,25,hwnd,(HMENU)(IDC_BUTTONS+i),hIns,0);
           CreateWindowEx(0,L"static",szBuffer,WS_CHILD|WS_VISIBLE,75,20+30*i,200,25,hwnd,(HMENU)-1,hIns,0);
           memset(szBuffer,0,64);
       }
       return 0;
    }
   case WM_COMMAND:
    {
        if(LOWORD(wParam)>=IDC_BUTTONS && LOWORD(wParam)<IDC_BUTTONS+10)
        {
           if(HIWORD(wParam)==BN_CLICKED)
           {
              int iCtrlId=LOWORD(wParam);
              iCtrlId=iCtrlId-IDC_BUTTONS+1;
              wchar_t szBuffer[64], szTmp[16];
              wcscpy(szBuffer,L"You Want Program Activity #");
              wsprintf(szTmp,L"%d",iCtrlId);
              wcscat(szBuffer,szTmp);
              MessageBox(hwnd,szBuffer,L"Button Click Report",MB_OK);
           }
        }
        return 0;
    }
   case WM_DESTROY:
    {
       PostQuitMessage(0);
       return 0;
    }
 }

 return (DefWindowProc(hwnd, msg, wParam, lParam));
}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevIns, LPSTR lpszArgument, int iShow)
{
 TCHAR szClassName[]=L"Form1";
 WNDCLASSEX wc={};
 MSG messages;
 HWND hWnd;

 wc.lpszClassName = szClassName;
 wc.lpfnWndProc   = fnWndProc;
 wc.cbSize        = sizeof(WNDCLASSEX);
 wc.hbrBackground = (HBRUSH)COLOR_BTNSHADOW;
 wc.hInstance     = hInstance;
 RegisterClassEx(&wc);
 hWnd=CreateWindowEx(0,szClassName,L"Only Three CreateWindowqEx() Calls",WS_OVERLAPPEDWINDOW,150,150,350,360,HWND_DESKTOP,0,hInstance,0);
 ShowWindow(hWnd,iShow);
 while(GetMessage(&messages,NULL,0,0))
 {
    TranslateMessage(&messages);
    DispatchMessage(&messages);
 }

 return messages.wParam;
}


Try running it.
OMG!, explain something in another language sucks, forget it .. thanks to everyone who helped, what I wanted was to recycle the CreateWindow, ex: CreateButton(1) it creates a button with ID 1, then if I wanted to create another button I would use CreateButton(2) and it would create another button with a single id 2 .. not want to create several buttons with a createwindows only but anyway...
Topic archived. No new replies allowed.