How to repaint the window with button click?

Pages: 12
This is the other necessary functions


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
LRESULT CALLBACK DialogProcedure(HWND hWnd,UINT msg, WPARAM wp, LPARAM lp)
{
	switch(msg)
	{
		// Sent when an application requests that a window be created by calling the CreateWindowEx or CreateWindow function.
		case WM_CREATE:
			AddDialogBox(hWnd);
			break;
		
		// Sent as a signal that a window or an application should terminate. 		
		case WM_CLOSE:
			DestroyWindow(hWnd);
			break;
		default:
			return DefWindowProcW(hWnd, msg, wp, lp);
	} // end switch 
} // end LRESULT CALLBACK DialogProcedure


void RegisterDialogClass(HINSTANCE hInst)
{
	WNDCLASSW dialog  = {0} ;

	dialog.hbrBackground = (HBRUSH)COLOR_WINDOW ;										 // A handle to the class background brush
	dialog.hCursor = LoadCursor(NULL, IDC_HAND);  										 // A handle to the class cursor. This member must be a handle to a cursor resource. 
	dialog.hInstance = hInst;															 // A handle to the instance that contains the window procedure for the class.
	dialog.lpszClassName = L"SimpleDialog";												 // A pointer to a null-terminated string or is an atom
	dialog.lpfnWndProc = DialogProcedure ;  											 // A pointer to the window procedure	

	RegisterClassW(&dialog);
} // end RegisterDialogClass



void displayDialog(HWND hWnd)
{
    CreateWindowW(L"SimpleDialog", L"DialogBox",  WS_VISIBLE | WS_OVERLAPPEDWINDOW , 400,200,500,300,hWnd,NULL,NULL,NULL);	
	
} // end displayDialog



 void AddDialogBox(HWND hWnd)
{	
	
	CreateWindowW(L"static", L"User Name: Lee D. Daniel\n\nProgram Name: JimmyDaLou\n\nProgram Description: Simple GUI with Menu and    DialogBox\n\nProgram Version Number: 1.0", WS_VISIBLE | WS_BORDER | WS_CHILD ,5,5,450,150,hWnd,NULL,NULL,NULL);	
}



void addControls(HWND hWnd)
{	
	CreateWindowW(L"Button", L"Fill", WS_VISIBLE | WS_CHILD, 40, 40, 100, 50, hWnd, (HMENU)ID_FILL_TANK, NULL, NULL);
	CreateWindowW(L"Button", L"Empty", WS_VISIBLE | WS_CHILD, 210, 40, 100, 50, hWnd, (HMENU)ID_EMPTY_TANK, NULL, NULL);
	CreateWindowW(L"Button", L"Half", WS_VISIBLE | WS_CHILD, 380, 40, 100, 50, hWnd, (HMENU)ID_HALF_FILL_TANK, NULL, NULL);
	CreateWindowW(L"Button", L"AutoBot", WS_VISIBLE | WS_CHILD, 550, 40, 100, 50, hWnd, (HMENU)ID_AUTO_RUN, NULL, NULL);
	TextBox = CreateWindowW(L"Edit", L"", WS_VISIBLE | WS_CHILD | ES_MULTILINE| WS_BORDER | WS_VSCROLL, 12, 200, 650, 300, hWnd, NULL, NULL, NULL);
}
InvalidateRect(hWnd, &rect, false);
You need to invalidate the second window where you draw the Tank
OK.. So where would I add this code? Do I add it in the main or in one of the procedures? Cause I did add them in the window 1 procedure as you said, unless if I am doing it wrong.
So I added the InvalidateRect() function to the if statements in the WM_PAINT function calls..

I can see some slight changes but the original rectangle I had drawn to behave as water, it is redrawn smaller and it draws over shapes over each the other, meaning they don't disappear when a new one is drawn.

So based on what I'm seeing in summary is that it draws a completely new set of rectangles and they, not inline with each other and there is also a constant flickering.


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
LRESULT CALLBACK WindowProcedure2(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	HDC hdc;          	// handle to device context (DC)  
    PAINTSTRUCT ps;   	// paint data for Begin/EndPaint   
    int width,height;  	// Width and height of Client Area
	RECT rect = {30, 20, 500, 500};
    
    switch (msg)  
    {  
	    case WM_CREATE :  
	          
	        return 0;  
	      
	    case WM_SIZE :  
	        width = LOWORD(lParam);  
	        height = HIWORD(lParam);  
	  
	    case WM_PAINT :  
	        hdc = BeginPaint(hWnd, &ps);  


			if(Tank_Initial_Level)
			{
				draw_Tank_Status(hdc, width, height);
	        	InvalidateRect(hWnd, &rect, false);
	        	UpdateWindow(SecdWnd);
			}
				
//	        cout << Tank_Filled << endl;
	        if(Tank_Filled)
			{
				cout << "Filling" << endl;
	        	water_Level = 80;
	        	draw_Tank_Status(hdc, width, height);
	        	InvalidateRect(hWnd, &rect, false);
	        	UpdateWindow(SecdWnd);
	        }	
	        
	        cout << Tank_Empty << endl;
			if(Tank_Empty)
	        {
				cout << "Empty" << endl;
	        	water_Level = 525;
	        	draw_Tank_Status(hdc, width, height);
	        	InvalidateRect(hWnd, &rect, false);
	        	UpdateWindow(SecdWnd);
	        }	
	        
	        cout << Tank_Half_Filled << endl;
			if(Tank_Half_Filled)
	        {
				cout << "Half-Filled" << endl;
	        	water_Level = 300;
	        	draw_Tank_Status(hdc, width, height);
	        	InvalidateRect(hWnd, &rect, false);
	        	UpdateWindow(SecdWnd);
	        }	
	  
	        EndPaint(hWnd, &ps);  
	          
	        return 0; 
			 
	    case WM_DESTROY:  
	    case WM_CLOSE :  
	        PostQuitMessage(EXIT_SUCCESS);  
	        return 0;  
	}

    return DefWindowProc(hWnd, msg, wParam, lParam);
}
Last edited on
Remove al of
1
2
InvalidateRect(hWnd, &rect, false);
	        	UpdateWindow(SecdWnd);
in WindowProcedure2

InvalidateRect needs to be called only in the WindowProcedure, probably like this:
1
2
3
4
5
6
7
8
case ID_FILL_TANK:
  water_Level = 80;
  SetWindowText(TextBox, "Filled");
  InvalidateRect(SecdWnd, &rect, false); // ONLY update the second window 
  Tank_Filled = true;
  Tank_Empty = false;
  Tank_Half_Filled = false;
break;

Same principle for ID_EMPTY_TANK, ID_HALF_FILL_TANK

Also try invalidate the second window. Use GetClientRect(SecdWnd, &rect);
Yoooo Thomas this works smoothly, no flickering.. Thanks much...

Only problem now is that, it redraws to the left of the screen.. I'm not sure why it does that.

Any Ideas?
I am not sure what you mean.
Can you make a screenshot, upload it somewhere and post the link here.
Registered users can post here. Sign in or register to post.
Pages: 12