Zde můžete vidět rozdíly mezi vybranou verzí a aktuální verzí dané stránky.
| Obě strany předchozí revizePředchozí verze | |||
| pitel:itu:cviceni2 [12. 09. 2013, 08.11:43] – file pitel | pitel:itu:cviceni2 [30. 12. 2022, 13.43:01] (aktuální) – upraveno mimo DokuWiki 127.0.0.1 | ||
|---|---|---|---|
| Řádek 1: | Řádek 1: | ||
| + | ====== Uživatelské vstupy – klávesnice, | ||
| + | <file c> | ||
| + | #include < | ||
| + | //#include < | ||
| + | //#include < | ||
| + | // Global variable | ||
| + | |||
| + | HINSTANCE hInst; | ||
| + | |||
| + | // Function prototypes. | ||
| + | int WINAPI WinMain(HINSTANCE, | ||
| + | LRESULT CALLBACK MainWndProc(HWND, | ||
| + | |||
| + | // Application entry point. | ||
| + | int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, | ||
| + | LPSTR lpCmdLine, int nCmdShow) | ||
| + | { | ||
| + | MSG msg; | ||
| + | BOOL bRet; | ||
| + | WNDCLASS wcx; // register class | ||
| + | HWND hWnd; | ||
| + | //UINT uResult; | ||
| + | |||
| + | hInst = hInstance; | ||
| + | // Fill in the window class structure with parameters that describe the main window. | ||
| + | |||
| + | wcx.style = CS_HREDRAW | CS_VREDRAW; | ||
| + | wcx.lpfnWndProc = (WNDPROC) MainWndProc; | ||
| + | wcx.cbClsExtra = 0; // no extra class memory | ||
| + | wcx.cbWndExtra = 0; // no extra window memory | ||
| + | wcx.hInstance = hInstance; | ||
| + | wcx.hIcon = LoadIcon(NULL, | ||
| + | wcx.hCursor = LoadCursor(NULL, | ||
| + | wcx.hbrBackground = GetStockObject(WHITE_BRUSH); | ||
| + | wcx.lpszMenuName = (LPCSTR) " | ||
| + | wcx.lpszClassName = (LPCSTR) " | ||
| + | |||
| + | // Register the window class. | ||
| + | |||
| + | if (!RegisterClass(& | ||
| + | return FALSE; | ||
| + | |||
| + | // create window of registered class | ||
| + | |||
| + | hWnd = CreateWindow(" | ||
| + | " | ||
| + | WS_OVERLAPPEDWINDOW, | ||
| + | 100, // default horizontal position | ||
| + | 100, // default vertical position | ||
| + | 300, // default width | ||
| + | 300, // default height | ||
| + | (HWND) NULL, // no owner window | ||
| + | (HMENU) NULL, // use class menu | ||
| + | hInstance, | ||
| + | (LPVOID) NULL); // no window-creation data | ||
| + | if (!hWnd) | ||
| + | return FALSE; | ||
| + | |||
| + | // Show the window and send a WM_PAINT message to the window procedure. | ||
| + | // Record the current cursor position. | ||
| + | |||
| + | ShowWindow(hWnd, | ||
| + | UpdateWindow(hWnd); | ||
| + | |||
| + | // loop of message processing | ||
| + | |||
| + | while ((bRet = GetMessage(& | ||
| + | if (bRet == -1) { | ||
| + | // handle the error and possibly exit | ||
| + | } else { | ||
| + | TranslateMessage(& | ||
| + | DispatchMessage(& | ||
| + | } | ||
| + | } | ||
| + | return (int) msg.wParam; | ||
| + | } | ||
| + | |||
| + | #define BUFSIZE 65535 | ||
| + | |||
| + | LRESULT APIENTRY MainWndProc(HWND hwndMain, UINT uMsg, WPARAM wParam, | ||
| + | | ||
| + | { | ||
| + | static int nchLast; | ||
| + | static RECT TextArea; | ||
| + | static PTCHAR pchInputBuf; | ||
| + | int i; // counter | ||
| + | //int nVirtKey; | ||
| + | HDC hdc; // handle to device context | ||
| + | PAINTSTRUCT ps; // required by BeginPaint | ||
| + | static DWORD dwCharY; // height of characters | ||
| + | static int nCaretPosX = 0; // horizontal position of caret - real position | ||
| + | static int nCaretPosY = 0; // vertical position of caret - number of lines | ||
| + | TCHAR ch; // current character | ||
| + | int chWidth; | ||
| + | TEXTMETRIC tm; // structure for text metrics | ||
| + | POINT ReleasePoint; | ||
| + | HWND hwndTmp; | ||
| + | //HGLOBAL hglb; // global memory object | ||
| + | //LPTSTR lptstr; | ||
| + | static int caretpos; | ||
| + | static int bufptr; | ||
| + | static int indent; | ||
| + | |||
| + | switch (uMsg) { | ||
| + | case WM_CREATE: | ||
| + | // Allocate a buffer to store keyboard input. | ||
| + | pchInputBuf = (LPTSTR) GlobalAlloc(GPTR, | ||
| + | nchLast = 0; | ||
| + | caretpos = 0; | ||
| + | pchInputBuf[nchLast] = TEXT(' | ||
| + | // Get the metrics of the current font. | ||
| + | hdc = GetDC(hwndMain); | ||
| + | GetTextMetrics(hdc, | ||
| + | ReleaseDC(hwndMain, | ||
| + | |||
| + | // Save the average character width and height. | ||
| + | dwCharY = tm.tmHeight; | ||
| + | |||
| + | break; | ||
| + | |||
| + | case WM_CHAR: | ||
| + | switch (wParam) { | ||
| + | case 0x08: // backspace | ||
| + | //FIXME: Mazani odradkovani!!! | ||
| + | if (caretpos <= 0) { | ||
| + | break; | ||
| + | } | ||
| + | ch = (TCHAR) pchInputBuf[--caretpos]; | ||
| + | HideCaret(hwndMain); | ||
| + | |||
| + | // Retrieve the character' | ||
| + | hdc = GetDC(hwndMain); | ||
| + | GetCharWidth32(hdc, | ||
| + | nCaretPosX -= chWidth; | ||
| + | ReleaseDC(hwndMain, | ||
| + | SetCaretPos(nCaretPosX, | ||
| + | |||
| + | bufptr = caretpos; | ||
| + | while (bufptr < nchLast) { | ||
| + | pchInputBuf[bufptr] = pchInputBuf[bufptr + 1]; | ||
| + | bufptr++; | ||
| + | } | ||
| + | nchLast--; | ||
| + | |||
| + | ShowCaret(hwndMain); | ||
| + | break; | ||
| + | case 0x0A: // linefeed | ||
| + | case 0x1B: // escape | ||
| + | MessageBeep((UINT) - 1); | ||
| + | break; | ||
| + | |||
| + | case 0x09: // tab | ||
| + | // Convert tabs to four consecutive spaces. | ||
| + | for (i = 0; i < 4; i++) | ||
| + | SendMessage(hwndMain, | ||
| + | break; | ||
| + | |||
| + | case 0x0D: // carriage return | ||
| + | // Record the carriage return and position the caret at the beginning of the new line. | ||
| + | pchInputBuf[nchLast++] = 0x0D; | ||
| + | nCaretPosY++; | ||
| + | nCaretPosX = 0; | ||
| + | caretpos++; | ||
| + | break; | ||
| + | |||
| + | default: | ||
| + | ch = (TCHAR) wParam; | ||
| + | HideCaret(hwndMain); | ||
| + | |||
| + | // Retrieve the character' | ||
| + | hdc = GetDC(hwndMain); | ||
| + | TextOut(hdc, | ||
| + | GetCharWidth32(hdc, | ||
| + | nCaretPosX += chWidth; | ||
| + | ReleaseDC(hwndMain, | ||
| + | |||
| + | SetCaretPos(nCaretPosX, | ||
| + | ShowCaret(hwndMain); | ||
| + | |||
| + | // Store the character in the buffer. | ||
| + | if (nchLast != caretpos) { | ||
| + | bufptr = nchLast; | ||
| + | while (bufptr >= caretpos) { | ||
| + | pchInputBuf[bufptr + 1] = pchInputBuf[bufptr]; | ||
| + | bufptr--; | ||
| + | } | ||
| + | } | ||
| + | pchInputBuf[caretpos++] = ch; | ||
| + | nchLast++; | ||
| + | break; | ||
| + | } | ||
| + | InvalidateRect(hwndMain, | ||
| + | break; | ||
| + | |||
| + | case WM_KEYDOWN: | ||
| + | switch (wParam) { | ||
| + | case VK_LEFT: | ||
| + | if (caretpos <= 0) { | ||
| + | break; | ||
| + | } | ||
| + | HideCaret(hwndMain); | ||
| + | caretpos--; | ||
| + | ch = (TCHAR) pchInputBuf[caretpos]; | ||
| + | if (ch == 0x0D) { | ||
| + | nCaretPosY--; | ||
| + | caretpos--; | ||
| + | while (caretpos >= 0 && pchInputBuf[caretpos] != 0x0D) { | ||
| + | // | ||
| + | caretpos--; | ||
| + | } | ||
| + | while (caretpos < nchLast | ||
| + | && | ||
| + | ch = (TCHAR) pchInputBuf[caretpos]; | ||
| + | hdc = GetDC(hwndMain); | ||
| + | GetCharWidth32(hdc, | ||
| + | nCaretPosX += chWidth; | ||
| + | ReleaseDC(hwndMain, | ||
| + | } | ||
| + | } else { | ||
| + | hdc = GetDC(hwndMain); | ||
| + | GetCharWidth32(hdc, | ||
| + | nCaretPosX -= chWidth; | ||
| + | ReleaseDC(hwndMain, | ||
| + | } | ||
| + | SetCaretPos(nCaretPosX, | ||
| + | ShowCaret(hwndMain); | ||
| + | break; | ||
| + | |||
| + | case VK_RIGHT: | ||
| + | if (caretpos >= nchLast) { | ||
| + | break; | ||
| + | } | ||
| + | HideCaret(hwndMain); | ||
| + | ch = (TCHAR) pchInputBuf[caretpos]; | ||
| + | caretpos++; | ||
| + | if (ch == 0x0D) { | ||
| + | nCaretPosY++; | ||
| + | nCaretPosX = 0; | ||
| + | } else { | ||
| + | // | ||
| + | hdc = GetDC(hwndMain); | ||
| + | GetCharWidth32(hdc, | ||
| + | nCaretPosX += chWidth; | ||
| + | ReleaseDC(hwndMain, | ||
| + | } | ||
| + | SetCaretPos(nCaretPosX, | ||
| + | ShowCaret(hwndMain); | ||
| + | break; | ||
| + | |||
| + | case VK_UP: | ||
| + | HideCaret(hwndMain); | ||
| + | indent = 0; | ||
| + | while (caretpos > 0 && pchInputBuf[caretpos] != 0x0D) { | ||
| + | caretpos--; | ||
| + | indent++; | ||
| + | } | ||
| + | if (caretpos > 0) { | ||
| + | indent--; | ||
| + | } | ||
| + | // | ||
| + | if (caretpos == 0) { // 1. radek -> nic se nestane | ||
| + | caretpos += indent; | ||
| + | ShowCaret(hwndMain); | ||
| + | break; | ||
| + | } | ||
| + | nCaretPosY--; | ||
| + | nCaretPosX = 0; | ||
| + | caretpos--; | ||
| + | while (caretpos > 0 && pchInputBuf[caretpos] != 0x0D) { //posune se na zacatek radku | ||
| + | caretpos--; | ||
| + | } | ||
| + | if (caretpos > 0) { | ||
| + | caretpos++; | ||
| + | } | ||
| + | // | ||
| + | for (; indent > 0; indent--) { | ||
| + | ch = (TCHAR) pchInputBuf[caretpos]; | ||
| + | if (ch == 0x0D) { | ||
| + | break; | ||
| + | } | ||
| + | hdc = GetDC(hwndMain); | ||
| + | GetCharWidth32(hdc, | ||
| + | nCaretPosX += chWidth; | ||
| + | ReleaseDC(hwndMain, | ||
| + | caretpos++; | ||
| + | } | ||
| + | SetCaretPos(nCaretPosX, | ||
| + | ShowCaret(hwndMain); | ||
| + | break; | ||
| + | |||
| + | case VK_DOWN: | ||
| + | HideCaret(hwndMain); | ||
| + | indent = 0; | ||
| + | while (caretpos > 0 && pchInputBuf[caretpos] != 0x0D) { //posun na zacatek radku a pocitani o kolik znaku | ||
| + | caretpos--; | ||
| + | indent++; | ||
| + | } | ||
| + | if (caretpos > 0) { | ||
| + | indent--; | ||
| + | } | ||
| + | caretpos++; | ||
| + | while (caretpos < nchLast && pchInputBuf[caretpos] != 0x0D) { //posun na konec radku | ||
| + | // | ||
| + | caretpos++; | ||
| + | } | ||
| + | caretpos++; | ||
| + | // | ||
| + | if (caretpos >= nchLast) { //kdyz jsem na konci textu | ||
| + | // | ||
| + | caretpos = nchLast; | ||
| + | while (caretpos > 0 && pchInputBuf[caretpos] != 0x0D) { //posun na zacatek radku | ||
| + | // | ||
| + | caretpos--; | ||
| + | } | ||
| + | caretpos++; | ||
| + | nCaretPosX = 0; | ||
| + | for (; indent > 0; indent--) { //posun od odsazeni | ||
| + | ch = (TCHAR) pchInputBuf[caretpos]; | ||
| + | if (ch == 0x0D) { | ||
| + | break; | ||
| + | } | ||
| + | hdc = GetDC(hwndMain); | ||
| + | GetCharWidth32(hdc, | ||
| + | nCaretPosX += chWidth; | ||
| + | ReleaseDC(hwndMain, | ||
| + | caretpos++; | ||
| + | } | ||
| + | SetCaretPos(nCaretPosX, | ||
| + | ShowCaret(hwndMain); | ||
| + | break; | ||
| + | } | ||
| + | nCaretPosY++; | ||
| + | while (caretpos > 0 && pchInputBuf[caretpos] != 0x0D) { //posun na zacatek radku | ||
| + | // | ||
| + | caretpos--; | ||
| + | } | ||
| + | caretpos++; | ||
| + | nCaretPosX = 0; | ||
| + | for (; indent > 0; indent--) { //posun od odsazeni | ||
| + | ch = (TCHAR) pchInputBuf[caretpos]; | ||
| + | if (ch == 0x0D) { | ||
| + | break; | ||
| + | } | ||
| + | hdc = GetDC(hwndMain); | ||
| + | GetCharWidth32(hdc, | ||
| + | nCaretPosX += chWidth; | ||
| + | ReleaseDC(hwndMain, | ||
| + | caretpos++; | ||
| + | } | ||
| + | SetCaretPos(nCaretPosX, | ||
| + | ShowCaret(hwndMain); | ||
| + | break; | ||
| + | |||
| + | case VK_HOME: | ||
| + | HideCaret(hwndMain); | ||
| + | caretpos--; | ||
| + | while (caretpos >= 0 && pchInputBuf[caretpos] != 0x0D) { //posun na zacatek radku | ||
| + | // | ||
| + | caretpos--; | ||
| + | } | ||
| + | caretpos++; | ||
| + | nCaretPosX = 0; | ||
| + | SetCaretPos(nCaretPosX, | ||
| + | ShowCaret(hwndMain); | ||
| + | break; | ||
| + | |||
| + | case VK_END: | ||
| + | HideCaret(hwndMain); | ||
| + | while (caretpos < nchLast && pchInputBuf[caretpos] != 0x0D) { | ||
| + | ch = (TCHAR) pchInputBuf[caretpos]; | ||
| + | hdc = GetDC(hwndMain); | ||
| + | GetCharWidth32(hdc, | ||
| + | nCaretPosX += chWidth; | ||
| + | ReleaseDC(hwndMain, | ||
| + | caretpos++; | ||
| + | } | ||
| + | SetCaretPos(nCaretPosX, | ||
| + | ShowCaret(hwndMain); | ||
| + | break; | ||
| + | |||
| + | case VK_DELETE: | ||
| + | |||
| + | break; | ||
| + | } | ||
| + | break; | ||
| + | |||
| + | case WM_SIZE: | ||
| + | GetClientRect(hwndMain, | ||
| + | break; | ||
| + | |||
| + | case WM_SETFOCUS: | ||
| + | |||
| + | // Create, position, and display the caret when the window receives the keyboard focus. | ||
| + | CreateCaret(hwndMain, | ||
| + | SetCaretPos(nCaretPosX, | ||
| + | ShowCaret(hwndMain); | ||
| + | break; | ||
| + | |||
| + | case WM_KILLFOCUS: | ||
| + | |||
| + | // Hide and destroy the caret when the window loses the keyboard focus. | ||
| + | HideCaret(hwndMain); | ||
| + | DestroyCaret(); | ||
| + | break; | ||
| + | |||
| + | case WM_PAINT: | ||
| + | hdc = BeginPaint(hwndMain, | ||
| + | |||
| + | // Set the clipping rectangle, and then draw the text into it. | ||
| + | DrawText(hdc, | ||
| + | |||
| + | SetCaretPos(nCaretPosX, | ||
| + | |||
| + | EndPaint(hwndMain, | ||
| + | break; | ||
| + | |||
| + | |||
| + | case WM_DESTROY: | ||
| + | PostQuitMessage(0); | ||
| + | |||
| + | // Free the input buffer. | ||
| + | GlobalFree((HGLOBAL) pchInputBuf); | ||
| + | break; | ||
| + | |||
| + | default: | ||
| + | return DefWindowProc(hwndMain, | ||
| + | } | ||
| + | } | ||
| + | </ | ||