mfc 在对话框中显示直角坐标系
散点图已经画好,需添加坐标。voidCMfc00Dlg::OnPaint(){if(IsIconic()){CPaintDCdc(this);//devicecontex...
散点图已经画好,需添加坐标。
void CMfc00Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
//绘制矩形框
CDC *pDC=GetDC();
CRect rect;
GetClientRect(&rect);
pDC->Rectangle(200,10,rect.right-10,rect.bottom-10);
CDialog::OnPaint();
}
} 展开
void CMfc00Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
//绘制矩形框
CDC *pDC=GetDC();
CRect rect;
GetClientRect(&rect);
pDC->Rectangle(200,10,rect.right-10,rect.bottom-10);
CDialog::OnPaint();
}
} 展开
2个回答
展开全部
1. 添加static静态文本框到对话框上, 并修改其ID为IDC_MYSTATIC
2. 在对话框的申明文件即CXXXDlg.h文件中定义变量:
CWnd *pWnd; //获取控件窗口类指针
CDC *pDC; //获取控件的CDC指针
3. 在其cpp实现文件中的OnInitDialog()函数中获取:
pWnd =
GetDlgItem(IDC_STATIC);
pDC = pWnd->GetDC();
4. 在OnPaint()中的代码如下:
void CXXXDlg::OnPaint()
{
CPaintDC dc(this); // device context for painting
CString str;
int i, j;
int x, y;
//强制更新绘图, 不可少, 否则绘图会出错
//使static控件区域无效
pWnd->Invalidate();
//更新窗口, 此时才真正向系统发送重绘消息, 没有这句你可以试下, 绝对出问题
pWnd->UpdateWindow();
pDC->Rectangle(0, 0, 380, 390); //画一个矩形框
CPen *pPenRed = new CPen(); //创建画笔对象
pPenRed ->CreatePen(PS_SOLID, 1, RGB(255, 0, 0)); //红色画笔
CPen *pPen = NULL;
//选中当前红色画笔,并保存以前的画笔
CGdiObject *pOldPen = pDC ->SelectObject(pPenRed);
pDC ->MoveTo(20, 20); //绘制坐标轴
pDC ->LineTo(20, 360); //竖起轴
pDC ->LineTo(360, 360); //水平轴
//写X轴刻度值
for(i = 0; i <= 10; i ++)
{
str.Format("%d", i);
pDC ->TextOut(17 + 30 * i, 365, str);
//绘制X轴刻度
pDC ->MoveTo(i * 30 + 20, 360);
pDC ->LineTo(i * 30 + 20, 355);
}
//写Y轴刻度值
for(i = 1; i <= 10; i ++)
{
str.Format("%d", i);
pDC ->TextOut(2, 360 - 30 * i - 5, str);
//绘制Y轴刻度
pDC ->MoveTo(25, 360 - 30 * i);
pDC ->LineTo(20, 360 - 30 * i);
}
//绘制X箭头
pDC ->MoveTo(350, 357);
pDC ->LineTo(360, 360);
pDC ->LineTo(350, 363);
//绘制Y箭头
pDC ->MoveTo(17, 30);
pDC ->LineTo(20, 20); //绘制左边箭头
pDC ->LineTo(23, 30); //绘制右边箭头
//恢复以前的画笔
pDC ->SelectObject(pOldPen);
delete pPenRed;
if(pPen != NULL)
delete pPen;
}
2. 在对话框的申明文件即CXXXDlg.h文件中定义变量:
CWnd *pWnd; //获取控件窗口类指针
CDC *pDC; //获取控件的CDC指针
3. 在其cpp实现文件中的OnInitDialog()函数中获取:
pWnd =
GetDlgItem(IDC_STATIC);
pDC = pWnd->GetDC();
4. 在OnPaint()中的代码如下:
void CXXXDlg::OnPaint()
{
CPaintDC dc(this); // device context for painting
CString str;
int i, j;
int x, y;
//强制更新绘图, 不可少, 否则绘图会出错
//使static控件区域无效
pWnd->Invalidate();
//更新窗口, 此时才真正向系统发送重绘消息, 没有这句你可以试下, 绝对出问题
pWnd->UpdateWindow();
pDC->Rectangle(0, 0, 380, 390); //画一个矩形框
CPen *pPenRed = new CPen(); //创建画笔对象
pPenRed ->CreatePen(PS_SOLID, 1, RGB(255, 0, 0)); //红色画笔
CPen *pPen = NULL;
//选中当前红色画笔,并保存以前的画笔
CGdiObject *pOldPen = pDC ->SelectObject(pPenRed);
pDC ->MoveTo(20, 20); //绘制坐标轴
pDC ->LineTo(20, 360); //竖起轴
pDC ->LineTo(360, 360); //水平轴
//写X轴刻度值
for(i = 0; i <= 10; i ++)
{
str.Format("%d", i);
pDC ->TextOut(17 + 30 * i, 365, str);
//绘制X轴刻度
pDC ->MoveTo(i * 30 + 20, 360);
pDC ->LineTo(i * 30 + 20, 355);
}
//写Y轴刻度值
for(i = 1; i <= 10; i ++)
{
str.Format("%d", i);
pDC ->TextOut(2, 360 - 30 * i - 5, str);
//绘制Y轴刻度
pDC ->MoveTo(25, 360 - 30 * i);
pDC ->LineTo(20, 360 - 30 * i);
}
//绘制X箭头
pDC ->MoveTo(350, 357);
pDC ->LineTo(360, 360);
pDC ->LineTo(350, 363);
//绘制Y箭头
pDC ->MoveTo(17, 30);
pDC ->LineTo(20, 20); //绘制左边箭头
pDC ->LineTo(23, 30); //绘制右边箭头
//恢复以前的画笔
pDC ->SelectObject(pOldPen);
delete pPenRed;
if(pPen != NULL)
delete pPen;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询