Code:
#include <windows.h>
#include <windef.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>
#include <stdio.h>
#include <math.h>
HBRUSH foregroundbrush = CreateSolidBrush(RGB(255, 255, 255));
HBRUSH backgroundbrush = CreateSolidBrush(RGB(255, 255, 255));
static TCHAR szWindowClass[] = _T("win32app");
static TCHAR szTitle[] = _T("Win32 Guided Tour Application");
HINSTANCE hInst;
RECT background;
int windowwidth = 1000;
int windowheight = 500;
int mf = 0x57;
int mb = 0x53;
int ml = 0x44;
int mr = 0x41;
double mx = 100;
double my = 100;
double ma = 0;
int test = 0;
void Line (HDC _hdc, int x1, int y1, int x2, int y2, int w)
{
MoveToEx(_hdc, x1, y1, NULL);
LineTo(_hdc, x2, y2);
int i = 1;
while(i < w)
{
MoveToEx(_hdc, x1 + i, y1, NULL);
LineTo(_hdc, x2 + i, y2);
MoveToEx(_hdc, x1, y1 + i, NULL);
LineTo(_hdc, x2, y2 + i);
MoveToEx(_hdc, x1 - i, y1, NULL);
LineTo(_hdc, x2 - i, y2);
MoveToEx(_hdc, x1, y1 - i, NULL);
LineTo(_hdc, x2, y2 - i);
i++;
}
}
/*
hdc = GetDC(hwnd); //hdc is handle to device context
x = LOWORD(lParam); //Store the current x
y = HIWORD(lParam); //Store the current y
if (wParam & MK_LBUTTON) //If Left mouse button is down then draw
{
Line(hdc, lastx, lasty, x, y); //Draw the line frome the last pair of coordiates to current
lastx = x; //The current x becomes the lastx for next line to be drawn
lasty = y; //The current y becomes the lasty for next line to be drawn
}
ReleaseDC(hwnd, hdc);
*/
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
//const RECT *background = {0, 0, 1000, 500};
/*background->left = 0;
background->top = 0;
background->right = windowwidth;
background->bottom = windowheight;*/
if(GetAsyncKeyState(ml) < 0)
{
ma = ma + 0.001;
}
if(GetAsyncKeyState(mr) < 0)
{
ma = ma - 0.001;
}
if(GetAsyncKeyState(mf) < 0)
{
mx = mx + (0.1 * cos(ma));
my = my + (0.1 * sin(ma));
}
if(GetAsyncKeyState(mb) < 0)
{
mx = mx - (0.1 * cos(ma));
my = my - (0.1 * sin(ma));
}
/*if(test == 0)
{
test = 1;
} else
{
test = 0;
}*/
switch (message)
{
case WM_PAINT:
BeginPaint(hWnd, &ps);
GetWindowRect(hWnd , &background);
hdc = GetDC(hWnd);
//backgroundbrush = CreateSolidBrush(RGB(255 * test, 255 * test, 255 * test));
FillRect(hdc, &background, backgroundbrush);
//int FillRect(HDC hDC, CONST RECT *lprc, HBRUSH hbr);
//FillRectangle(
Line(hdc, (int)mx, (int)my, (int)(mx + (10 * cos(ma))), (int)(my + (10 * sin(ma))), 3);
InvalidateRect(hWnd , &background, true);
UpdateWindow(hWnd);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_MOUSEMOVE:
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
if (!RegisterClassEx(&wcex))
{
MessageBox(NULL, _T("Call to RegisterClassEx failed!"), _T("Win32 Guided Tour"), NULL);
return 1;
}
hInst = hInstance;
HWND hWnd = CreateWindow(
szWindowClass,
szTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
windowwidth, windowheight,
NULL,
NULL,
hInstance,
NULL
);
if (!hWnd)
{
MessageBox(NULL, _T("Call to CreateWindow failed!"), _T("Win32 Guided Tour"), NULL);
return 1;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam;
}
main.cpp, compiled with Visual C++ 2008 Express Edition