API, redrawing a window after a mouse click

EDIT: Solved this by using...


case WM_LBUTTONUP:
InvalidateRect(hwnd,NULL,TRUE);
break;


----------------------------------------------

So lately I've been playing around with API trying to learn how to make windows and applications etc. Have a Mandelbrot set creator in which I'm trying to put in a zoom feature by redrawing the set based on where you left click.

Order of operations in this is...

You choose the x and y limits.
WM_PAINT draws a Mandelbrot set based on these limits (e.g. might only draw part of the set).
At this point, if you click somewhere in the window, the x and y limits are re-calculated and whatever part of the Mandelbrot set that is contained within these limits is re-drawn in the window.

At least that's what should happen...

Everything works up to the re-drawing part. At this point I have to move the window of my laptop screen and back on to get it to redraw the set (though the correct thing is then drawn I should say).
I've tried using UpdateWindow and RedrawWindow throughout the code and it hasn't worked (none of the two are in the code below though). What I would like to happen is for the window to be redrawn after I click, without me having to move it somewhere or anything else. Below is the code, bit long but you can likely skip to line 93 for the switch statement.

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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include <iostream>
#include <windows.h>
#include <cmath>

using std::cout;
using std::cin;
using std::endl;

const int window_size = 500;

double x_min, x_max, y_min, y_max,
       x_min0, x_max0, y_min0, y_max0,
       x_scale, y_scale;

double Abs (double input)
{
    if (input < 0)
    {
        return -input;
    }
    return input;
}

LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam );
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow );

int WINAPI WinMain( HINSTANCE hInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR szCmdLine,
                    int iCmdShow )
{
    #pragma region part 1 - STARTUP STUFF
    WNDCLASS wc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hbrBackground = (HBRUSH)GetStockObject( WHITE_BRUSH );
    wc.hCursor = LoadCursor( NULL, IDC_ARROW );
    wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
    wc.hInstance = hInstance;
    wc.lpfnWndProc = WndProc;
    wc.lpszClassName = TEXT("B");

    wc.lpszMenuName = 0;
    wc.style = CS_HREDRAW | CS_VREDRAW;

    RegisterClass( &wc );

    HWND hwnd = CreateWindow
    (
        TEXT("B"), TEXT("Graph test"),
        WS_OVERLAPPEDWINDOW,            // Style
        700, 40,                        // Starting point
        window_size, window_size,       // Dimensions
        NULL,
        NULL,
        hInstance,
        NULL
    );

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

    ShowWindow(hwnd, iCmdShow );
    UpdateWindow(hwnd);
    #pragma endregion

    #pragma region part 2 - ENTER A LOOP TO CONTINUALLY KEEP CHECKING WITH WIN O/S FOR USER INTERACTION

    MSG msg;

    while( GetMessage( &msg, NULL, 0, 0 ) )
    {
        TranslateMessage( &msg );
        DispatchMessage( &msg );
    }
    #pragma endregion

    return msg.wParam;
}

LRESULT CALLBACK WndProc(   HWND hwnd,
                            UINT message,
                            WPARAM wparam,
                            LPARAM lparam )
{
    HDC hdc;
    PAINTSTRUCT ps;

    switch( message )
    {
    case WM_CREATE:

        cout << "Enter limits, x_min, x_max, y_min, y_max:" << endl;
        cout << "x_min: ";
        cin >> x_min;
        if (x_min < -3 || x_min >= 2)
        {
            x_min = x_min0 = -2;
            x_max = x_max0 = 1;

            y_min = y_min0 = -1.5;
            y_max = y_max0 = 1.5;
        }
        else
        {
            cout << "x_max: ";
            cin >> x_min;
            cout << "y_min: ";
            cin >> y_min;
            cout << "y_max: ";
            cin >> y_max;

            x_min0 = x_min;
            x_max0 = x_max;

            y_min0 = y_min;
            y_max0 = y_max;
        }
        break;

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

        const int top_left_x = 0;
        const int top_left_y = 0;
        const int bottom_right_x = window_size;
        const int bottom_right_y = window_size;

        x_scale = x_max - x_min;
        y_scale = y_max - y_min;

        const int width = bottom_right_x - top_left_x;
        const int height = bottom_right_y - top_left_y;
        const double step = x_scale/width;

        for (double x = x_min; x <= x_max; x += step)
        {
            for (double y = y_min; y <= y_max; y += step)
            {
                double X = x;
                double Y = y;
                //bool check = true;
                float number_of_iterations = 0;
                for (int i = 0; i < 100; i++)
                {
                    double X_temp = X;
                    X = X*X - Y*Y + x;
                    Y = 2*X_temp*Y + y;
                        if (X*X + Y*Y >= 4)
                        {
                            //check = false;
                            break;
                        }
                        number_of_iterations+=2.5;
                }
                //if (check)
                //{
                    int pix_x = top_left_x + Abs(x - x_min)/x_scale*width;
                    int pix_y = bottom_right_y - Abs(y - y_min)/y_scale*height;

                    SetPixel( hdc, pix_x, pix_y, RGB(255 - number_of_iterations,255 - number_of_iterations,255 - number_of_iterations));
                    //SetPixel( hdc, pix_x, pix_y, RGB(0,0,0));
                //}
            }
        }

            EndPaint( hwnd, &ps );

            cout << "done" << endl;
        }
        break;

    case WM_LBUTTONDOWN:
        {
            POINT pt;

            GetCursorPos(&pt);

            ScreenToClient(hwnd, &pt);

            double centre_x = x_min + (double)pt.x/window_size*x_scale;
            double centre_y = y_max - (double)pt.y/window_size*y_scale;

            x_min = centre_x - x_scale/8;
            x_max = centre_x + x_scale/8;
            y_min = centre_y - y_scale/8;
            y_max = centre_y + y_scale/8;
        }
        break;

    case WM_RBUTTONDOWN:
        x_min = x_min0;
        x_max = x_max0;
        y_min = y_min0;
        y_max = y_max0;
        break;

    case WM_CLOSE:
        DestroyWindow(hwnd);
        break;

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

    default:
        return DefWindowProc( hwnd, message, wparam, lparam );
        break;
    }
    return 0;
}


If you run this code then note that I haven't really done anything with regards to non-square coordinates so best idea is just to put in something like 9 for the first entry and it will draw the full set.

So basically, what command will make the window update and where do I insert it?
Perhaps a WM_LBUTTONUP case which refreshes it, not sure.

Cheers.
Last edited on
Topic archived. No new replies allowed.