建立MFC窗口
建立MFC窗口
2011年03月15日
【转】http://hi.baidu.com/lmhopen/blog/item/0282dccbdbbb f21bbf09e62f.html
三种建立OpenGL窗口的方法,
一种是win32 sdk加OpenGL函数,这种方法写起来极其麻烦,很是繁琐,
第二种把建立OpenGL环境所用到的OpenGL函数写成一个小类,然后用win32 sdk加OpenGL类的方式建立OpenGL窗口,这种方法比前一个方法简洁清晰了点,不过还是感觉繁琐.
第三种方法用MFC编程建立OpenGL窗口,这种方法我最推崇.因为首先用MFC建立windows窗口方便简洁.其次可以考虑把要用到的OpenGL函数编成一个类,这样用起OpenGL来更是简洁方便了.
我现在手头有这三种代码,但我以后主要用MFC来写OpenGL程序,下边把如何用MFC建立一个最基本的OpenGL窗口贴出来.
首先是头文件:
// smotri.h
// smotri - Simple MFC, C++, OpenGL Tutorial Program
// by: Joel Parris
// date: 10/9/2000
#include // MFC Windows include file
#include // OpenGL include file
#include // OpenGL Utilities include file
#include
class CSmotri: public CWinApp
{
public:
virtual BOOL InitInstance();
};
class COpenGLWindow: public CFrameWnd
{
public:
COpenGLWindow(); // COpenGLWindow Class Constructor
~COpenGLWindow(); // COpenGLWindow Class Constructor
HDC m_hgldc; // GDI Device Context
HGLRC m_hglRC; // Rendering Context
BOOL SetPixelformat(HDC hdc);
void GetGLInfo();
int DrawGLScene(void);
int InitGL(void);
void ReSizeGLScene(int width, int height);
//virtual BOOL PreCreateWindow( CREATESTRUCT& cs );
protected:
afx_msg void OnPaint();
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnSize(UINT nType, int cx, int cy );
afx_msg BOOL OnEraseBkgnd( CDC* pDC );
afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
DECLARE_MESSAGE_MAP()
};
然后是CPP文件:
// smotri - Simple MFC, C++, OpenGL Tutorial Program
// by: Joel Parris
// date: 1/21/2001
#include "smotri.h"
//#include "timer.h"
CSmotri Smotri; // Instantiate the Smotri application
////////////////////////////////////////////////// //////////////////
// CMyApp Member Function
BOOL CSmotri::InitInstance() // Initialize Smotri
{
m_pMainWnd = new COpenGLWindow;
// m_nCmdShow = SW_SHOWMAXIMIZED; // Comment to UnMaximize the Window
m_pMainWnd -> ShowWindow(m_nCmdShow);
m_pMainWnd -> UpdateWindow();
return true;
}
////////////////////////////////////////////////// //////////////////
// COpenGLWindow message map and Member functions
BEGIN_MESSAGE_MAP(COpenGLWindow, CFrameWnd)
ON_WM_PAINT()
ON_WM_CREATE()
ON_WM_SIZE()
ON_WM_ERASEBKGND()
ON_WM_KEYDOWN()
END_MESSAGE_MAP()
COpenGLWindow::COpenGLWindow()
{
Create( NULL, _T("Smotri - a Simple MFC OpenGL Application"),
WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS | CS_OWNDC);
//ModifyStyle(WS_CAPTION, 0, SWP_FRAMECHANGED);
}
COpenGLWindow::~COpenGLWindow()
{
wglMakeCurrent(NULL,NULL);
wglDeleteContext(m_hglRC);
}
/*BOOL COpenGLWindow::PreCreateWindow( CREATESTRUCT& cs )
{
if(!CFrameWnd::PreCreateWindow(cs))
return FALSE;
// Set the initial size of the window
cs.cx = 550;
cs.cy = 400;
return TRUE;
}*/
BOOL COpenGLWindow::OnEraseBkgnd( CDC* pDC )
{
/* This overrides the MFC Message Loop's OnEraseBkgnd(), which
keeps the OpenGL window from flashing. I shouldn't tell you
this but contrary to popular opinion it doesn't matter what this
override returns TRUE or FALSE. If you don't believe me, try it
for yourself.
*/
return TRUE;
}
void COpenGLWindow::OnSize(UINT nType, int cx, int cy )
{
ReSizeGLScene( cx, cy );
}
void COpenGLWindow::OnPaint()
{
CPaintDC dc(this); // Needed
//QueryPerformanceFrequency(&timerFrequency); // Initialize timer
//QueryPerformanceCounter(&startCount); // Start timer
// process block
int imax = 100;
for (int i = 1; i 属性->链接器->输入->附加依赖项 中导入相关的lib)
基本步骤就是:
1创建一个windows窗口.
2在OnCreate函数中创建和设置OpenGL环境.
3在OnSize函数中设置OpenGL窗口跟着windows窗口自动调整大小
4在OnPaint函数中使用OpenGL的作图命令,用swappbuffers把图形显示出来
5设置空的onerasebkgnd函数返回TRUE,目的是防止窗口闪烁.
6在窗口类的析构函数中释放环境描述表DC和着色描述表RC,删除RC
基本上用其它方法编OpenGL程序也得是这样的步骤,
猜你喜欢
转载自wnq498yx.iteye.com/blog/1361645
今日推荐
周排行