|
楼主 |
发表于 2013-10-15 21:03:59
|
显示全部楼层
仰望天上的光 发表于 2013-10-15 20:19
不好意思,之前解释错了。其实DispatchMessage()基本上也就是做了一些工作后再调用窗口过程。所以,我个人 ...
用这段代码就有问题:
#include<windows.h>
#include<stdlib.h>
LRESULT CALLBACK WndProc (HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain
(
IN HINSTANCE hInstance,
HINSTANCE hPrevInstance,
PSTR szCmdLine, //LPCTSTR多字节与宽字节通用的类型,PSTR为char类型
int iCmdShow
)
{
static TCHAR szAppName[]=TEXT("hello windows");
HWND hwnd,hwnd2; //窗口句柄
MSG msg; //消息结构
WNDCLASS wndclass; //窗口类结构
char s[100];
int Er;
wndclass.style =CS_HREDRAW|CS_VREDRAW; //窗口类的风格,准确的说叫属性
wndclass.lpfnWndProc =WndProc; //窗口过程指针
wndclass.cbClsExtra =0;
wndclass.cbWndExtra =0;
wndclass.hInstance =hInstance;//实例句柄,由主函数传入的参数
wndclass.hIcon =LoadIcon(NULL,IDI_QUESTION);//图标
wndclass.hCursor =LoadCursor(NULL,IDC_ARROW );
wndclass.hbrBackground =(HBRUSH) GetStockObject(BLACK_BRUSH);
wndclass.lpszMenuName =NULL;
wndclass.lpszClassName =szAppName;//窗口类名
if(!RegisterClass(&wndclass))
{
Er=GetLastError();
wsprintf(s,"窗口注册没有成功,错误代码:%d",Er);
//MessageBox(NULL,TEXT("窗口注册没有成功"),szAppName,0);
MessageBox(NULL,s,szAppName,0);
return 0;
}
Er=GetLastError();
wsprintf(s,"RegisterClass,错误代码:%d",Er);
//MessageBox(NULL,TEXT("窗口注册没有成功"),szAppName,0);
MessageBox(NULL,s,szAppName,0);
hwnd=CreateWindow(
szAppName,//窗口类名
TEXT("The Hello Program 哈哈哈"), //窗口标题
WS_OVERLAPPEDWINDOW, //窗口风格
CW_USEDEFAULT, //初始坐标x
CW_USEDEFAULT, //初始坐标y
CW_USEDEFAULT, //初始大小x
CW_USEDEFAULT, //初始大小y
NULL,
NULL,
hInstance,
NULL
);
Er=GetLastError();
wsprintf(s,"CreateWindow,错误代码:%d",Er);
//MessageBox(NULL,TEXT("窗口注册没有成功"),szAppName,0);
MessageBox(NULL,s,szAppName,0);
hwnd2=CreateWindow(
TEXT("Static"),//窗口类名
TEXT("哈哈,这是按钮"), //窗口标题
WS_CHILD|WS_BORDER , //窗口风格
100, //初始坐标x
100, //初始坐标y
300, //初始大小x
100, //初始大小y
hwnd,
NULL,
hInstance,
NULL
);
ShowWindow(hwnd,iCmdShow);
UpdateWindow(hwnd);
ShowWindow(hwnd2,iCmdShow);
UpdateWindow(hwnd2);
//MessageBox(NULL,TEXT("hello,windows 98!"),TEXT("HelloMsg"),0);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
//DispatchMessage(&msg);
WndProc (hwnd,msg.message ,msg.wParam,msg.lParam);
}
//Sleep(5000);
return msg.wParam;
}
LRESULT CALLBACK WndProc (HWND hwnd,UINT message ,WPARAM wParam,LPARAM lParam)//此函数由操作系统调用,根据消息不同实现不现功能
{
char s[100];
HDC hdc; //设备环境句柄
PAINTSTRUCT ps; //绘图结构
RECT rect; //矩形结构
switch(message)
{
case WM_DESTROY:
{
wsprintf(s,"%p",hwnd);
MessageBox(NULL, s,TEXT("弹窗提示"),0);
PostQuitMessage (0) ;
return 0;
}
case WM_CREATE:
{
PlaySound(TEXT("HelloWin.wav"),NULL,SND_FILENAME|SND_ASYNC);
return 0;
}
case WM_PAINT:
{
//Sleep(2000);
PlaySound(TEXT("HelloWin.wav"),NULL,SND_FILENAME|SND_ASYNC);
wsprintf(s,"%p 消息:%d",hwnd,message);
//MessageBox(NULL, s,TEXT("弹窗提示"),0);
hdc=BeginPaint(hwnd,&ps);
GetClientRect(hwnd,&rect);
//PlaySound(TEXT("HelloWin.wav"),NULL,SND_FILENAME|SND_ASYNC);
DrawText(hdc,TEXT("哈哈哈!66666666666666666666"),-1,&rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
EndPaint(hwnd,&ps);
return 0;
}
}
return DefWindowProc (hwnd, message, wParam, lParam);
}
|
|