#include<windows.h>
#include<tchar.h>
#include<iostream>
#include"resource.h" //很重要,要引入资源头文件
void MessageBoxPrintf(char* pszCaputre,char* Format,...)
{
va_list vaList;//equal to Format + sizeof(FOrmat)
char szBuff[100];
memset(szBuff,0,sizeof(char)*100);
va_start(vaList,Format);
//vsPrintf 三个参数 buff,format,参数数组的指针,va_list类型的。这个函数
// 多用于实现多个参数的自定义函数
_vsnprintf(szBuff,100,Format,vaList);
va_end(vaList);
MessageBoxA(NULL,szBuff,pszCaputre,MB_OK);
}
LRESULT CALLBACK WinProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)//LRESULT long型的指针。 CALLBACK _stacall
{
if(uMsg == WM_COMMAND)
{
}
else if(uMsg == WM_DESTROY)
{
PostQuitMessage(0);
return 0; //return 0 此处消息,我自己处理,不需要默认所以直接 return 0;
}
else if(uMsg == WM_CREATE)
{
MessageBoxPrintf("create","%s","CreateWindow");
return 0;
}
return DefWindowProc(hWnd,uMsg,wParam,lParam);
}
INT WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPreInstance,LPSTR lpszCmd,int nCmdShow)//win32 的时候hPreInstance总为NULL,不检查,16位时候检查运行可以共用窗口类
{
WNDCLASS wndClass;
HCURSOR hCursor = LoadCursor(hInstance,MAKEINTRESOURCE(IDC_CURSOR1));//load 参数第一个NULL表示在系统内部找,适合第一默认的,系统共有,自己定义就设为自己句柄
HICON hIocn = LoadIcon(hInstance,MAKEINTRESOURCE(IDI_ICON1));// IDC_CURSOR1 是ID表示转化字符串表示,int 转化为字符串
wndClass.cbClsExtra = 0;
wndClass.cbWndExtra = 0;
wndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndClass.hCursor = hCursor;
wndClass.hIcon =hIocn;
wndClass.hInstance = hInstance; //注意一定要赋值为当前进程的hInstance
wndClass.lpfnWndProc = WinProc;
wndClass.lpszClassName=_T("MyWindow");
wndClass.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);
wndClass.style =CS_HREDRAW|CS_VREDRAW;
if(RegisterClass(&wndClass)==NULL)
MessageBoxPrintf("warning","%s","Register");
HMENU hMenue = LoadMenu(hInstance,MAKEINTRESOURCE(IDR_MENU1));
HWND hWnd = CreateWindowA("MyWindow","mytest",WS_VSCROLL|WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,500,500,NULL,NULL,hInstance,NULL);
if(hWnd==NULL)
MessageBoxPrintf("warning","%s","create error");
ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);
MSG msg;
//NULL
// GetMessage retrieves messages for any window that belongs to the calling thread and thread messages posted to the calling thread using the PostThreadMessage function.
while(GetMessage(&msg,NULL,NULL,NULL))//注意GetMessage(&msg,hWnd,NULL,NULL)是不可以的,NULL表示接受自己创建的所有窗口
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
/*
BOOL bRet;放参数不合法时候会返回-1 ,也就是为什么不对的原因
while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
{
if (bRet == -1)
{
// handle the error and possibly exit
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
*/
return 0;
}
Win32 体会
最新推荐文章于 2017-05-28 21:32:22 发布