Button

Can I make a functional button that activates a command in c++? I use Code::Blocks and run a windows 8.1 machine. Thanks for the help!
Last edited on
Do you mean sth. like this?
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
#include <windows.h>
#include <tchar.h>
#include <crtdbg.h>

#if defined(UNICODE) || defined(_UNICODE)
#error "Unicode not supported - Please use Multibyte Character Set"
#endif

#define IDC_BUTTON  100

LRESULT CALLBACK WndProc (HWND hwnd, unsigned int msg, WPARAM wParam, LPARAM lParam)
{
  switch (msg)
  {
    case WM_CREATE:
    {
      HINSTANCE hInstance = ((LPCREATESTRUCT)lParam)->hInstance;
      HWND hwndButton = CreateWindow("BUTTON","OK", WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 
                                     10, 10, 75, 35, hwnd, (HMENU)IDC_BUTTON, hInstance, NULL);
      _ASSERTE(IsWindow(hwndButton));
      return 0;
    }
    case WM_COMMAND:
    {
      switch (LOWORD(wParam))
      {
        case IDC_BUTTON:
          ::MessageBox(hwnd, "Button clicked", "Message", MB_OK | MB_ICONINFORMATION);
          break;
      }
      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[] = _T("Template");
  TCHAR szWindowName[] = _T("Template");
  WNDCLASSEX wc = { 0 };
  MSG messages;
  HWND hWnd;

  wc.lpszClassName = szClassName;
  wc.lpfnWndProc = WndProc;
  wc.cbSize = sizeof (WNDCLASSEX);
  wc.hbrBackground = (HBRUSH)COLOR_BTNSHADOW;
  wc.hInstance = hInstance;
  
  _ASSERTE(RegisterClassEx (&wc) !=0);
  
  hWnd = CreateWindowEx (0, szClassName, szWindowName, WS_OVERLAPPEDWINDOW, 
      CW_USEDEFAULT, CW_USEDEFAULT, 450, 300, 
      HWND_DESKTOP, 0, hInstance, 0);
      
  _ASSERTE(::IsWindow(hWnd));
  
  ShowWindow (hWnd, iShow);
  while (GetMessage (&messages, NULL, 0, 0))
  {
    TranslateMessage (&messages);
    DispatchMessage (&messages);
  }

  return static_cast<int>(messages.wParam);
}


Yes, thank you. Is it too much to ask for a commented version of it? I will use it in a school project. Thanks!
I think there is too much to comment or explain. Might be better if you read this tutorials.
http://www.functionx.com/win32/Lesson01.htm
OK thank you very much!
Topic archived. No new replies allowed.