#include <windows.h>
#include "resource.h"
LRESULT CALLBACK Moje_Smycka_Zprav(HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdline, int nCmdShow)
{
char *szAppName = "Ahoj !";
HWND hwnd;
MSG msg;
WNDCLASSEX wndclass;
wndclass.cbSize = sizeof(wndclass);
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = Moje_Smycka_Zprav;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
wndclass.hCursor = LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW));
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
wndclass.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);
wndclass.lpszClassName = szAppName;
wndclass.hIconSm = LoadIcon(NULL, MAKEINTRESOURCE(IDI_APPLICATION));
RegisterClassEx(&wndclass);
hwnd = CreateWindow(szAppName, "Ahoj",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, // pozice
320, 200, // rozmery
NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
// zobrazi okno
SetTimer(hwnd, 0, 30, 0);
// vytvori timer s periodou 30 mSec
while(GetMessage (&msg, NULL, 0, 0)) {
TranslateMessage (&msg);
DispatchMessage (&msg);
}
// smyčka zpráv
return msg.wParam;
}
LRESULT CALLBACK Moje_Smycka_Zprav(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
HBRUSH sh_brush, black_brush, prev_brush;
HPEN red_pen, prev_pen;
HDC hdc;
int x;
switch(iMsg) {
case WM_TIMER:
hdc = GetDC(hwnd);
// zacne kreslit
red_pen = CreatePen(PS_SOLID, 2, 0x0000ff);
// vytvori cervene pero, 2 body tluste
sh_brush = CreateSolidBrush(0xf0f0f0);
// vytvori sedy stetec
black_brush = CreateSolidBrush(0x000000);
// vytvori cerny stetec
prev_pen = SelectObject(hdc, red_pen);
// nastavi pero (musi si zapamatovat posledni objekt)
MoveToEx(hdc, 50, 30, false);
LineTo(hdc, 10, 20);
LineTo(hdc, 40, 60);
LineTo(hdc, 80, 20);
// nakresli caru
prev_brush = SelectObject(hdc, sh_brush);
// nastavi stetec
Rectangle(hdc, 100, 100, 120, 130);
// kresli obdelnik
SelectObject(hdc, black_brush);
// nastavi jiny stetec (funkce ted vrati prev_brush)
RoundRect(hdc, 200, 20, 230, 50, 30, 30);
// nakresli kruznici (tohle je obdelnik
// se zaoblenymi rohy, nastaveny na uplne zaobleni)
SelectObject(hdc, prev_pen);
SelectObject(hdc, prev_brush);
// musi dat zpatky objekt co tam byl naposled !
MoveToEx(hdc, 230, 130, false);
for(x = 230; x < 280; x += 5)
LineTo(hdc, x, 130 - (int)((float)((x - 230) * (x - 230)) / 20));
// nakresli graf funkce x^2
DeleteObject(red_pen);
DeleteObject(sh_brush);
// musi smazat vsecky objekty
ReleaseDC(hwnd, hdc);
// musi zrusit kontext zarizeni, inac to spadne.
return 0;
case WM_KEYDOWN:
if(wParam == 27)
PostMessage(hwnd, WM_CLOSE, 0, 0); // Escape
return 0;
case WM_COMMAND:
if(wParam == IDM_KONEC) { // Je celkem na nás, jak se to bude jmenovat
if(MessageBox(hwnd, "Opravdu chcete skončit ?", "Koniec", MB_YESNO) == IDYES)
PostMessage(hwnd, WM_CLOSE, 0, 0);
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, iMsg, wParam, lParam);
}
|