Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.02 KB | None | 0 0
  1. #include<iostream>
  2. #include <windows.h>
  3. #include <stdio.h>
  4. #include <commctrl.h>
  5. #include <tchar.h>
  6.  
  7. LRESULT WINAPI WndProc(HWND, UINT, WPARAM, LPARAM);
  8. long width;
  9. long height;
  10. HDC dc;
  11. HWND w;
  12. STARTUPINFO si;
  13. PROCESS_INFORMATION pi;
  14. char Istr[50];
  15. int global = 5;
  16. CRITICAL_SECTION sect1, sect2;
  17. static HWND hList1, hList2, hButton1, hButton2;
  18.  
  19. DWORD WINAPI Thread1(LPVOID) {
  20.     EnterCriticalSection(&sect1);
  21.     for (int i = 0; i < 50; i++){
  22.         wsprintf(Istr, L"%d", global);
  23.         SendMessageA(hList1, LB_ADDSTRING, (WPARAM)0, (LPARAM)Istr);
  24.         global++;
  25.     }
  26.     EnterCriticalSection(&sect2);
  27.     LeaveCriticalSection(&sect2);
  28.     LeaveCriticalSection(&sect1);
  29.     return 0;
  30. }
  31.  
  32. DWORD WINAPI Thread2(LPVOID) {
  33.     EnterCriticalSection(&sect2);
  34.     EnterCriticalSection(&sect1);
  35.     for (int i = 0; i < 50; i++){
  36.         wsprintf(Istr, L"%d", global);
  37.         SendMessageA(hList2, LB_ADDSTRING, (WPARAM)0, (LPARAM)Istr);
  38.         global++;
  39.     }
  40.     LeaveCriticalSection(&sect1);
  41.     LeaveCriticalSection(&sect2);
  42.     return 0;
  43. }
  44.  
  45.  
  46. int main(){
  47.     HINSTANCE histance = GetModuleHandleW(NULL);
  48.     WNDCLASSEX wclass = { 0 };
  49.     wclass.cbSize = sizeof(WNDCLASSEX);
  50.     wclass.style = CS_HREDRAW | CS_VREDRAW;
  51.     wclass.lpfnWndProc = WndProc;
  52.     wclass.cbClsExtra = 0;
  53.     wclass.cbWndExtra = 0;
  54.     wclass.hInstance = histance;
  55.     wclass.hIcon = LoadIcon(0, IDI_APPLICATION);
  56.     wclass.hCursor = LoadCursor(0, IDC_ARROW);
  57.     wclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  58.     wclass.lpszMenuName = NULL;
  59.     wclass.lpszClassName = "Test";
  60.     RegisterClassEx(&wclass);
  61.     w = CreateWindowExW(
  62.         0,
  63.         L"Test",
  64.         L"Form",
  65.         WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
  66.         CW_USEDEFAULT,
  67.         CW_USEDEFAULT,
  68.         300,
  69.         350,
  70.         0,
  71.         0,
  72.         histance,
  73.         0);
  74.     ShowWindow(w, SW_SHOW);
  75.     UpdateWindow(w);
  76.     MSG msg = { 0 };
  77.     while (GetMessage(&msg, 0, 0, 0)) {
  78.         TranslateMessage(&msg);
  79.         DispatchMessage(&msg);
  80.     }
  81.     return 0;
  82. }
  83.  
  84. LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam){
  85.     PAINTSTRUCT MyPaint;
  86.     HBRUSH Brush;
  87.     HPEN Pen;
  88.     HANDLE hThread1, hThread2;
  89.     DWORD IDThread1, IDThread2;
  90.  
  91.     switch (msg) {
  92.     case WM_DESTROY:
  93.         PostQuitMessage(0);
  94.         break;
  95.     case WM_PAINT:
  96.  
  97.         break;
  98.     case WM_CLOSE:
  99.  
  100.         break;
  101.     case WM_CREATE:
  102.         hList1 = CreateWindow(L"LISTBOX", L"", WS_VISIBLE | WS_CHILD | WS_BORDER | LBS_STANDARD, 0, 30, 100, 200, hWnd, HMENU(NULL), NULL, NULL);
  103.         hList2 = CreateWindow(L"LISTBOX", L"", WS_VISIBLE | WS_CHILD | WS_BORDER | LBS_STANDARD, 105, 30, 100, 200, hWnd, HMENU(NULL), NULL, NULL);
  104.         hButton1 = CreateWindow(L"BUTTON", L"Запустить", WS_VISIBLE | WS_CHILD, 0, 250, 110, 20, hWnd, HMENU(NULL), NULL, NULL);
  105.         InitializeCriticalSection(&sect1);
  106.         InitializeCriticalSection(&sect2);
  107.        
  108.         break;
  109.     case WM_COMMAND:
  110.         if (lParam == (LPARAM)hButton1) {
  111.             hThread1 = CreateThread(NULL, 0, Thread1, NULL, 0, &IDThread1);
  112.             hThread2 = CreateThread(NULL, 0, Thread2, NULL, 0, &IDThread2);
  113.         }
  114.         if (lParam == (LPARAM)hButton2) {
  115.             LeaveCriticalSection(&sect2);
  116.         }
  117.         break;
  118.     }
  119.     return DefWindowProc(hWnd, msg, wParam, lParam);
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement