Windows GUI Button Usage And Programming

I am using MVC 2010 Express and have just begun exploring the use of a GUI for my programs. I've been able to create, within the program, a window that is as I would like it but I don't see how I'm supposed to take a program I've already written in C++ and combine it with this window. Its in the same project, but I mean in terms of its functionality. How do I write my code so that the pressing of a button does the equivalent of being seen as "a++" or "a = 1" or something like that. How do I utilize these stinkin' buttons and my code associated with the window/buttons/etc? I've been searching the internet for the info but have been unsuccessful in finding information that 1, clearly explains that, or 2, that I can even begin to understand.

1
2
3
4
5
6
7
8
9
10
// 
// button1
// 
this->button1->Location = System::Drawing::Point(23, 37);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(190, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"Do #1";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &main::button1_Click);


That is an example of one of my buttons in the "main.h" file for the project, but, again, I don't see how to utilize it.
Last edited on
Thats not C++!!!
That is C++/CLI. Not many of the regulars here know this variation of C++. I recommend that you post your C++/CLI questions @ the MSDN Forums instead. You'll get much faster replies.
1
2
3
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
     // Your code here
}


I recommend the Win32 API for C++ and GUI programming beginners. I don't recommend Windows Frameworks, CLI and above all, Microsoft sh*t because the apps run slower, are not portable and anybody can deobfuscate their code and get even the comments.
Last edited on
??? If it is for Windows, why do you care that is portable or not?

And I don't see why Microsoft frameworks are sh+t to you. They are perfectly fine frameworks. ATL is a beauty for COM programming.

I do am reluctant to C++/CLI because I don't see much benefit in it, but other than that I'm pretty happy with MS Frameworks, much like most of the world.
The humble opinion of a beginner. Indeed, you can create nice applications with original design. quickly and easily. You have some flexibility and freedom but only "inside the borders". Rebellious people like me love to create applications the most platform-independent possible.
I am using the Win32 API for C++ in MVC 2010 Express. I just don't see where/how (whether in code or in their GUI for creating the GUI) to associate buttons with commands etc.
Below is a piece of code who get use of 1 button in Win32 GUI. Hopes that get you going.

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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <windows.h>
#include <string.h>

// Make global definitions

UINT Timer_ID;
HWND hWnd;
HDC hdc;
PAINTSTRUCT ppaint;

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

#define ID_BUTTON   102

static int   iXpos;
static int   iYpos;
       char  szMessage[100];

static char gszClassName[]  = "My Program";
static HINSTANCE ghInstance = NULL;

/*****************************************************************************/
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {

    WNDCLASSEX WndClass;
    MSG Msg;

    ghInstance = hInstance;

    WndClass.cbSize        = sizeof(WNDCLASSEX);
    WndClass.style         = NULL;
    WndClass.lpfnWndProc   = WndProc;
    WndClass.cbClsExtra    = 0;
    WndClass.cbWndExtra    = 0;
    WndClass.hInstance     = ghInstance;
    WndClass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    WndClass.hCursor       = LoadCursor(NULL, IDC_ARROW);
    WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    WndClass.lpszMenuName  = NULL;
    WndClass.lpszClassName = gszClassName;
    WndClass.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

    if(!RegisterClassEx(&WndClass)) {
        MessageBox(0, "Window Registration Failed!", "Error!", MB_ICONSTOP | MB_OK);
        return 0;
    }

    hWnd = CreateWindowEx(
        WS_EX_STATICEDGE,
        gszClassName,
        " My Program",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT,
        320, 240,
        NULL, NULL,
        ghInstance,
        NULL);


    if(hWnd == NULL) {
        MessageBox(0, "Window Creation Failed!", "Error!", MB_ICONSTOP | MB_OK);
        return 0;
    }

    strcpy(szMessage, "Welcome to My Window");
    iXpos = 70;
    iYpos = 10;
    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);

    while(GetMessage(&Msg, NULL, 0, 0)) {
            TranslateMessage(&Msg);
            DispatchMessage(&Msg);
    }
    return Msg.wParam;
}

/*****************************************************************************/
void WriteText(int _Xpos, int _Ypos, char *_szMessage)
    {
        hdc = BeginPaint(hWnd, &ppaint);
        // Here you can set the color of the string
        //SetTextColor(hdc, RGB(0,0,0));
        TextOut(hdc, _Xpos, _Ypos, _szMessage, strlen(_szMessage));
        EndPaint(hWnd, &ppaint);
    }

/*****************************************************************************/
LRESULT CALLBACK WndProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam) {

    HWND hWndButton;

    switch(Message) {
        case WM_PAINT:
            WriteText(iXpos, iYpos, szMessage);
            break;

        case WM_TIMER:
            break;

        case WM_CLOSE:
            DestroyWindow(hWnd);
            break;

        case WM_DESTROY:
            PostQuitMessage(0);
            break;

        case WM_CREATE:{
            hWndButton = CreateWindowEx(0,          /* more or ''extended'' styles */
            TEXT("BUTTON"),                         /* GUI ''class'' to create */
            TEXT("Run "),                           /* GUI caption */
            WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUTTON,   /* control styles separated by | */
            260,                                    /* LEFT POSITION (Position from left) */
            185,                                    /* TOP POSITION  (Position from Top) */
            47,                                     /* WIDTH OF CONTROL */
            25,                                     /* HEIGHT OF CONTROL */
            hWnd,                                   /* Parent window handle */
            (HMENU)ID_BUTTON,                       /* control''s ID for WM_COMMAND */
            ghInstance,                             /* application instance */
            NULL);
            break;
        }

        case WM_COMMAND:{
            if(((HWND)lParam) && (HIWORD(wParam) == BN_CLICKED)){
                int iMID;
                iMID = LOWORD(wParam);
                switch(iMID){
                   // Button clicked
                    case ID_BUTTON:{
                        // Notice this is a "C" call in string .h
                        strcpy(szMessage, "*** This is my program ***");
                        iXpos = 10;
                        iYpos = 40;
                        // Write string in window by sending message WM_PAINT
                        InvalidateRect(hWnd, NULL, NULL);
                        break;
                    }
                    default:
                    break;
                }
            }
            break;
        }
        default:
        return DefWindowProc(hWnd, Message, wParam, lParam);
    }
    return 0;
}
that code didn't compile for me it said WndClass.lpszClassName = gszClassName
cannot convert from char[11] to LPCWSTR
Add this to the top or disable Unicode from project settings.

1
2
#undef UNICODE
#undef _UNICODE 
now I'm getting this:


1>mt.exe : general error c101008a: Failed to save the updated manifest to the file "Debug\winAPI 2 solution.exe.embed.manifest". The parameter is incorrect.
The program was created and compiled under Borland C++ ver 5.03. I'm downloading Visual Studio C++ 10 Express to see what is going on.
I get the same errors in VSC++. I thing the way to do it is to create a new project in VSC++. That brings up a template for a window and the copy the needed case calls into that project and recompile it. That should work. VSC++ is very different from Borland but general code is the same. Hope that helps.
Per
Topic archived. No new replies allowed.