include windows include objidl include gdiplus include cstdlib include

  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
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <windows.h>
#include <objidl.h>
#include <gdiplus.h>
#include <cstdlib>
#include <ctime>
#pragma comment (lib,"Gdiplus.lib")
using namespace Gdiplus;
VOID OnPaint(HDC hdc, int direction)
{
Graphics graphics(hdc);
Color Orange(255, 255, 165, 0);
SolidBrush brush(Orange);
Pen pen(&brush);
PointF offset(20.0f, 20.0f);
SizeF size(20.0f, 20.0f);
RectF snake(offset, size);
switch (direction)
{
case 1 : snake.Offset(-20.0f, 0.0f); MessageBox(NULL, "1", NULL, MB_OK);
break;
case 2 : snake.Offset(20.0f, 0.0f); MessageBox(NULL, "2", NULL, MB_OK);
break;
case 3 : snake.Offset(0.0f, -20.0f); MessageBox(NULL, "3", NULL, MB_OK);
break;
case 4 : snake.Offset(0.0f, 20.0f); MessageBox(NULL, "4", NULL, MB_OK);
break;
}
graphics.DrawEllipse(&pen, snake);
}
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, PSTR, INT iCmdShow)
{
HWND hWnd;
MSG msg;
WNDCLASS wndClass;
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
// Initialize GDI+.
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
wndClass.style = CS_HREDRAW | CS_VREDRAW;
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 = TEXT("GraphicsWithGDIPlus");
RegisterClass(&wndClass);
hWnd = CreateWindow(
TEXT("GraphicsWithGDIPlus"), // window class name
TEXT("Graphics with GDI+"), // window caption
WS_CAPTION|WS_SYSMENU, // window style
100, // initial x position
100, // initial y position
500, // initial x size
500, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL); // creation parameters
ShowWindow(hWnd, iCmdShow);
UpdateWindow(hWnd);
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
GdiplusShutdown(gdiplusToken);
return msg.wParam;
} // WinMain
LRESULT CALLBACK WndProc(HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
switch(message)
{
case WM_KEYDOWN :
switch ( wParam )
{
case VK_LEFT : hdc = BeginPaint(hWnd, &ps);
OnPaint(hdc, 1);
EndPaint(hWnd, &ps);
return 0;
case VK_RIGHT : hdc = BeginPaint(hWnd, &ps);
OnPaint(hdc, 2);
EndPaint(hWnd, &ps);
return 0;
case VK_UP : hdc = BeginPaint(hWnd, &ps);
OnPaint(hdc, 3);
EndPaint(hWnd, &ps);
return 0;
case VK_DOWN : hdc = BeginPaint(hWnd, &ps);
OnPaint(hdc, 4);
EndPaint(hWnd, &ps);
return 0;
}
return 0;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
OnPaint(hdc, 0);
EndPaint(hWnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
} // WndProc