2012-10-21 127 views
0

我有3個窗戶,有相互利用的活動進行互動。 窗口1和窗口2是相同的;每個只有一個按鈕。的Win32等待事件(synchroniztion)

基本上,我點擊主窗口(程序3)直到一個另外兩個窗口按鈕 被點擊;這究竟是怎麼回事描述了在實驗室:

您需要使用同步控制過程(通過PROGRAM1或2起節目3,通過關閉節目3結束PROGRAM1或2)。注:PROGRAM1和Program2中必須有自己的按鈕,以便點擊他們接受的信號死

我一直在四處尋找,並得到這個至今代碼:

主窗口(程序3) :

#include <windows.h> 
#include<string.h> 

#include <stdio.h> 
#include <tchar.h> 
#include <conio.h> 
#pragma comment(lib, "winmm.lib") 


LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ; 

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, 
        PSTR szCmdLine, int iCmdShow) 
{ 
    static TCHAR szAppName[] = TEXT ("RACE") ; 
    HWND   hwnd ; 
    MSG   msg ; 
    WNDCLASS  wndclass ; 
    HANDLE hEvents[2]; 
    hEvents[0] = "btn2"; 
    hEvents[1] = "btn3"; 
    DWORD count = 2; 

    HBRUSH brush; 
    brush = CreateSolidBrush(RGB(255,0,0)); 

    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 = brush; 
    wndclass.lpszMenuName = NULL ; 
    wndclass.lpszClassName = szAppName ; 

    if (!RegisterClass (&wndclass)) 
    { 
      MessageBox (NULL, TEXT ("This program requires Windows NT!"), 
         szAppName, MB_ICONERROR) ; 
      return 0 ; 
    } 
    TCHAR* name; 

    //WAIT FOR SIGNAL 
    DWORD result = WaitForMultipleObjects(count,hEvents,FALSE,INFINITE);//work on this 
    if(result == WAIT_OBJECT_0) 
    { 
     name = TEXT("Program 1");  
    } 
    else if(result == WAIT_OBJECT_0 + 1) 
    { 
     name = TEXT("Program 2"); 
    } 

    hwnd = CreateWindow (szAppName,     // window class name 
          name, // window caption 
          WS_OVERLAPPEDWINDOW,  // window style 
          0,    // initial x position 
          0,    // initial y position 
          600,    // initial x size 
          600,    // initial y size 
          NULL,      // parent window handle 
          NULL,      // window menu handle 
          hInstance,     // program instance handle 
          NULL);      // creation parameters 



    ShowWindow (hwnd, iCmdShow) ;//DON'T SHOW UNTIL ANOTHER WINDOW'S BUTTON IS PUSHED. 
    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) 
{ 
    HDC   hdc ; 
    PAINTSTRUCT ps ; 


    TCHAR* carNames[5] = {TEXT("Red Car"), TEXT("Blue Car"), TEXT("Black Car"), TEXT("Green Car"), TEXT("Orange Car")}; 
    switch (message) 
    { 
    case WM_CREATE: 
     HWND hwndButton; 

     for(int i = 0; i < 5; i++) 
     { 
     hwndButton = CreateWindow (TEXT("button"),//type of child window 
            carNames[i],//text displayed on button 
            WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,//type of button 
            20, (20*i*5+10), 
            85, 25, 
            hwnd, //parent handle i.e. main window handle 
            (HMENU) i,//child ID – any number 
            ((LPCREATESTRUCT) lParam)->hInstance, NULL); 
     } 



     break; 


      return 0 ; 

    case WM_PAINT: 
      hdc = BeginPaint (hwnd, &ps) ; 



      EndPaint (hwnd, &ps) ; 
      return 0 ; 

    /* case WM_CLOSE: 

      c--; 
      DestroyWindow(hwnd); 
      return 0 ;*/ 

    case WM_DESTROY: 

      PostQuitMessage (0) ; 
      return 0 ; 
    } 
    return DefWindowProc (hwnd, message, wParam, lParam) ; 
} 

窗口2(程序2):

