TYPER

Jak cię złapią, to znaczy, że oszukiwałeś. Jak nie, to znaczy, że posłużyłeś się odpowiednią taktyką.
C
/*
TYPER.C - Program do pisania tekstu
(c) Charles Petzold, 1998
*/
include
define BUFFER(x,y) *(pBuffer + y * cxBuffer + x)
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
static TCHAR szAppName[] = TEXT (".Typer") ;
HWND hwnd :
246 Cz I: Podstawy I
(cig dalszy ze strony 245) i
MSG msg ;
WNDCLASS wndclass ;
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 = szAppName ;
if (!RegisterClass (&wndclass))
(
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName, TEXT ("Typing Program"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CWUSEDEFAULT, CW USEDEFAULT,
NULL, NULL, hInstance, NULL) ;
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
(
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
)
return msg.wParam ;
)
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
(
static DWORD dwCharSet = DEFAULT_CHARSET ;
static int cxChar, cyChar, cxClient, cyClient, cxBuffer, cyBuffer,
xCaret, yCaret ;
static TCHAR * pBuffer = NULL ;
HDC hdc ;
int x, y, i ;
PAINTSTRUCT ps ;
TEXTMETRIC tm ;
switch (message)
(
case WM_INPUTLANGCHANGE:
dwCharSet = wParam ;
// kontynuuj
case WM_CREATE:
hdc = GetDC (hwnd) ;
Rozdzia 6: Klawiatura 247
SelectObject (hdc, CreateFont (0, 0, 0, 0, 0, 0, 0, 0,
dwCharSet, 0, 0, 0, FIXED PITCH, NULL)) ;
GetTextMetrics (hdc, &tm) ;
cxChar = tm.tmAveCharWidth ;
cyChar = tm.tmHeight ;
Delete0bject (SelectObject (hdc, GetStockObject (SYSTEMFONT))) ;
ReleaseDC (hwnd, hdc) ;
// kontynuuj
case WM_SIZE:
// pobierz rozmiar okna w pikselach
if (message == WM SIZE)
cxClient = LOWORD (lParam) ;
cyClient = HIWORD (lParam) ; I::
1
// wyznacz rozmiar okna w znakach
; f.
cxBuffer = max (l, cxClient / cxChar) ;
cyBuffer = max (i, cyClient / cyChar) ;
// przydziel pami na bufor i wykasuj j
if (pBuffer != NULL)
free (pBuffer) ;
pBuffer = (TCHAR *) malloc (cxBuffer * cyBuffer * sizeof (TCHAR)) ;
for (y = 0 ; y < cyBuffer ; y++)
for (x = 0 ; x < cxBuffer ; x++)
I BUFFER(x,y) = ' ' , i:'
// ustaw karetk w lewym grnym naroniku
1
xCaret = 0 ;
yCaret = 0 ;
if (hwnd == GetFocus ())
( SetCaretPos (xCaret * cxChar, yCaret * cyChar) ;
InvalidateRect (hwnd, NULL, TRUE) ;
return 0 ;
case WM SETFOCUS:
// stwrz i wywietl karetk
CreateCaret (hwnd, NULL, cxChar, cyChar) ;
SetCaretPos (xCaret * cxChar, yCaret * cyChar) ;
ShowCaret (hwnd) ;
return 0 ;
i
case WMKILLFOCUS:
// ukryj i usu karetk
HideCaret (hwnd) ;
( DestroyCaret () ;
return 0 ;
248 Cz I: Podstawy
(cig dalszy ze strony 247)
case WM KEYDOWN:
switch (wParam)
t
case VK_HOME:
xCaret = 0 :
break ;
case UK_END:
xCaret = cxBuffer - 1 ;
break ;
case UK_PRIOR:
yCaret = 0 ;
break ;
case VK_NEXT:
yCaret = cyBuffer - 1 ;
break ;
case UK_LEFT:
xCaret = max (xCaret - l, 0)
break ; I
case UKRIGHT:
xCaret = min (xCaret + 1, cxBuffer - 1) ;
break :
i
case VK_UP: !
yCaret = max (yCaret - i, 0) :
break ;
i
case VK_DOWN:
Caret = min (yCaret + 1, cyBuffer - 1) ;
y
break ; !
case VK_DELETE:
for (x = xCaret ; x < cxBuffer - 1 ; x++) I
BUFFER (x, yCaret) = BUFFER (x + l, yCaret) ;
I
BUFFER (cxBuffer - 1, yCaret) = ' ' .
HideCaret (hwnd) ;
hdc = GetDC (hwnd) ;
SelectObject (hdc, CreateFont (0, 0, 0, 0, 0, 0, 0, 0,
dwCharSet, 0, 0, 0, FIXEDPITCH, NULL)) ;
TextOut (hdc, xCaret * cxChar, yCaret * cyChar.
& BUFFER (xCaret, yCaret).
cxBuffer - xCaret) ;
Delete0bject (SelectObject (hdc, GetStockObject (SYSTEMFONT))) ;
ReleaseDC (hwnd, hdc) ;
ShowCaret (hwnd) ;
break ; !
Rozdzia 6: Ktawiatura 249
1
SetCaretPos (xCaret * cxChar, yCaret * cyChar) ;
return 0 ;
,.
case WM CHAR:
for (i = 0 ; i < (int) LOWORD (lParam) ; i++)
switch (wParam)
!
case '\b': // backspace
if (xCaret > 0)
xCaret- ,
SendMessage (hwnd, WMKEYDOWN, VK DELETE, 1) ;
I
break ;
case '\t': // tab
do
(
SendMessage (hwnd, WM CHAR, ' , 1) ;
while (xCaret % 8 != 0) :
break ;
j case '\n': // przejcie do nowej linii
if (++yCaret = cyBuffer)
yCaret = 0 :
break ;
case '\r': // powrt karetki
xCaret = 0 :
if (++yCaret = cyBuffer)
yCaret = 0 :
break ;
I
case '\x1B': /! escape
for (y = 0 ; y < cyBuffer ; y++)
for (x = 0 : x < cxBuffer ; x++) i
BUFFER (x, y) = ' ' ,
xCaret = 0 ;
yCaret = 0 ;
InvalidateRect (hwnd, NULL, FALSE) ;
break :
default: // kody znakw
BUFFER (xCaret, yCaret) = (TCHAR) wParam ; .
HideCaret (hwnd) ;
hdc = GetDC (hwnd) ;
i
SelectObject (hdc, CreateFont (0, 0, 0, 0, 0, 0, 0, 0,
dwCharSet, 0, 0, 0, FIXED PITCH, NULL)) ;
TextOut (hdc, xCaret * cxChar, yCaret * cyChar,
& BUFFER (xCaret, yCaret), 1) ;
t
250 Cz I: Podstawy ,
(cig dalszy ze strony 249)
Delete0bject (
SelectObject (hdc, GetStockObject (SYSTEM FONT))) ;
ReleaseDC (hwnd, hdc) ;
ShowCaret (hwnd) ; !
i
if (++xCaret = cxBuffer)
I
xCaret = 0 ;
if (++yCaret == cyBuffer)
yCaret = 0 ;
)
break ;
)
SetCaretPos (xCaret * cxChar, yCaret * cyChar) ;
return 0 ;
case b!M_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
r
SelectObject (hdc, CreateFont (0, 0, 0, 0, 0, 0, 0, 0,
dwCharSet, 0, 0, 0, FIXED PITCN, NULL)) ;
Wątki
Powered by wordpress | Theme: simpletex | © Jak cię złapią, to znaczy, że oszukiwałeś. Jak nie, to znaczy, że posłużyłeś się odpowiednią taktyką.