MFC中用OpenGL在Picture控制元件中顯示圖片如何獲取控制元件內的滑鼠輪轉動訊息
在Picture控制元件新建了一個類 可以回應滑鼠左右鍵 也可以回應中鍵和滑鼠移動訊息 就是不能回應滑鼠輪轉動訊息
#OpenGL.h
class COpenGL : public CWnd
{
DECLARE_DYNAMIC(COpenGL)
public:
COpenGL();
virtual ~COpenGL();
protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnPaint();
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
afx_msg void OnMButtonDown(UINT nFlags, CPoint point);
afx_msg void OnRButtonDown(UINT nFlags, CPoint point);
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg BOOL OnMouseWheel(UINT nFlags, short zDelta, CPoint pt);//已添加滑鼠輪回應
afx_msg void OnMButtonUp(UINT nFlags, CPoint point);
afx_msg void OnSize(UINT nType, int cx, int cy);
public:
int MySetPixelFormat(HDC hdc);
void RefreshDisplay1();
public:
HDC hdc;
HGLRC hglrc;
GLfloat step,s;
};
OpenGL.cpp
IMPLEMENT_DYNAMIC(COpenGL, CWnd)
COpenGL::COpenGL()
{
m_xPos = 0.0f;
m_yPos = 0.0f;
m_zPos = 0.0f;
m_xAngle = 0.0f;
m_yAngle = 0.0f;
m_zAngle = 0.0f;
m_Scale = 1.0f;
m_Capture=false;
m_MouseDown=false;
m_LButtonDown=false;
}
COpenGL::~COpenGL()
{
wglMakeCurrent(NULL,NULL);
wglDeleteContext(hglrc);//洗掉渲染描述表
::ReleaseDC(m_hWnd,hdc);//釋放設備描述表
}
BEGIN_MESSAGE_MAP(COpenGL, CWnd)
ON_WM_CREATE()
ON_WM_PAINT()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MBUTTONDOWN()
ON_WM_MBUTTONUP()
ON_WM_RBUTTONDOWN()
ON_WM_MOUSEMOVE()
ON_WM_MOUSEWHEEL() //已添加滑鼠輪回應
ON_WM_SIZE()
END_MESSAGE_MAP()
// COpenGL 訊息處理程式
int COpenGL::OnCreate(LPCREATESTRUCT lpCreateStruct)//創建表單
{
if (CWnd::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: 在此添加您專用的創建代碼
MySetPixelFormat(::GetDC(m_hWnd)); //創建繪圖描述表,并關聯渲染描述表
CPaintDC dc(this);
hdc=::GetDC(m_hWnd);
hglrc=wglCreateContext(hdc);//使繪圖描述表為當前呼叫執行緒的當前繪圖描述表
wglMakeCurrent(hdc,hglrc);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);//清除顏色快取
glClearDepth(10.0f);//清除深度快取
glEnable(GL_DEPTH_TEST);//使能深度
return 0;
}
void COpenGL::OnPaint()//繪制表單
{
CPaintDC dc(this); //沒有這條指令則無法使用定時器
// 繪畫設備背景關系
// TODO: 在此處添加訊息處理程式代碼
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //清除顏色快取和深度快取
glLoadIdentity();//加載身份
glTranslatef(m_xPos, m_yPos, m_zPos);//平移
glRotatef(m_xAngle, 1.0f, 0.0f, 0.0f);//按角度旋轉
glRotatef(m_yAngle, 0.0f, 1.0f, 0.0f);
glScalef(m_Scale, m_Scale, m_Scale);//縮放
DrawBox();
SwapBuffers(hdc);
// 不為繪圖訊息呼叫 COpenGL::OnPaint();
}
void COpenGL::OnSize(UINT nType, int cx, int cy)
{
CWnd::OnSize(nType, cx, cy);
// TODO: 在此處添加訊息處理程式代碼
if (0 >= cx || 0 >= cy)
{
return;
}
m_cx=(float)cx;m_cy=(float)cy;
glViewport(0, 0, cx, cy);//視口引數X,Y指定了視見區域的左下角在視窗中的位置,一般情況下為(0,0),Width和Height指定了視見區域的寬度和高度
glMatrixMode(GL_PROJECTION);//矩陣模式
glLoadIdentity();//加載
//gluPerspective(60.0, (GLfloat) cx/(GLfloat) cy, 1.0, 10.0);
//gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
if (cx < cy)
{
//glOrtho(-100.0, 100.0, -100.0*(GLfloat)cy / (GLfloat)cx, 100.0*(GLfloat)cy / (GLfloat)cx, -100.0, 100.0);//容器//左/右/下/上/近/遠
glOrtho(-(GLfloat)(cx/2), (GLfloat)(cx/2), -(GLfloat)(cy/2)*(GLfloat)cy / (GLfloat)cx, (GLfloat)(cy/2)*(GLfloat)cy / (GLfloat)cx, -(GLfloat)(cx/2), (GLfloat)(cx/2));
}
else
{
//glOrtho(-100.0*(GLfloat)cx / (GLfloat)cy, 100.0*(GLfloat)cx / (GLfloat)cy, -100.0, 100.0, -100.0, 100.0);
glOrtho(-(GLfloat)(cx/2)*(GLfloat)cx / (GLfloat)cy, (GLfloat)(cx/2)*(GLfloat)cx / (GLfloat)cy, -(GLfloat)(cy/2), (GLfloat)(cy/2), -(GLfloat)(cx/2), (GLfloat)(cx/2));
}
glMatrixMode(GL_MODELVIEW);//矩陣模式
glLoadIdentity();//加載
}
int COpenGL::MySetPixelFormat(HDC hdc)
{
PIXELFORMATDESCRIPTOR pfd={
sizeof(PIXELFORMATDESCRIPTOR), //pfd結構的大小
1, // 版本號
PFD_DRAW_TO_WINDOW | //支持在視窗繪圖
PFD_SUPPORT_OPENGL | //支持OPENGL
PFD_DOUBLEBUFFER , //雙緩沖
PFD_TYPE_RGBA, //RGBA顏色模式
24, //24位顏色深度
0,0,0,0,0,0, //忽略顏色位
0, //沒有非透明度快取
0, //忽略移位位
0, //無累加快取
0,0,0,0, //忽略累加位
32, //32位深度快取
0, //無模板快取
0, //無輔助快取
PFD_MAIN_PLANE, //主層
0, //保留
0,0,0 //忽略層,可見性和損毀掩模
};
int iPixelFormat;
if( (iPixelFormat = ChoosePixelFormat(hdc,&pfd))==0)
{
MessageBox("ChooseFixelForamt Failed 選擇固定孔失敗!",NULL,MB_OK);
return 0;
}
if(SetPixelFormat(hdc,iPixelFormat,&pfd)==false)
{
MessageBox("SetPixelFormat Failed 設定像素格式失敗!",NULL,MB_OK);
return 0;
}
return 1;
}
void COpenGL::OnLButtonDown(UINT nFlags, CPoint point)//單擊左鍵
{
// TODO: 在此添加訊息處理程式代碼和/或呼叫默認值
m_LButtonDown=true;
m_LButtonDownPoint=point;
SetCapture();
CWnd::OnLButtonDown(nFlags, point);
}
void COpenGL::OnLButtonUp(UINT nFlags, CPoint point)//放開左鍵
{
// TODO: 在此添加訊息處理程式代碼和/或呼叫默認值
m_LButtonDown=false;
ReleaseCapture();
CWnd::OnLButtonUp(nFlags, point);
}
void COpenGL::OnMButtonDown(UINT nFlags, CPoint point)//單擊中鍵
{
// TODO: 在此添加訊息處理程式代碼和/或呼叫默認值
m_MouseDown=true;
m_MouseDownPoint=point;
SetCapture();
CWnd::OnMButtonDown(nFlags, point);
}
void COpenGL::OnMButtonUp(UINT nFlags, CPoint point)//釋放中鍵
{
// TODO: 在此添加訊息處理程式代碼和/或呼叫默認值
m_MouseDown=false;
m_MouseDownPoint = CPoint(0, 0);
ReleaseCapture();
CWnd::OnMButtonUp(nFlags, point);
}
void COpenGL::OnRButtonDown(UINT nFlags, CPoint point)//單擊右鍵
{
// TODO: 在此添加訊息處理程式代碼和/或呼叫默認值
m_xPos = 0.0f;
m_yPos = 0.0f;
m_zPos = 0.0f;
m_xAngle = 0.0f;
m_yAngle = 0.0f;
m_zAngle = 0.0f;
m_Scale = 1.0f;
m_ax=0;m_ay=0;m_bx=0;m_by=0;
CWnd::OnRButtonDown(nFlags, point);
}
void COpenGL::OnMouseMove(UINT nFlags, CPoint point)//滑鼠移動
{
// TODO: 在此添加訊息處理程式代碼和/或呼叫默認值
if (GetCapture() == this)
{
//Increment the object rotation angles
if(m_MouseDown==true)
{
m_xAngle += (point.y - m_MouseDownPoint.y) / 3.6;
m_yAngle += (point.x - m_MouseDownPoint.x) / 3.6;
InvalidateRect(NULL, FALSE);
m_MouseDownPoint = point;
}
if(m_LButtonDown==true)
{
m_xPos += (point.x - m_LButtonDownPoint.x)*(m_cx/m_cy);
m_yPos += (m_LButtonDownPoint.y - point.y);
m_LButtonDownPoint=point;
InvalidateRect(NULL, FALSE);
m_LButtonDownPoint=point;
}
}
m_Capture=true;
m_Mouse=point;
CWnd::OnMouseMove(nFlags, point);
}
BOOL COpenGL::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)//滾輪訊息無法回應不知道什么原因
{
return CWnd::OnMouseWheel(nFlags, zDelta, pt);
}
uj5u.com熱心網友回復:
在Picture控制元件中 即 class CMyStatic : public CStatic // 不是 CWndBOOL CMyStatic::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
{
// TODO: Add your message handler code here and/or call default
afxDump << "OnMouseWheel\n";
return CStatic::OnMouseWheel(nFlags, zDelta, pt);
}
uj5u.com熱心網友回復:
滑鼠滾輪這個訊息,是發送給具有焦點的視窗訊息佇列中,這個訊息比較惡心,建議使用鉤子去處理這個訊息uj5u.com熱心網友回復:
是我的類用錯了嗎? 這是系統自動生成的 但是現在可以回應滑鼠左右鍵 也可以回應中鍵和滑鼠移動訊息 就是不能回應滑鼠輪轉動訊息uj5u.com熱心網友回復:
down up 事件中 加入 SetFocus 試試。uj5u.com熱心網友回復:
2樓的兄弟什么是鉤子 4樓的兄弟 我給控制元件加了SetFocus焦點但是沒有用uj5u.com熱心網友回復:
請 上 視窗 創建 代碼uj5u.com熱心網友回復:
// CPaintDC dc(this); //沒有這條指令則無法使用定時器其實是因為沒有這句 視窗 總是無效, 就 不停地 OnPaint ,沒法 回應 Timer
你可以 用 下句 代替:
ValidateRect(NULL);// 使視窗 有效 !
// CPaintDC dc(this); //沒有這條指令則無法使用定時器
uj5u.com熱心網友回復:
請上傳 DrawBox()代碼 !uj5u.com熱心網友回復:
至于 OnMouseWheel 如果 父視窗 只有 COpenGL 那么 不用 SetFocus ,否則 要 SetFocus,讓 焦點 從 別的控制元件,轉到 本視窗
uj5u.com熱心網友回復:
void COpenGL::OnPaint()//繪制表單{
CPaintDC dc(this); //沒有這條指令則無法使用定時器
//ValidateRect(NULL);//可以替代上一條指令
// 繪畫設備背景關系
// TODO: 在此處添加訊息處理程式代碼
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //清除顏色快取和深度快取
glLoadIdentity();//加載身份
glTranslatef(m_xPos, m_yPos, m_zPos);//平移
glRotatef(m_xAngle, 1.0f, 0.0f, 0.0f);//按角度旋轉
glRotatef(m_yAngle, 0.0f, 1.0f, 0.0f);
glScalef(m_Scale, m_Scale, m_Scale);//縮放
DrawBox();
SwapBuffers(hdc);
// 不為繪圖訊息呼叫 COpenGL::OnPaint();
}
SwapBuffers(hdc); 這條陳述句是什么作用沒有它為什么不能顯示圖形
還不能在void COpenGL::OnPaint()函式外面執行
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/50090.html
標籤:圖形處理/算法
上一篇:急求函式代碼!!!
