这似乎是一个简单的问题,但我不知道在程序运行时将Win32代码放在何处。作为一个简化示例,我提供了一个示例,其中包含我认为是标准的Win32 Window初始化代码,然后是简单的“Beep”命令。我尝试在各种不同的地方插入beep命令,但结果是以下三种之一:
我正在使用的代码如下所示。这只是我从在线资源中提取的一个例子,最后添加了我的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;
}
答案 0 :(得分:2)
在典型的Windows应用程序中,一切都是作为对消息循环中收到的消息的响应而发生的。如果您想在首次打开窗口时发出一次哔声,可以为WM_CREATE消息添加处理程序并将代码放在那里。
当您回复消息时,您需要尽快返回,以避免UI缓慢或无响应。如果你需要做很多工作,你应该创建一个单独的线程来处理工作。
答案 1 :(得分:1)
您正在Windows消息回调中放置Beep调用,该窗口消息回调应该在窗口收到消息时触发(例如调整大小,鼠标在nc区域等等)。所以它可能会爆发很多。
您可以在separate thread中运行代码(如果操作将很长),以避免冻结GUI作为变通方法,或者如果您需要单次代码,则可以使用WM_CREATE案例消息创建窗口后运行代码。
答案 2 :(得分:0)
简单地说,如果你想让代码在每个窗口消息上运行(你可能没有),那么就把哔声调用放在窗口proc的顶部。如果您希望仅针对特定消息发生消息,请为这些消息添加case
,并将其置于该情况下。