WINAPI Buttons and Shapes

Why doesn't this work? i am trying to make the button make the circle red.

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
 #include <windows.h>
#include "main.h"

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);                                           //Declaring the windows Procedure Function that handle the events

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int nShowCmd)
{
    static char name[] = "My Window Class";                                                     //Declaring the Windows Class Name
    WNDCLASSEX wc;                                                                              //Declaring the Windows Class Structure
    HWND hwnd;                                                                                  //Declaring the Main Window Handle
    MSG Msg;                                                                                    //Declaring the Message Structure

    //Step 1: Registering the Window Class
    wc.cbSize        = sizeof(WNDCLASSEX);                                                      //The size of the Windows class always set it to sizeof(WNDCLASSEX);
    wc.style         = CS_HREDRAW | CS_VREDRAW;                                                 //The Windows style, this is how it will look.
    wc.lpfnWndProc   = WndProc;                                                                 //Needs a pointer to WndProc
    wc.cbClsExtra    = 0;                                                                       //How many extra bytes to assign to the Class and the Window
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;                                                               //The instance handle being passed to WinMain
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);                                         //A handle to the application icon
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);                                             //A handle to the cursor the program will use
    wc.hbrBackground = (HBRUSH) CreateSolidBrush(RGB(0, 0, 0));                                 //The colour of the Window background
    wc.lpszMenuName  = NULL;                                                                    //A handle t the menu, havn't used one so set to 0
    wc.lpszClassName = name;                                                                    //The declared class name
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);                                         //A handle to the small top left icon

    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Registration Failure", MB_ICONEXCLAMATION | MB_OK);    //Registering the class, will display a message if it fails
        return 0;
    }

    // Step 2: Creating the Window //This will return the handle of the main window    (TheExtendedWindowsStyle, ClassName, WindowName, WindowStyle, XWindowPos, YWindowPos, WindowWidth, WindowHeight, ParentWindowHandle, MenuHandle, InstanceHandle, WM_CREATEPointer)
    hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, name, "Practice Window", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 400, 400, NULL, NULL, hInstance, NULL);

    ShowWindow(hwnd, nShowCmd);                                                                 //Display the window
    UpdateWindow(hwnd);                                                                         //Handles messages sent by Windows to my program

    // Step 3: The Message Loop
    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return (int)Msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    int x = 0;
    int y = 0;
    int z = 0;

    switch(msg)
    {

    case WM_CREATE:
        hCloseBTN = CreateWindowEx(WS_EX_WINDOWEDGE, "BUTTON", "Colour",
        WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 20, 50, 80, 30, hwnd, NULL,
        GetModuleHandle(NULL), NULL);
        break;
    case WM_COMMAND:

    switch(LOWORD(wParam))
    {
    case BN_CLICKED:
        if(hCloseBTN == (HWND)lParam)
        {
            if(hCloseBTN == (HWND)lParam)
            {
                x = 255;
            }
        }
    }
    break;

    case WM_PAINT:
        {
            HDC hdc;
            PAINTSTRUCT ps;
            hdc = BeginPaint(hwnd, &ps);

            HGDIOBJ original = NULL;
            original = SelectObject(hdc,GetStockObject(DC_BRUSH));
            SelectObject(hdc, GetStockObject(DC_BRUSH));
            SetDCBrushColor(hdc, RGB(x,y,z));

            Ellipse(hdc, 100, 100, 160, 160);

            EndPaint(hwnd, &ps);
        }
        break;

    case WM_CLOSE:
        DestroyWindow(hwnd);
        break;

    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    }

    return DefWindowProc(hwnd, msg, wParam, lParam);
}
Last edited on
The first reason is that x,y,z are local variables. You get one msg per call. Hence x,y,z is always 0 when msg -> WM_PAINT. Solution: Make the variables global.

The second problem is that you need to invalidate the rect/ellipse that you want to redraw:

http://msdn.microsoft.com/en-us/library/windows/desktop/dd145002%28v=vs.85%29.aspx

Otherwise you might not get a WM_PAINT msg.
Thankyou very much i have managed to make that work, Would you happen to know how to check for more than one button click?

For context i tried to change the;
x = 255;
into
x =+ 10;

