Win32 体会

#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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值