#include <windows.h> 
    #include<string.h> 
    #pragma comment(lib, "winmm.lib") 

    LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ; 

    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, 
         PSTR szCmdLine, int iCmdShow) 
    { 
     static TCHAR szAppName[] = TEXT ("Part 2") ; 
     HWND   hwnd ; 
     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,     // window class name 
           TEXT ("Part 2"), // window caption 
           WS_OVERLAPPEDWINDOW,  // window style 
           0,    // initial x position 
           0,    // initial y position 
           300,    // initial x size 
           200,    // initial y size 
           NULL,      // parent window handle 
           NULL,      // window menu handle 
           hInstance,     // program instance handle 
          NULL) ;      // creation parameters 



     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) 
    { 
     HDC   hdc ; 
     PAINTSTRUCT ps ; 
     HANDLE hEvent; 


     switch (message) 
     { 
     case WM_CREATE: 
      HWND hwndButton2; 
       hwndButton2 = CreateWindow (TEXT("button"),//type of child window 
             TEXT("PRESS ME!"),//text displayed on button 
             WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,//type of button 
             20, 20, 
             200, 25, 
             hwnd, //parent handle i.e. main window handle 
             (HMENU) 45,//child ID – any number 
             ((LPCREATESTRUCT) lParam)->hInstance, NULL); 

       hEvent = CreateEvent(NULL, //no security attributes 
       FALSE, //auto-reset event object 
       FALSE, //initial state is nonsignaled 
       L"btn2"); //unnamed object 



       return 0 ; 

     case WM_PAINT: 
       hdc = BeginPaint (hwnd, &ps) ; 



       EndPaint (hwnd, &ps) ; 
       return 0 ; 

     case WM_COMMAND: 


      SetEvent("btn2"); 

      return 0; 

     case WM_DESTROY: 

       PostQuitMessage (0) ; 
       return 0 ; 
     } 
     return DefWindowProc (hwnd, message, wParam, lParam) ; 
    } 

窗3(方案3):這是幾乎相同的窗口2

#include <windows.h> 
#include<string.h> 
#pragma comment(lib, "winmm.lib") 

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ; 

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, 
        PSTR szCmdLine, int iCmdShow) 
{ 
    static TCHAR szAppName[] = TEXT ("Part 3") ; 
    HWND   hwnd ; 
    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,     // window class name 
          TEXT ("Part 3"), // window caption 
          WS_OVERLAPPEDWINDOW,  // window style 
          0,    // initial x position 
          0,    // initial y position 
          300,    // initial x size 
          200,    // initial y size 
          NULL,      // parent window handle 
          NULL,      // window menu handle 
          hInstance,     // program instance handle 
         NULL) ;      // creation parameters 



    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) 
{ 
    HDC   hdc ; 
    PAINTSTRUCT ps ; 
    HANDLE hEvent1; 

    switch (message) 
    { 
    case WM_CREATE: 
      HWND hwndButton3; 
      hwndButton3 = CreateWindow (TEXT("button"),//type of child window 
            TEXT("PRESS ME!"),//text displayed on button 
            WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,//type of button 
            20, 20, 
            200, 25, 
            hwnd, //parent handle i.e. main window handle 
            (HMENU) 95,//child ID – any number 
            ((LPCREATESTRUCT) lParam)->hInstance, NULL); 

      hEvent1 = CreateEvent(NULL, //no security attributes 
      FALSE, //auto-reset event object 
      FALSE, //initial state is nonsignaled 
      L"btn3"); //unnamed object 


      return 0 ; 

    case WM_PAINT: 
      hdc = BeginPaint (hwnd, &ps) ; 



      EndPaint (hwnd, &ps) ; 
      return 0 ; 

      case WM_COMMAND: 

     SetEvent("btn3"); 

     return 0; 


    case WM_DESTROY: 

      PostQuitMessage (0) ; 
      return 0 ; 
    } 
    return DefWindowProc (hwnd, message, wParam, lParam) ; 
} 

這些都是在相同溶液中;但在不同的項目中(多個項目在一個解決方案中)。

現在的問題是,我似乎無法讓主窗口中打開,當我點擊按鈕 任何一個。我嘗試過很多不同的東西,但沒有一個能夠工作。

任何和所有幫助表示讚賞。提前致謝。

+1

(1)你的第一PROG具有用於處理子控制/菜單命令沒有WM_COMMAND處理程序。 (2)你的第二個程序幾乎是空白的,但是使用字符串常量而不是有效的事件句柄包含無效的'SetEvent()'調用。你的第三個實際上是第二個的重複,唯一不同是使用不同的(和不正確的)字符串常量的不同的無效SetEvent()調用。簡而言之,除了向庫存WIN32嚮導生成的項目文件添加十幾行代碼之外,這與您分配的任務無關。 – WhozCraig

回答

0
HANDLE hEvents[2]; 
hEvents[0] = "btn2"; 
hEvents[1] = "btn3"; 

這是不對的。您必須調用CreateEvent函數,就像在其他兩個程序中一樣。

+0

我正在另外兩個窗口程序中創建事件。我被告知我將它們放在那裏,然後單擊按鈕時,使用SetEvent設置事件。而事件的名稱將是如何在等待事件時識別它(WaitForMultipleObjects)。 所以我認爲我必須把這些具體的事件到手柄陣列。但我無法弄清楚如何做到這一點。 不會創建一個新的事件是一個不同的事件,而不是我需要的事件? –

+0

我相信你很容易混淆*真實世界*事件,比如在物理桌面上用物理鼠標點擊按鈕,以及同步原語。 – ixe013

+0

@LuciferFayte:閱讀有關命名事件如何工作的文檔。使用相同的名稱將爲現有事件創建一個新的句柄。 –