Code:
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include<time.h>
#undef WS_OVERLAPPEDWINDOW
#define WS_OVERLAPPEDWINDOW (WS_OVERLAPPED | \
WS_SYSMENU | \
WS_THICKFRAME )
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("Connect") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
wndclass.style = WS_EX_DLGMODALFRAME ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("Program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName, TEXT ("C"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
100, 100,
NULL, NULL, hInstance, NULL) ;
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
srand(time(NULL));
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
TCHAR s[100];
static int iCount,bottom, right, top, left, sx = 1, sy = 1 ;
HDC hdc ;
int i, j, x, y;
PAINTSTRUCT ps ;
RECT r,win;
top = left = 0;
right = GetSystemMetrics(SM_CXSCREEN) - 100;
bottom = GetSystemMetrics(SM_CYSCREEN) - 100;
switch (message)
{
case WM_LBUTTONDOWN:
GetWindowRect(hwnd,&r);
x = rand() % 5 + 1;
y = rand() % 5 + 1;
while((r.bottom < bottom) && (r.right < right) && (r.top > top) && (r.left > left))
{
r.left += (x*sx);
r.top += (y*sy);
MoveWindow(hwnd,r.left,r.top,100,100, TRUE);
GetWindowRect(hwnd,&r);
}
if(r.bottom>= bottom)
sy = -1;
else if(r.right>= right)
sx = -1;
else if(r.top <= top)
sy = 1;
else if(r.left<=left)
sx = 1;
return 0;
case WM_MOUSEMOVE:
SendMessage(hwnd, WM_LBUTTONDOWN,wParam,lParam);
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
TextOut (hdc, 0, 0, TEXT("Click Me"),8) ;
EndPaint(hwnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}