我正在使用win32 API在Windows 7 64位操作系统上进行实验。当我在 Debug模式下运行我的程序时,出现窗口,但是我有2个bug。首先,这是我从Charles Petzold的书Ch3修改的api代码:
#include<Windows.h>
#include<mmsystem.h>
LRESULT CALLBACK HandleMyWindowsClassMessages(HWND WindowHandle, UINT Message, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPreviousInstance, PSTR szCmdLine, int iCmdShow)
{
WNDCLASSEX MyWindowsClass;
static wchar_t szMyWindowsClassName[] = TEXT("The Name of My Window Class");
static wchar_t szAppName[] = TEXT("AppName");
HWND myWindowHandle;
MSG msg;
MyWindowsClass.hInstance = hInstance;
MyWindowsClass.style = CS_HREDRAW | CS_VREDRAW;
MyWindowsClass.lpfnWndProc = HandleMyWindowsClassMessages;
MyWindowsClass.lpszClassName = szMyWindowsClassName;
MyWindowsClass.cbSize = sizeof(WNDCLASSEX);
MyWindowsClass.cbClsExtra = 0;
MyWindowsClass.cbWndExtra = 0;
MyWindowsClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
MyWindowsClass.hCursor = LoadCursor(NULL, IDC_ARROW);
MyWindowsClass.hIcon = LoadIcon(NULL, IDI_SHIELD);
MyWindowsClass.hIconSm = NULL;
MyWindowsClass.lpszMenuName = NULL;
if (!RegisterClassEx(&MyWindowsClass))
{
MessageBoxEx(0, TEXT("This Programm Requires WINNT!"), szMyWindowsClassName, MB_ICONERROR, 0);
return(0);
}
myWindowHandle = CreateWindowEx(
WS_EX_OVERLAPPEDWINDOW,
szMyWindowsClassName,
szAppName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(myWindowHandle, iCmdShow);
UpdateWindow(myWindowHandle);
while (GetMessage(&msg, myWindowHandle, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return(msg.wParam);
}
LRESULT CALLBACK HandleMyWindowsClassMessages(HWND WindowHandle, UINT Message, WPARAM wParam, LPARAM lParam)
{
//wchar_t szGreeting[] = TEXT("Heeeey");
HDC hdc;
PAINTSTRUCT ps;
switch (Message)
{
case WM_CREATE:
{
PlaySound(TEXT("D:\\mp3\\aywy._&_EphRem_-_Adderall.wav"), NULL, SND_FILENAME | SND_ASYNC);
return(0);
} break;
case WM_PAINT:
{
hdc = BeginPaint(WindowHandle, &ps);
DrawText(hdc, TEXT("Hello Win 7!!!"), -1, &ps.rcPaint, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
//TextOut(hdc, 0, 0, szGreeting, (int)wcslen(szGreeting));
EndPaint(WindowHandle, &ps);
return(0);
}break;
case WM_DESTROY:
{
PostQuitMessage(0);
return(0);
}break;
}
return(DefWindowProc(WindowHandle, Message, wParam, lParam));
}
Bug1:即使我关闭了Window窗口,PlaySound仍会继续播放波形。当我将原始示例与纯WNDCLASS一起使用时,该错误消失了!
因此,使用WNDCLASSEX ???我一定做错了什么。
同样在结束调试的bug中,我必须按
Shift+F5
。Bug2:TextOut与DrawText
信息:我只有1个屏幕。我在笔记本电脑上编程。
当我使用drawText且窗口中的文本超出桌面屏幕边框时,文本不会更新正确性。
当我使用TextOut时,错误消失了!!!
为什么???
下面,我命令一些说明来解释这一点。
任何2个错误中的任何帮助将不胜感激。
我的WndProc的第二版:
LRESULT CALLBACK HandleMyWindowsClassMessages(HWND WindowHandle, UINT Message, WPARAM wParam, LPARAM lParam)
{
//wchar_t szGreeting[] = TEXT("Heeeey");
HDC hdc;
PAINTSTRUCT ps;
RECT rc;
rc.left = 50; rc.top = 100; rc.right = 200; rc.bottom = 200;
switch (Message)
{
case WM_CREATE:
{
PlaySound(TEXT("D:\\mp3\\aywy._&_EphRem_-_Adderall.wav"), NULL, SND_FILENAME | SND_ASYNC);
return(0);
} break;
case WM_PAINT:
{
hdc = BeginPaint(WindowHandle, &ps);
DrawText(hdc, TEXT("Hello Win 7!!!"), -1, &rc, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
//TextOut(hdc, 0, 0, szGreeting, (int)wcslen(szGreeting));
EndPaint(WindowHandle, &ps);
return(0);
}break;
case WM_DESTROY:
{
PlaySound(NULL, NULL, SND_FILENAME | SND_ASYNC);
PostQuitMessage(0);
return(0);
}break;
}
return(DefWindowProc(WindowHandle, Message, wParam, lParam));
}
最佳答案
PlaySound( NULL, NULL, SND_FILENAME | SND_ASYNC)
之前调用PostQuitMessage
以停止声音。 DrawText
-请注意,BeginPaint
返回需要重绘区域的矩形rcPaint
。如果将其用于DrawText
的起点,则文本将出现在不同的位置。例如-首次创建时,该区域将从0,0开始。然后,您可以在DrawText应用程序窗口上移动另一个窗口,并且它可能需要从80,100点重新绘制到右下角。尝试使用绝对(客户)坐标,例如定义RECT rc
并设置rc.left = 50; rc.top = 100; rc.right = 200; rc.bottom = 200;
(总是相同的位置)。 关于c - 在WM_DESTROY和DrawText问题之后,PlaySound()继续播放我的.wav,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28508248/