vc自定义控件PreSubclassWindow及OnCreate

原文地址 http://blog.csdn.net/wuzoujing/article/details/4943242

由于自定义控件Custom control需要进行类绑定,只有注册窗口类后才能显示窗口。因此一般定义自定义控件对象后,在构造函数中用AfxRegisterClass注册窗口类,而AfxRegisterClass并不激活WM_CREATE消息,也就是说不能响应到OnCreate函数。那怎么响应OnCreate呢?
替代办法:OnCreate的内容可以用PreSubclassWindow代替。

 

  1. COpenGLControl::COpenGLControl() 
  2.     RegisterWndClass(); 
  3. BOOL COpenGLControl::RegisterWndClass() 
  4.     WNDCLASS windowclass; 
  5.     HINSTANCE hInst = AfxGetInstanceHandle(); 
  6.     if (!(::GetClassInfo(hInst, MYWNDCLASS, &windowclass))) 
  7.     { 
  8.         windowclass.style = CS_DBLCLKS;// | CS_HREDRAW | CS_VREDRAW; 
  9.         windowclass.lpfnWndProc = ::DefWindowProc; 
  10.         windowclass.cbClsExtra = windowclass.cbWndExtra = 0; 
  11.         windowclass.hInstance = hInst; 
  12.         windowclass.hIcon = NULL; 
  13.         windowclass.hCursor = AfxGetApp()->LoadStandardCursor(IDC_ARROW); 
  14.         windowclass.hbrBackground = ::GetSysColorBrush(COLOR_WINDOW); 
  15.         windowclass.lpszMenuName = NULL; 
  16.         windowclass.lpszClassName = MYWNDCLASS; 
  17.         if (!AfxRegisterClass(&windowclass)) 
  18.         { 
  19.             AfxThrowResourceException(); 
  20.             return FALSE; 
  21.         } 
  22.     } 
  23.     return TRUE; 
  24. void COpenGLControl::PreSubclassWindow() 
  25.     CDialog::PreSubclassWindow(); 
  26.     dc = new CClientDC(this); 
  27.     CRect rc; 
  28.     GetClientRect(&rc); 
  29.     openGLDevice.create(dc->m_hDC); 
  30.     ShowWindow(SW_SHOW); 
  31.     SetForegroundWindow(); 
  32.     SetFocus();                                  
  33.     ReSizeGLScene(rc.Width(),rc.Height());                   
  34.     InitGL(); 

当然,如果不用自定义控件Custom control,而采用类似第三方控件一样,通过void COpenGLControl::Create(CRect rect, CWnd *parent)再调用CreateEx创建窗口,则会激活WM_CREATE消息。

  1. void COpenGLControl::Create(CRect rect, CWnd *parent) 
  2.     CString className = AfxRegisterWndClass( 
  3.         CS_HREDRAW | CS_VREDRAW | CS_OWNDC, 
  4.         NULL, 
  5.         (HBRUSH)GetStockObject(BLACK_BRUSH), 
  6.         NULL); 
  7.     CreateEx( 
  8.         0, 
  9.         className, 
  10.         "OpenGL"
  11.         WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, 
  12.         rect, 
  13.         parent, 
  14.         0); 
  15.  
  16. int COpenGLControl::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  17.     if (CWnd::OnCreate(lpCreateStruct) == -1) 
  18.         return -1; 
  19.     dc = new CClientDC(this); 
  20.     openGLDevice.create(dc->m_hDC); 
  21.     ShowWindow(SW_SHOW); 
  22.     SetForegroundWindow(); 
  23.     SetFocus();                                  
  24.     ReSizeGLScene(lpCreateStruct->cx, lpCreateStruct->cy);                 
  25.     InitGL(); 
  26.     return 0; 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值