kot1990 Expert Cheater
Reputation: 1
Joined: 06 Sep 2009 Posts: 131 Location: Greece
|
Posted: Wed Dec 30, 2009 2:37 pm Post subject: My first api shit. |
|
|
This is my first release with win32 api, I couldn't post it in binaries section as it requires 100 post,, so anyway...
Here's what I have:
It is a window, when the mouse touches it, it runs away, if you manage to hit the X to close it you win
DON'T CHEAT!
| Code: | #include <windows.h>
int checkDir(int,int);
int max_x,max_y;
const char g_szClassName[] = "myWindowClass";
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CLOSE:
MessageBox(NULL,"got me!! :)","",MB_OK);
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
//Step 1: Registering the Window Class
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
// Step 2: Creating the Window
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"CATCH ME!!!",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
NULL, NULL, hInstance, NULL);
if(hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
int curr_x,curr_y,randx,dir,randy,leftright,updown;
dir = 1;
curr_x = 0;
curr_y = 0;
max_x = GetSystemMetrics(SM_CXSCREEN);
max_y = GetSystemMetrics(SM_CYSCREEN);
max_x-= 240;
max_y-= 120;
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
// The Message Loop
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
if (curr_x <= 0 || curr_x >= max_x || curr_y <= 0 || curr_y >= max_y)
{
randx = (int)5*rand()/RAND_MAX+7;
randy = (int)5*rand()/RAND_MAX+7;
}
if (dir == 1)
{
if (curr_x <= 0)
updown = (int)2*rand()/RAND_MAX+1;
MoveWindow(hwnd,curr_x,curr_y,240,120,TRUE);
curr_x += randx;
if (updown == 1)
curr_y -= randy;
else
curr_y += randy;
Sleep(2);
dir = checkDir(curr_x,curr_y);
}
else if (dir == 2)
{
if (curr_y <= 0)
leftright = (int)2*rand()/RAND_MAX+1;
MoveWindow(hwnd,curr_x,curr_y,240,120,TRUE);
if (leftright == 1)
curr_x -= randx;
else
curr_x += randx;
curr_y += randy;
Sleep(2);
dir = checkDir(curr_x,curr_y);
}
else if (dir == 3)
{
if (curr_x >= max_x)
updown = (int)2*rand()/RAND_MAX+1;
MoveWindow(hwnd,curr_x,curr_y,240,120,TRUE);
curr_x -= randx;
if (updown == 1)
curr_y -= randy;
else
curr_y += randy;
Sleep(2);
dir = checkDir(curr_x,curr_y);
}
else
{
if (curr_y >= max_y)
leftright = (int)2*rand()/RAND_MAX+1;
MoveWindow(hwnd,curr_x,curr_y,240,120,TRUE);
if (leftright == 1)
curr_x -= randx;
else
curr_x += randx;
curr_y -= randy;
Sleep(2);
dir = checkDir(curr_x,curr_y);
}
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
int checkDir(int x, int y)
{
if (x <= 0)
return 1;
else if (y <= 0)
return 2;
else if (x >= max_x)
return 3;
else if (y >= max_y)
return 4;
} |
Edit: If you've noticed that sometimes the window moves by itself without having to touch it with the mouse is because everytime the window gets a message it executes the movewindow() function in the message loop, and messages can be sent by the OS to the window too, so everytime time the window recieves a message it will always make a movement.
|
|