Buttons

I am trying to make a click game. it is supose to move the button every time you hit it. I cannot make the button move. if you guys could help me that would be very nice. Here is my source code:

Thanks,
MM

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

int X = rand() % 100 + 1; //25;
int Y = rand() % 100 + 1; //50;
int Width = 65;
int Height = 25;

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


int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
				   LPSTR lpCmdLine, int nCmdShow )
{
	MSG  msg ;    
	WNDCLASS wc = {0};
	wc.lpszClassName = TEXT( "Click Game" );
	wc.hInstance     = hInstance ;
	wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
	wc.lpfnWndProc   = WndProc ;
	wc.hCursor       = LoadCursor(0, IDC_ARROW);

	RegisterClass(&wc);
	CreateWindow( wc.lpszClassName, TEXT("Click Game"),
		WS_OVERLAPPEDWINDOW | WS_VISIBLE,
		150, 150, 230, 150, 0, 0, hInstance, 0);  

	while( GetMessage(&msg, NULL, 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:
	{
		CreateWindow(TEXT("button"), TEXT("Click Me"),    
			WS_VISIBLE | WS_CHILD | ,
			X, Y, Width, Height,        
			hwnd, (HMENU) 1, NULL, NULL);  
	}

	case WM_COMMAND:
	{
		if (LOWORD(wParam) == 1) 
		{
			X = rand() % 100 + 1;
			Y = rand() % 100 + 1;
		}
		break;
	}

	case WM_DESTROY:
		{
			PostQuitMessage(0);
			break;
		}
	}
	return DefWindowProc(hwnd, msg, wParam, lParam);
}	
Last edited on
How does the MoveWindow work, I can't figure it out.

Thanks,
MM
1
2
HWND MyButton = CreateWindow( /* some values */ );
MoveWindow( MyButton, X, Y, Width, Height, 1 );


Add a global variable of type HWND for the button and assign it the value returned in line 41 by CreateWindow. then use MoveWindow
Last edited on
If i get this right HWND MyButton = CreateWindow( /* some values */ ); takes place of my
1
2
3
4
CreateWindow(TEXT("button"), TEXT("Click Me"),    
			WS_VISIBLE | WS_CHILD | ,
			X, Y, Width, Height,        
			hwnd, (HMENU) 1, NULL, NULL);  


Am i right.

Thanks for your help so far,
MM
Remember that 'MyButton' should be visible in WM_COMMAND block, so you should put the declaration on the global scope or as static in WndProc before the switch
what is the format for declaring a 'MyButton'?
1
2
3
HWND MyButton;
//...
MyButton = CreateWindow (/* ... */);
The button still doesn't move. If you could tell me what is wrong i would be very greatful.

Thanks,
MM

Here is part of my code:
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
LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
	switch(msg)  
	{
	case WM_CREATE:
	{
		MyButton = CreateWindow(TEXT("button"), TEXT("Click Me"),    
					WS_VISIBLE | WS_CHILD,
					X, Y, Width, Height,        
					hwnd, (HMENU) 1, NULL, NULL);  
	}

	case WM_COMMAND:
	{
		if (LOWORD(wParam) == 1) 
		{
			MoveWindow( MyButton, X, Y, Width, Height, 1);
		}
		break;
	}

	case WM_DESTROY:
		{
			PostQuitMessage(0);
			break;
		}
	}
	return DefWindowProc(hwnd, msg, wParam, lParam);
}	
Thank you for your help. I know what i am doing now.

Thank you very much,
MM

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
LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
	switch(msg)  
	{
	case WM_CREATE:
	{
		MyButton = CreateWindow(TEXT("button"), TEXT("Click Me"),    
					WS_VISIBLE | WS_CHILD,
					X, Y, Width, Height,        
					hwnd, (HMENU) 1, NULL, NULL);  
	}

	case WM_COMMAND:
	{
		if (LOWORD(wParam) == 1) 
		{
			DestroyWindow(MyButton);
			X = rand() % 100 + 1;
			Y = rand() % 100 + 1;
			MyButton = CreateWindow(TEXT("button"), TEXT("Click Me"),    
					WS_VISIBLE | WS_CHILD,
					X, Y, Width, Height,        
					hwnd, (HMENU) 1, NULL, NULL);
		}
		break;
	}

	case WM_DESTROY:
		{
			PostQuitMessage(0);
			break;
		}
	}
	return DefWindowProc(hwnd, msg, wParam, lParam);
}
Last edited on
why destroy and re-create the window? - just calculate the new X and Y position - and use MoveWindow.
When you was using MoveWindow, you didn't set the new X Y values, that's why it didn't move!
1
2
3
4
5
6
7
8
9
10
11
12
13
case WM_COMMAND:
{
    if (LOWORD(wParam) == 1) 
    {
        /* 
      missing:
            X = rand() % 100 + 1;
            Y = rand() % 100 + 1;
        */
        MoveWindow( MyButton, X, Y, Width, Height, 1);
    }
    break;
}
closed account (S6k9GNh0)
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
BOOL MoveWindow(          
    HWND hWnd,
    int X,
    int Y,
    int nWidth,
    int nHeight,
    BOOL bRepaint
);

Parameters

hWnd
[in] Handle to the window. 
X
[in] Specifies the new position of the left side of the window. 
Y
[in] Specifies the new position of the top of the window. 
nWidth
[in] Specifies the new width of the window. 
nHeight
[in] Specifies the new height of the window. 
bRepaint
[in] Specifies whether the window is to be repainted. If this parameter is TRUE, the window receives a message. If the parameter is FALSE, no repainting of any kind occurs. This applies to the client area, the nonclient area (including the title bar and scroll bars), and any part of the parent window uncovered as a result of moving a child window. 
Return Value

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError.


This is pretty much all you really need to know. Just read it, toy with it, and make sense.
I Got it. After your help i figured out how to use the MoveWindow fucntion.
Thank you very much for all yor help.

Thanks,
MM
Topic archived. No new replies allowed.