So it would become more red the more it was clicked, but it only seemes to do the first click, thanks again.
Bump?

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
#include <windows.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);                                           //Declaring the windows Procedure Function that handle the events

    int x = 0;
    int y = 0;
    int z = 0;

    HWND hCloseBTN;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int nShowCmd)
{



    static char name[] = "My Window Class";                                                     //Declaring the Windows Class Name
    WNDCLASSEX wc;                                                                              //Declaring the Windows Class Structure
    HWND hwnd;                                                                                  //Declaring the Main Window Handle
    MSG Msg;                                                                                    //Declaring the Message Structure

    //Step 1: Registering the Window Class
    wc.cbSize        = sizeof(WNDCLASSEX);                                                      //The size of the Windows class always set it to sizeof(WNDCLASSEX);
    wc.style         = CS_HREDRAW | CS_VREDRAW;                                                 //The Windows style, this is how it will look.
    wc.lpfnWndProc   = WndProc;                                                                 //Needs a pointer to WndProc
    wc.cbClsExtra    = 0;                                                                       //How many extra bytes to assign to the Class and the Window
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;                                                               //The instance handle being passed to WinMain
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);                                         //A handle to the application icon
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);                                             //A handle to the cursor the program will use
    wc.hbrBackground = (HBRUSH) CreateSolidBrush(RGB(0, 0, 0));                                 //The colour of the Window background
    wc.lpszMenuName  = NULL;                                                                    //A handle t the menu, havn't used one so set to 0
    wc.lpszClassName = name;                                                                    //The declared class name
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);                                         //A handle to the small top left icon

    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Registration Failure", MB_ICONEXCLAMATION | MB_OK);    //Registering the class, will display a message if it fails
        return 0;
    }

    // Step 2: Creating the Window //This will return the handle of the main window    (TheExtendedWindowsStyle, ClassName, WindowName, WindowStyle, XWindowPos, YWindowPos, WindowWidth, WindowHeight, ParentWindowHandle, MenuHandle, InstanceHandle, WM_CREATEPointer)
    hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, name, "Practice Window", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 400, 400, NULL, NULL, hInstance, NULL);

    ShowWindow(hwnd, nShowCmd);                                                                 //Display the window
    UpdateWindow(hwnd);                                                                         //Handles messages sent by Windows to my program

    // Step 3: The Message Loop
    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return (int)Msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{

    switch(msg)
    {

    case WM_CREATE:
        hCloseBTN = CreateWindowEx(WS_EX_WINDOWEDGE, "BUTTON", "Colour",
        WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 20, 50, 80, 30, hwnd, NULL,
        GetModuleHandle(NULL), NULL);
        break;
    case WM_COMMAND:

    switch(LOWORD(wParam))
    {
    case BN_CLICKED:
        if(hCloseBTN == (HWND)lParam)
        {
            if(hCloseBTN == (HWND)lParam)
            {
                x =+ 10;
                InvalidateRect(hwnd, NULL, TRUE);

            }
        }
    }
    break;

    case WM_PAINT:
        {
            HDC hdc;
            PAINTSTRUCT ps;
            hdc = BeginPaint(hwnd, &ps);

            HGDIOBJ original = NULL;
            original = SelectObject(hdc,GetStockObject(DC_BRUSH));
            SelectObject(hdc, GetStockObject(DC_BRUSH));
            SetDCBrushColor(hdc, RGB(x,y,z));

            Ellipse(hdc, 100, 100, 160, 160);

            EndPaint(hwnd, &ps);
        }
        break;

    case WM_CLOSE:
        DestroyWindow(hwnd);
        break;

    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    }

    return DefWindowProc(hwnd, msg, wParam, lParam);
}
Well: It is x += 10; // Note: += not =+
Idiocy strikes again, thankyou!
How can i use a button to change the shape? because this doesn't work.
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <windows.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);                                           //Declaring the windows Procedure Function that handle the events

    int x = 0;
    int y = 0;
    int z = 0;
    int s = 0;

    HWND hRedBTN;
    HWND hBlueBTN;
    HWND hGreenBTN;
    HWND hBlackBTN;
    HWND hShapeBTN;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int nShowCmd)
{
    static char name[] = "My Window Class";                                                     //Declaring the Windows Class Name
    WNDCLASSEX wc;                                                                              //Declaring the Windows Class Structure
    HWND hwnd;                                                                                  //Declaring the Main Window Handle
    MSG Msg;                                                                                    //Declaring the Message Structure

    //Step 1: Registering the Window Class
    wc.cbSize        = sizeof(WNDCLASSEX);                                                      //The size of the Windows class always set it to sizeof(WNDCLASSEX);
    wc.style         = CS_HREDRAW | CS_VREDRAW;                                                 //The Windows style, this is how it will look.
    wc.lpfnWndProc   = WndProc;                                                                 //Needs a pointer to WndProc
    wc.cbClsExtra    = 0;                                                                       //How many extra bytes to assign to the Class and the Window
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;                                                               //The instance handle being passed to WinMain
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);                                         //A handle to the application icon
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);                                             //A handle to the cursor the program will use
    wc.hbrBackground = (HBRUSH) CreateSolidBrush(RGB(255, 255, 255));                                 //The colour of the Window background
    wc.lpszMenuName  = NULL;                                                                    //A handle t the menu, havn't used one so set to 0
    wc.lpszClassName = name;                                                                    //The declared class name
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);                                         //A handle to the small top left icon

    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Registration Failure", MB_ICONEXCLAMATION | MB_OK);    //Registering the class, will display a message if it fails
        return 0;
    }

    // Step 2: Creating the Window //This will return the handle of the main window    (TheExtendedWindowsStyle, ClassName, WindowName, WindowStyle, XWindowPos, YWindowPos, WindowWidth, WindowHeight, ParentWindowHandle, MenuHandle, InstanceHandle, WM_CREATEPointer)
    hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, name, "Practice Window", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 460, 400, NULL, NULL, hInstance, NULL);

    ShowWindow(hwnd, nShowCmd);                                                                 //Display the window
    UpdateWindow(hwnd);                                                                         //Handles messages sent by Windows to my program

    // Step 3: The Message Loop
    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return (int)Msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{

    switch(msg)
    {

    case WM_CREATE:

        hRedBTN = CreateWindowEx(WS_EX_WINDOWEDGE, "BUTTON", "R + 10",
        WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 20, 50, 80, 30, hwnd, NULL,
        GetModuleHandle(NULL), NULL);

        hBlueBTN = CreateWindowEx(WS_EX_WINDOWEDGE, "BUTTON", "B + 10",
        WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 100, 50, 80, 30, hwnd, NULL,
        GetModuleHandle(NULL), NULL);

        hGreenBTN = CreateWindowEx(WS_EX_WINDOWEDGE, "BUTTON", "G + 10",
        WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 180, 50, 80, 30, hwnd, NULL,
        GetModuleHandle(NULL), NULL);

        hBlackBTN = CreateWindowEx(WS_EX_WINDOWEDGE, "BUTTON", "RGB, 0, 0, 0",
        WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 260, 50, 80, 30, hwnd, NULL,
        GetModuleHandle(NULL), NULL);

        hShapeBTN = CreateWindowEx(WS_EX_WINDOWEDGE, "BUTTON", "Shape",
        WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 340, 50, 80, 30, hwnd, NULL,
        GetModuleHandle(NULL), NULL);

        break;

    case WM_COMMAND:

    switch(LOWORD(wParam))
    {
    case BN_CLICKED:
        if(hRedBTN == (HWND)lParam)
        {
            if(hRedBTN == (HWND)lParam)
            {
                x += 10;
                if(x > 255){
                    x = 0;
                }
            InvalidateRect(hwnd, NULL, TRUE);
            }
        }
        if(hBlueBTN == (HWND)lParam)
        {
            if(hBlueBTN == (HWND)lParam)
            {
                z += 10;
                if(z > 255){
                    z = 0;
                }
            InvalidateRect(hwnd, NULL, TRUE);
            }
        }
        if(hGreenBTN == (HWND)lParam)
        {
            if(hGreenBTN == (HWND)lParam)
            {
                y += 10;
                if(y > 255){
                    y = 0;
                }
            InvalidateRect(hwnd, NULL, TRUE);
            }
        }
        if(hBlackBTN == (HWND)lParam)
        {
            if(hBlackBTN == (HWND)lParam)
                {
                    z = 0;
                    y = 0;
                    x = 0;
                }
                InvalidateRect(hwnd, NULL, TRUE);
        }
        if(hShapeBTN == (HWND)lParam)
        {
            if(hShapeBTN == (HWND)lParam)
                {
                    if(s = 1){
                        s -= 1;}
                    else{
                    s += 1;
                    }

                }
                InvalidateRect(hwnd, NULL, TRUE);
        }

    }
    break;

    case WM_PAINT:
        {
            HDC hdc;
            PAINTSTRUCT ps;
            hdc = BeginPaint(hwnd, &ps);

            HGDIOBJ original = NULL;
            original = SelectObject(hdc,GetStockObject(DC_BRUSH));
            SelectObject(hdc, GetStockObject(DC_BRUSH));
            SetDCBrushColor(hdc, RGB(x,y,z));

            if(s = 0){
            Ellipse(hdc, 100, 100, 160, 160);
            }
            else{
            Rectangle(hdc, 100, 100, 160, 160);
            }

            EndPaint(hwnd, &ps);
        }
        break;

    case WM_CLOSE:
        DestroyWindow(hwnd);
        break;

    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    }

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


Also, why am i getting a warning for the OR staments, i googled it and its somthing to do with the difference between = and == but i'm afraid i don't understand it.
"Suggest parentheses around assignment used as truth value"
"Suggest parentheses around assignment used as truth value"


check line 140
Thanks, just for anyone witha similar problem, = is the assignment opereator I.E. s = 1; and == is the equality operator, so if(s == 1){}. Hint, dont use = in if's.
Thanks, just for anyone witha similar problem, = is the assignment opereator I.E. s = 1; and == is the equality operator, so if(s == 1){}. Hint, dont use = in if's.
Yes, to avoid problem with this put the constant value on the left side, i.e. if(1 == s)
Topic archived. No new replies allowed.