现在的位置: 首页 > 综合 > 正文

vc编程笔记0

2013年10月08日 ⁄ 综合 ⁄ 共 2239字 ⁄ 字号 评论关闭

   并不是新学vc编程,这次是在看孙鑫的视频,所以记点笔记。同时也学习一些vc的编程方法,比如首先下载vc小助手和msdn,多查msdn,查msdn的前提对那个函数有个大概的印象。

     创建一个窗口的四个步骤:

     1.首先注册窗口类。

     2.创建窗口。

     3.显示和更新窗口。

     4.消息队列。

#include <stdio.h>
#include <windows.h>
LRESULT CALLBACK WinProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);

#define BUF_LEN 256
int WINAPI WinMain(
            HINSTANCE hInstance, // handle to current instance
            HINSTANCE hPrevInstance, // handle to previous instance
            LPSTR lpCmdLine, // command line
            int nCmdShow // show state
        )
{
   HWND hWnd ; 
   WNDCLASS wcs ;
   MSG msg ;
   int r ;

   char szbuf[BUF_LEN];

    //注册窗口类
   wcs.style = CS_HREDRAW | CS_VREDRAW ;
    wcs.lpfnWndProc = WinProc ;
    wcs.cbClsExtra = 0 ;
    wcs.cbWndExtra = 0 ;
    wcs.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wcs.hCursor = LoadCursor(hInstance,IDC_ARROW);
    wcs.hIcon = LoadIcon(hInstance,IDI_APPLICATION);
    wcs.hInstance = hInstance ;
    wcs.lpszMenuName = NULL ;
    wcs.lpszClassName ="WndClass";
    r = RegisterClass(&wcs);
   if ( 0 == r )
   {
      sprintf(szbuf,"register class error:%d",GetLastError());
      MessageBox(NULL,szbuf,"error",MB_OK);
      exit(0);
      return FALSE ;
   }

    //创建窗口
    hWnd = CreateWindow("WndClass","test it",WS_OVERLAPPEDWINDOW&~WS_THICKFRAME,100,100,600,600,NULL,NULL,hInstance,NULL);
    if ( NULL == hWnd )
    {
        sprintf(szbuf,"createWindow error:%d",GetLastError());
        MessageBox(NULL,szbuf,"error",MB_OK);
        exit(0);
    }

    //显示窗口和更新窗口
   ShowWindow(hWnd,SW_SHOW);
   UpdateWindow(hWnd);

   //消息队列
   while( (r = GetMessage(&msg,NULL,0,0)) != 0 )
   {
       if( r < 0 )
       {
           sprintf(szbuf,"GetMessage error:%d",GetLastError());
           MessageBox(NULL,szbuf,"error",MB_OK);
           exit(0);
      }
      else
      {
          TranslateMessage(&msg);
          DispatchMessage(&msg);
      }
  }
   return 0 ;
}
LRESULT CALLBACK WinProc(
              HWND hwnd, // handle to window
              UINT uMsg, // message identifier
               WPARAM wParam, // first message parameter
              LPARAM lParam // second message parameter
)
{
   switch( uMsg) {
           case WM_CLOSE:      //点击窗口关闭时的消息
           {
               DestroyWindow(hwnd);
               break;
           }
           case WM_DESTROY:  //destroywindow发送的消息
           {
                PostQuitMessage(0);
                break ;
            }
          default:
                      return ::DefWindowProc(hwnd,uMsg,wParam,lParam);
  }
   return 0 ;
}

心得:

       win32 API编程时,自己对api有个大略的底,然后一边查函数一边编码,这跟我以前编win32程序不一样,这次写的熟练多了。而且也没有出错。

抱歉!评论已关闭.