Zde můžete vidět rozdíly mezi vybranou verzí a aktuální verzí dané stránky.
| Následující verze | Předchozí verze | ||
| pitel:itu:cviceni1 [03. 07. 2012, 11.53:41] – upraveno mimo DokuWiki 127.0.0.1 | pitel:itu:cviceni1 [30. 12. 2022, 13.43:01] (aktuální) – upraveno mimo DokuWiki 127.0.0.1 | ||
|---|---|---|---|
| Řádek 1: | Řádek 1: | ||
| + | ====== Událostmi řízená aplikace (WinAPI) ====== | ||
| + | [[http:// | ||
| + | <file c main.c> | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | // Global variables | ||
| + | HINSTANCE hInst; | ||
| + | UINT uMyMsgWndMoving; | ||
| + | HINSTANCE mhInst; | ||
| + | HWND mhWnd; // handle to own window (initialize in WinMain) | ||
| + | HDC mhDC; // device context (initialize in each Paint function calling) | ||
| + | |||
| + | // Function prototypes. | ||
| + | int WINAPI WinMain(HINSTANCE, | ||
| + | LRESULT CALLBACK MainWndProc(HWND, | ||
| + | void onPaint(HWND hWnd); | ||
| + | BOOL CALLBACK PrintEnumWindowsProc(HWND hWnd,LPARAM lParam); | ||
| + | |||
| + | // Application entry point. | ||
| + | int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, | ||
| + | MSG msg; | ||
| + | BOOL bRet; | ||
| + | WNDCLASS wcx; // register class | ||
| + | | ||
| + | hInst = hInstance; // Save the application-instance handle. | ||
| + | uMyMsgWndMoving = RegisterWindowMessage(" | ||
| + | | ||
| + | // Fill in the window class structure with parameters that describe the main window. | ||
| + | wcx.style = CS_HREDRAW | CS_VREDRAW; // redraw if size changes | ||
| + | wcx.lpfnWndProc = (WNDPROC) MainWndProc; | ||
| + | wcx.cbClsExtra = 0; // no extra class memory | ||
| + | wcx.cbWndExtra = 0; // no extra window memory | ||
| + | wcx.hInstance = hInstance; // handle to instance | ||
| + | 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 | ||
| + | mhWnd = CreateWindow(" | ||
| + | if (!mhWnd) { | ||
| + | return FALSE; | ||
| + | } | ||
| + | | ||
| + | // Show the window and send a WM_PAINT message to the window procedure. | ||
| + | // Record the current cursor position. | ||
| + | ShowWindow(mhWnd, | ||
| + | UpdateWindow(mhWnd); | ||
| + | | ||
| + | while ((bRet = GetMessage(& | ||
| + | if (bRet == -1) { | ||
| + | // handle the error and possibly exit | ||
| + | } else { | ||
| + | TranslateMessage(& | ||
| + | DispatchMessage(& | ||
| + | } | ||
| + | } | ||
| + | | ||
| + | PostMessage(HWND_BROADCAST, | ||
| + | return (int) msg.wParam; | ||
| + | } | ||
| + | |||
| + | LRESULT CALLBACK MainWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { | ||
| + | InvalidateRect(hWnd, | ||
| + | | ||
| + | // Some of the application instance is moving | ||
| + | if (uMsg == uMyMsgWndMoving) { | ||
| + | InvalidateRect(hWnd, | ||
| + | return 0; | ||
| + | } | ||
| + | | ||
| + | switch (uMsg) { | ||
| + | case WM_CREATE: | ||
| + | // Initialize the window. | ||
| + | return 0; | ||
| + | | ||
| + | case WM_SIZE: | ||
| + | // Set the size and position of the window. | ||
| + | PostMessage(HWND_BROADCAST, | ||
| + | return 0; | ||
| + | | ||
| + | case WM_MOVE: | ||
| + | // Set the new window position. (Send it to all application instances.) | ||
| + | PostMessage(HWND_BROADCAST, | ||
| + | return 0; | ||
| + | | ||
| + | case WM_PAINT: | ||
| + | // Paint the window' | ||
| + | onPaint(hWnd); | ||
| + | return 0; | ||
| + | | ||
| + | case WM_DESTROY: | ||
| + | // Clean up window-specific data objects. | ||
| + | PostQuitMessage(0); | ||
| + | return 0; | ||
| + | | ||
| + | default: | ||
| + | return DefWindowProc(hWnd, | ||
| + | } | ||
| + | } | ||
| + | |||
| + | void onPaint(HWND hWnd) { | ||
| + | PAINTSTRUCT ps; // information can be used to paint the client area of a window owned by that application | ||
| + | mhDC = BeginPaint(hWnd, | ||
| + | EnumWindows(& | ||
| + | DeleteDC(mhDC); | ||
| + | EndPaint(hWnd, | ||
| + | } | ||
| + | |||
| + | BOOL CALLBACK PrintEnumWindowsProc(HWND hWnd, LPARAM lParam) { | ||
| + | char WClassName[256]; | ||
| + | char mWClassName[256]; | ||
| + | RECT myRect, otherRect; | ||
| + | | ||
| + | GetClassName(hWnd, | ||
| + | GetClassName(mhWnd, | ||
| + | | ||
| + | if ((strcmp(WClassName, | ||
| + | GetWindowRect(mhWnd, | ||
| + | GetWindowRect(hWnd, | ||
| + | |||
| + | int myX = (myRect.right - myRect.left) / 2; | ||
| + | int myY = (myRect.bottom - myRect.top) / 2; | ||
| + | int otherX = otherRect.left - myRect.left + (otherRect.right - otherRect.left) / 2; | ||
| + | int otherY = otherRect.top - myRect.top + (otherRect.bottom - otherRect.top) / 2; | ||
| + | MoveToEx(mhDC, | ||
| + | LineTo(mhDC, | ||
| + | } | ||
| + | | ||
| + | return TRUE; | ||
| + | } | ||
| + | </ | ||