关于size消息的问题

来源:百度知道 编辑:UC知道 时间:2024/07/05 03:35:56
我用MFC做了个画线的程序,可是想通过响应size消息保证在窗口大小变化时图形随之变化,大家看在响应函数中该怎么写呢
void CGjxView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_point=point;
CView::OnLButtonDown(nFlags, point);
}

void CGjxView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
HDC dc;
dc=::GetDC(m_hWnd);
MoveToEx(dc,m_point.x,m_point.y,NULL);
LineTo(dc,point.x,point.y);
::ReleaseDC(m_hWnd,dc);
CView::OnLButtonUp(nFlags, point);
}
void CGjxView::OnSize(UINT nType, int cx, int cy)
{

// TODO: Add your message handler code here

}

OnSize中主要是要获得变化后的客户区大小,然后根据这个大小重新调整绘图的参数,再绘图。

绘图参数必须是相对值才可以自动调整,你的这个程序是绝对值,是没法调整的。

例如:
要画一个始终距离边框为30的矩形
void CTestView::OnDraw(CDC* pDC)
{
CTestDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here

//画一个始终距离边长为30的矩形
CRect rect;
rect.left = m_rectClient.left+30;
rect.top = m_rectClient.top+30;
rect.right = m_rectClient.right-30;
rect.bottom = m_rectClient.bottom-30;

CBrush brush(RGB(0,0,255));//创建蓝色画刷
pDC->SelectObject(&brush);

pDC->Rectangle(&rect);//画矩形
}

void CTestView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);

// TODO: Add your message handler code here
GetClientRect(m_rectClient);
}

学习了