C++ WIN32:在程序运行时执行的代码放置在哪里

发布于 2024-11-05 15:27:19 字数 4044 浏览 5 评论 0原文

这似乎是一个简单的问题,但我不知道在哪里放置在程序运行时执行的 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;
}

This seems like a simple question but I don't know where to put Win32 code that executes whilst the program is running. As a simplified example, I provide an example that contains what I believe to be standard Win32 Window initialisation code, followed by a simple 'Beep' command. I have tried slotting the beep command in various different places, but the result is one of the following three:

  1. The beep occurs and loops endlessly
  2. No beep is heard at all
  3. The beep only occurs as I close the program

The code I am using is shown below. This is simply an example I have lifted from an online resource, with my beep command added at the end. There are no compiler errors. In this example, the beep occurs as I close the program. As you might expect, this is my first Win32 app.

#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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝梦月影 2024-11-12 15:27:19

在典型的 Windows 应用程序中,一切都是作为对消息循环中收到的消息的响应而发生的。如果您想在窗口首次打开时发出蜂鸣声,您可以添加 WM_CREATE 消息的处理程序并将代码放在那里。

当您回复消息时,您需要尽快返回,以避免 UI 变得迟缓或无响应。如果你需要做很多工作,你应该创建一个单独的线程来处理工作。

In a typical Windows app, everything happens as a response to a message received in the message loop. If you want to beep once when the window is first opened you can add a handler for WM_CREATE messages and put your code there.

When you respond to a message, you need to return as quickly as possible to avoid making the UI sluggish or non-responsive. If you need to do a lot of work, you should create a separate thread to handle the work.

箜明 2024-11-12 15:27:19

您将 Beep 调用放在 Windows 消息回调中,只要窗口收到消息(例如调整大小、鼠标放在 NC 区域等),就会触发该回调;所以它可能会发射很多。

您可以在 单独的线程以避免冻结 GUI 作为一种解决方法,或者如果您想要单次代码,那么您可以在窗口打开后使用 WM_CREATE 案例消息来运行代码创建的。

也看看这个

You are placing the Beep call in the windows messages callback, which is supposed to trigger whenever the window receives a message (e.g resizing, mouse on nc area etc..); so it will probably fire a LOT.

You can run your code (if the operation is going to be long) in a separate thread to avoid freezing the GUI as a workaround, or if you want a single shot code then you can use the WM_CREATE case message to run your code once the window is created.

Check this out as well

挥剑断情 2024-11-12 15:27:19

简而言之,如果您希望代码在每个窗口消息上运行(您可能不这样做),那么请将 beep 调用放在窗口过程的顶部。如果您希望仅针对某些消息发生这种情况,请为这些消息添加一个 case,并将其放入该 case 中。

Putting it simply, if you want the code to run on every window message (you probably don't) then put the beep call at the top of your window proc. If you want it to occur only for certain messages, then add a case for those messages, and put it in that case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文