Winapi win32正在等待事件(同步)

Winapi win32正在等待事件(同步),winapi,events,synchronization,Winapi,Events,Synchronization,我有3个窗口,它们必须使用事件相互交互。 窗口1和窗口2是相同的;每个按钮中只有一个按钮 基本上,我希望主窗口(程序3)在其他两个窗口按钮中的一个按钮出现之前不显示 点击;以下是实验室中对其的描述: 您需要使用同步来控制进程(通过Program1或Program2启动Program3,通过关闭Program3结束Program1或Program2)。注意:Program1和Program2必须单击按钮才能接收死亡信号 我一直在四处寻找,到目前为止,我得到了以下代码: 主窗口(程序3): #inc

我有3个窗口,它们必须使用事件相互交互。 窗口1和窗口2是相同的;每个按钮中只有一个按钮

基本上,我希望主窗口(程序3)在其他两个窗口按钮中的一个按钮出现之前不显示 点击;以下是实验室中对其的描述:

您需要使用同步来控制进程(通过Program1或Program2启动Program3,通过关闭Program3结束Program1或Program2)。注意: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) ;
}
    #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) ;
    }
#包括
#包括
#包括
#包括
#包括
#pragma注释(lib,“winmm.lib”)
LRESULT回调WndProc(HWND、UINT、WPARAM、LPARAM);
int WINAPI WinMain(HINSTANCE HINSTANCE、HINSTANCE HPPreInstance、,
PSTR szCmdLine,int iCmdShow)
{
静态TCHAR szAppName[]=文本(“种族”);
HWND-HWND;
味精;
WNDCLASS WNDCLASS;
处理事件[2];
hEvents[0]=“btn2”;
hEvents[1]=“btn3”;
德沃德计数=2;
HBRUSH刷;
笔刷=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(空,IDI_应用程序);
wndclass.hCursor=LoadCursor(空,IDC_箭头);
wndclass.hbrBackground=刷子;
wndclass.lpszMenuName=NULL;
wndclass.lpszClassName=szAppName;
if(!RegisterClass(&wndclass))
{
MessageBox(空,文本(“此程序需要Windows NT!”),
szAppName,MB_i错误);
返回0;
}
TCHAR*名称;
//等待信号
DWORD result=WaitForMultipleObjects(count、hEvents、FALSE、INFINITE);//处理此问题
如果(结果==等待对象0)
{
名称=文本(“程序1”);
}
else if(结果==等待对象0+1)
{
名称=文本(“程序2”);
}
hwnd=CreateWindow(szAppName,//窗口类名称
名称,//窗口标题
WS\u重叠窗口,//窗口样式
0,//初始x位置
0,//初始y位置
600,//初始x尺寸
600,//初始y尺寸
NULL,//父窗口句柄
NULL,//窗口菜单句柄
hInstance,//程序实例句柄
NULL);//创建参数
ShowWindow(hwnd,iCmdShow);//在按下另一个窗口的按钮之前不显示。
更新窗口(hwnd);
while(GetMessage(&msg,NULL,0,0))
{
翻译信息(&msg);
发送消息(&msg);
}
返回msg.wParam;
}
LRESULT回调WndProc(HWND HWND,UINT消息,WPARAM WPARAM,LPARAM LPARAM)
{
HDC-HDC;
PAINTSTRUCT-ps;
TCHAR*carNames[5]={TEXT(“红色汽车”)、TEXT(“蓝色汽车”)、TEXT(“黑色汽车”)、TEXT(“绿色汽车”)、TEXT(“橙色汽车”)};
开关(信息)
{
案例WM_创建:
HWND HWND按钮;
对于(int i=0;i<5;i++)
{
hwndButton=CreateWindow(文本(“按钮”),//子窗口的类型
carNames[i],//按钮上显示的文本
WS|U CHILD | WS|U VISIBLE | BS|U按钮,//按钮类型
20,(20*i*5+10),
85, 25,
hwnd,//父句柄,即主窗口句柄
(humenu)i,//儿童ID–任意数字
((LPCREATESTRUCT)lParam)->hInstance,NULL);
}
打破
返回0;
案例WM_油漆:
hdc=开始喷漆(hwnd和ps);
端漆(hwnd和ps);
返回0;
/*案例WM_结束:
c--;
窗口(hwnd);
返回0*/
案例WM_销毁:
PostQuitMessage(0);
返回0;
}
返回DefWindowProc(hwnd、message、wParam、lParam);
}
窗口2(程序2):

#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) ;
}
    #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) ;
    }
#包括
#包括
#pragma注释(lib,“winmm.lib”)
LRESULT回调WndProc(HWND、UINT、WPARAM、LPARAM);
int WINAPI WinMain(HINSTANCE HINSTANCE、HINSTANCE HPPreInstance、,
PSTR szCmdLine,int iCmdShow)
{
静态TCHAR szAppName[]=文本(“第2部分”);
HWND-HWND;
味精;
WNDCLASS WNDCLASS;
wndclass.style=CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc=WndProc;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hInstance=hInstance;
wndclass.hIcon=LoadIcon(空,IDI_应用程序);
wndclass.hCursor=LoadCursor(空,IDC_箭头);
wndclass.hbrBackground=(HBRUSH)GetStockObject(白色画笔);
wndclass.lpszMenuName=NULL;
wndclass.lpszClassName=szAppName;
if(!RegisterClass(&wndclass))
{
MessageBox(空,文本(“此程序需要Windows NT!”),
szAppName,MB_i错误);
返回0;
}
hwnd=CreateWindow(szAppName,//窗口类名称
文本(“第2部分”),//窗口标题
WS\u重叠窗口,//窗口样式