lesson3_VC++对windows程序简单封装

#include<windows.h>
#include <stdio.h>

class CWnd{
public:
 BOOL CreateEx(
    DWORD dwExStyle,      // extended window style
    LPCTSTR lpClassName,  // registered class name
    LPCTSTR lpWindowName, // window name
    DWORD dwStyle,        // window style
    int x,                // horizontal position of window
    int y,                // vertical position of window
    int nWidth,           // window width
    int nHeight,          // window height
    HWND hWndParent,      // handle to parent or owner window
    HMENU hMenu,          // menu handle or child identifier
    HINSTANCE hInstance,  // handle to application instance
    LPVOID lpParam        // window-creation data
  );

 BOOL ShowWindow(
  //HWND hWnd,     // handle to window
  int nCmdShow   // show state
  );

 BOOL UpdateWindow();
public:
 HWND m_hWnd;


};


BOOL CWnd::CreateEx(DWORD dwExStyle,  LPCTSTR lpClassName,
     LPCTSTR lpWindowName,  DWORD dwStyle, 
     int x,  int y,  int nWidth,  int nHeight, 
     HWND hWndParent,  HMENU hMenu, 
     HINSTANCE hInstance,  LPVOID lpParam  )
{
 m_hWnd = ::CreateWindowEx(dwExStyle ,lpClassName,
  lpWindowName, dwStyle ,x, y,
 nWidth ,nHeight, hWndParent ,hMenu ,hInstance ,lpParam);
 
 if(m_hWnd != NULL){
  return TRUE;
 } else {
  return FALSE;
 }
}

BOOL CWnd::ShowWindow(int nCmdShow){
 return ::ShowWindow(m_hWnd,nCmdShow);
}

BOOL CWnd::UpdateWindow(){
 return ::UpdateWindow(m_hWnd);
}

LRESULT CALLBACK WinSunProc(
   HWND hwnd,      // handle to window
   UINT uMsg,      // message identifier
   WPARAM wParam,  // first message parameter
   LPARAM lParam   // second message parameter
 );

int WINAPI WinMain(
 HINSTANCE hInstance,
 HINSTANCE hPrevInstance,
 LPSTR lpCmdLine,
 int nCmdShow
       ){

WNDCLASS wndcls;                                          //P6
     wndcls.cbClsExtra=0;                                      //1注册窗口类时追加内存空间用于存储附加信息
     wndcls.cbWndExtra=0;                                      //2创建窗口类时追加内存空间用于存储附加信息
     wndcls.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH); //3画刷句柄窗口重绘时指定画刷擦除窗口背景&颜色
     wndcls.hCursor=LoadCursor(NULL,IDC_CROSS);                //4光标资源句柄
     wndcls.hIcon=LoadIcon(NULL,IDI_ERROR);                    //5图标
     wndcls.hInstance=hInstance;                               //6应用实例句柄,由WinMain函数传进来
     wndcls.lpfnWndProc=WinSunProc;                            //7指向窗口过程回调函数
     wndcls.lpszClassName="sunxin2006";                        //8指定菜单资源名字
     wndcls.lpszMenuName=NULL;                                 //9窗口名
     wndcls.style=CS_HREDRAW | CS_VREDRAW;                     //A窗口样式
     RegisterClass(&wndcls);                                   //
     
//...
RegisterClass(&wndcls);

CWnd wnd;
wnd.CreateEx(NULL,"sunxin2006","http://www.sunxin.org",WS_OVERLAPPEDWINDOW,
         0,0,600,400,NULL,NULL,hInstance,NULL);
wnd.ShowWindow(SW_SHOWNORMAL);
wnd.UpdateWindow();
//...
  MSG msg;                                                  //消息变量
     while(GetMessage(&msg,NULL,0,0))
     {
         TranslateMessage(&msg);                               //转换字符消息
         DispatchMessage(&msg);                                //将消息回传给操作系统
     }
     return msg.wParam;
}


LRESULT CALLBACK WinSunProc(
   HWND hwnd,      // handle to window
   UINT uMsg,      // message identifier
   WPARAM wParam,  // first message parameter
   LPARAM lParam   // second message parameter
 )
 {
     switch(uMsg)
     {
     case WM_CHAR:                                             //字符消息
         char szChar[20];
         sprintf(szChar,"您输入的字符代码为 %d",wParam);       //字符打印
         MessageBox(hwnd,szChar,"char",0);
         break;
     case WM_LBUTTONDOWN:                                      //左键
         MessageBox(hwnd,"mouse clicked","message",0);
         HDC hdc;
         hdc=GetDC(hwnd);
         TextOut(hdc,0,50,"程序员之家",strlen("程序员之家"));  //文本输出
         //ReleaseDC(hwnd,hdc);
         break;
     case WM_RBUTTONDOWN:                                      //右键
         MessageBox(hwnd,"mouse clicked","message",0);
         HDC hdc1;
         hdc1=GetDC(hwnd);
         TextOut(hdc1,5,150,"谁的心 落在水乡忘了收",strlen("谁的心 落在水乡忘了收"));  //文本输出
         //ReleaseDC(hwnd,hdc);
         break;
     case WM_PAINT:                                            //重绘窗口
         HDC hDC;
         PAINTSTRUCT ps;                                       //
         hDC=BeginPaint(hwnd,&ps);                             //开始重绘
         TextOut(hDC,0,0,"文本输出
         EndPaint(hwnd,&ps);                                   //重绘结束
         break;
     case WM_CLOSE:                                            //关闭窗口
         if(IDYES==MessageBox(hwnd,"是否真的结束?","message",MB_YESNO))//文本输出
         {
             DestroyWindow(hwnd);
         }
         break;
     case WM_DESTROY:                                          //销毁窗口
         PostQuitMessage(0);                                   //发送消息关闭窗口
         break;
     default:
         return DefWindowProc(hwnd,uMsg,wParam,lParam);        //
     }
     return 0;
 }

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值