c++ WIN32:放置在程序运行时执行的代码的地方
C++ WIN32: where to put code that executes as program is running
这似乎是一个简单的问题,但我不知道在哪里放置Win32代码执行,而程序正在运行。作为一个简化的示例,我提供了一个示例,其中包含我认为是标准的Win32窗口初始化代码,后跟一个简单的"Beep"命令。我尝试在不同的地方插入beep命令,但结果是以下三个之一:
- 哔哔声响起,循环不断
- 没有听到任何蜂鸣声
- 哔哔声只在我关闭程序时出现
#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
,并将其放在这种情况下。
相关文章:
- 需要将此代码更改为执行代码
- 当前不会命中断点。没有调试器目标代码类型的可执行代码与此文件关联
- 单步执行代码时重复上一行
- 如何使用介子在C++中执行代码覆盖?
- Visual Studio 2017,C++,在单步执行代码时指向错误的行
- 在 R 中执行C++代码
- 通过 dll 注入在主线程中执行代码
- 无法在 c++ 中循环后执行代码
- 执行C 代码时快速频繁的文件访问
- 执行 C++ 代码后出错
- 一个人如何从代表函数的字符串中执行运行时执行C 代码
- 是否可以在程序崩溃后执行代码?
- 第一次在 Linux 上执行 c++ 代码的时间非常慢
- 计算 JSON 中的条目数并相应地执行代码
- 将在 CATCH 块之后执行代码
- 分析执行C++代码的每一行所花费的确切时间
- 我在执行代码时不断得到"Bus Error"?
- c while()..执行代码行的条件
- 从并行线程在主 Maya 线程上执行代码
- 在调用GNUPLOT之后,如何继续执行C 代码