c++ WIN32:放置在程序运行时执行的代码的地方

C++ WIN32: where to put code that executes as program is running

本文关键字:执行 代码 运行时 程序 WIN32 c++      更新时间:2023-10-16

这似乎是一个简单的问题,但我不知道在哪里放置Win32代码执行,而程序正在运行。作为一个简化的示例,我提供了一个示例,其中包含我认为是标准的Win32窗口初始化代码,后跟一个简单的"Beep"命令。我尝试在不同的地方插入beep命令,但结果是以下三个之一:

  1. 哔哔声响起,循环不断
  2. 没有听到任何蜂鸣声
  3. 哔哔声只在我关闭程序时出现
我使用的代码如下所示。这只是我从一个在线资源中获取的一个示例,最后添加了beep命令。没有编译器错误。在这个例子中,当我关闭程序时,哔哔声响起。如你所料,这是我的第一个Win32应用。
#include <windows.h>
    /*  Declare Windows procedure  */
    LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
    /*  Make the class name into a global variable  */
    char szClassName[ ] = "WindowsApp";
    int WINAPI WinMain (HINSTANCE hThisInstance,
                HINSTANCE hPrevInstance,
                LPSTR lpszArgument,
                int nFunsterStil)
{
                    HWND hwnd;               /* This is the handle for our window */
                    MSG messages;            /* Here messages to the application are saved */
                    WNDCLASSEX wincl;        /* Data structure for the windowclass */
       /* The Window structure */
       wincl.hInstance = hThisInstance;
       wincl.lpszClassName = szClassName;
       wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
       wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
       wincl.cbSize = sizeof (WNDCLASSEX);
       /* Use default icon and mouse-pointer */
       wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
       wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
       wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
       wincl.lpszMenuName = NULL;                 /* No menu */
       wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
       wincl.cbWndExtra = 0;                      /* structure or the window instance */
       /* Use Windows's default color as the background of the window */
       wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
       /* Register the window class, and if it fails quit the program */
       if (!RegisterClassEx (&wincl))
               return 0;
       /* The class is registered, let's create the program*/
       hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           "Matt's Program That Beeps",       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           544,                 /* The programs width */
           375,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
            );
       /* Make the window visible on the screen */
       ShowWindow (hwnd, nFunsterStil);
       /* Run the message loop. It will run until GetMessage() returns 0 */
       while (GetMessage (&messages, NULL, 0, 0))
       {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
       }
       /* The program return-value is 0 - The value that PostQuitMessage() gave */
       return messages.wParam;
}
/*  This function is called by the Windows function DispatchMessage()  */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
 switch (message)                  /* handle the messages */
 {
    case WM_DESTROY:
        PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
        break;
    default:                      /* for messages that we don't deal with */
        return DefWindowProc (hwnd, message, wParam, lParam);
}
    Beep( 750, 300 );              /* This is the beep command */
    return 0;
}

在典型的Windows应用程序中,所有事情都是对消息循环中收到的消息的响应。如果你想在窗口第一次打开时发出哔哔声,你可以为WM_CREATE消息添加一个处理程序,并把你的代码放在那里。

当您响应消息时,您需要尽可能快地返回,以避免使UI缓慢或无响应。如果您需要做很多工作,您应该创建一个单独的线程来处理这些工作。

你正在把Beep调用在窗口消息回调,这应该是触发每当窗口收到一个消息(例如调整大小,鼠标在nc区域等);所以它可能会触发很多。

你可以在一个单独的线程中运行你的代码(如果操作将会很长),以避免将GUI冻结作为一个解决方案,或者如果你想要一个单一的镜头代码,那么你可以使用WM_CREATE案例消息在窗口创建后运行你的代码。

也看看这个

简单地说,如果您希望代码在每个窗口消息上运行(您可能不希望),那么将beep调用放在窗口进程的顶部。如果您希望它仅对某些消息发生,那么为这些消息添加case,并将其放在这种情况下。