6, 添加一个Windows消息WM_CREATE
LRESULT CMyControl::OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/,
LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
HDC hdc = GetDC();
RECT rc;
GetClientRect(&rc);
CreateContext(hdc, rc); //初始化
return 0;
}
7,添加一个Windows消息WM_DESTROY
LRESULT CMyControl::OnDestroy(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
wglMakeCurrent(NULL, NULL);
if (m_hRC)
{
wglDeleteContext(m_hRC);
m_hRC = NULL;
}
return 0;
}
8, 添加一个事件OnRender,点击ClassView中的IMyControlEvents添加方法OnRender参数int right,int left,int bottom,int top确定,然后编译你的idl文件,点击CMyControl实现连接点选中IMyControlEvents点击确定。
9, 在OnDraw添加代码:
HRESULT OnDraw(ATL_DRAWINFO& di)
{
HDC hdc = di.hdcDraw;
RECT& rc = *(RECT*)di.prcBounds;
wglMakeCurrent(hdc, m_hRC);
glClearColor(1.0f, 0.0f, 0.0f, 10.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//激发OnRender事件
FireOnRender(rc.right , rc.left, rc.bottom , rc.top)
//在vc7下不用加前缀Fire
//OnRender(rc.right , rc.left, rc.bottom , rc.top)
glFinish();
SwapBuffers(wglGetCurrentDC());
return S_OK;
}
10, 编译。
要使用本控件很简单注册拖到你的窗体上,然后在OnRender事件中写你要画的物体就可以了!
查看本文来源