15,979
社区成员
发帖
与我相关
我的任务
分享
void CTGraphDialog::OnPaint() //向导默认生产的对话框绘制函数
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// 使图标在工作区矩形中居中
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;
// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
//CDialog::OnPaint();
CPaintDC dc(this);
Test_DrawText(dc); //调用DrawText测试函数
//PaintDialog(dc);
}
}
void CTGraphDialog::Test_DrawText(CDC& dc) //测试
{
int dt=0;
int idc=dc.SaveDC();
dc.SetMapMode(MM_LOMETRIC); //将这行屏蔽了DrawText()就能输出文字了
CString s("abcdefg");
dc.TextOut(0,0,s,s.GetLength()); //TextOut能正常工作
CRect r(0,0,1000,1000);
for(int y=-10000;y<10000;y+=100)
{
for(int x=-10000;x<10000;x+=100)
{
dc.SetPixelV(x,y,RGB(255,0,0)); //在屏幕上绘制一些点
r.MoveToXY(x,y); //循环改变文字输出位置,让文字铺满窗口客户区
dt=dc.DrawText(s,s.GetLength(),&r,DT_TOP|DT_LEFT); //为什么循环完成后什么文字都没看到,只看到满屏SetPixelV()输出的点
}
}
dc.RestoreDC(idc);
}
为什么什么文字都看不到呢,即使给DC选入一个很大的字体也什么都没有
void CLoMetricDlg::DrawText(CDC& dc)
{
int idc=dc.SaveDC();
//
dc.SetMapMode(MM_LOMETRIC);
//
CString s("A");
CSize sChar=dc.GetTextExtent(s);
// afxDump << sChar << "\n";// (42 x 71)
CRect rc;
GetClientRect(rc);
dc.DPtoLP(rc); // rc {top=0 bottom=-1587 left=0 right=2540}
dc.SetBkMode(TRANSPARENT);
int x,y;
int stepX=sChar.cx+40;// 42+40
int stepY=sChar.cy;// 71
for(y=rc.top;y > rc.bottom;y -= stepY)
{
for(x=rc.left;x < rc.right;x += stepX)
{
// afxDump << x << ";" << y << "\n";
CRect rc1(x,y,x+sChar.cx,y-sChar.cy);
// dc.TextOut(x,y,s,s.GetLength());
dc.DrawText(s,s.GetLength(),&rc1,DT_TOP|DT_LEFT);// DT_VCENTER
}
}
dc.RestoreDC(idc);
}
注意
CSize sChar=dc.GetTextExtent(s);
把 间隔 计算 出来了
void CLoMetricDlg::DrawText(CDC& dc)
{
int idc=dc.SaveDC();
//
dc.SetMapMode(MM_LOMETRIC);
//
CString s("A");
//
CRect rc;
GetClientRect(rc);
dc.DPtoLP(rc); // rc {top=0 bottom=-1587 left=0 right=2540}
dc.SetBkMode(TRANSPARENT);
int x,y;
for(y=rc.top;y > rc.bottom;y -= 100)
{
for(x=rc.left;x < rc.right;x += 100)
{
// afxDump << x << ";" << y << "\n";
CRect rc1(x,y,x+45,y-65);
// dc.TextOut(x,y,s,s.GetLength());
dc.DrawText(s,s.GetLength(),&rc1,DT_TOP|DT_LEFT);
}
}
dc.RestoreDC(idc);
}
void CLoMetricDlg::Test_DrawText(CDC& dc) //测试
{
int idc=dc.SaveDC();
dc.SetMapMode(MM_LOMETRIC); //将这行屏蔽了DrawText()就能输出文字了
CString s("A");
CRect rc;
GetClientRect(rc);
dc.DPtoLP(rc); // rc {top=0 bottom=-1587 left=0 right=2540}
int x,y;
for(y=0;y > -1600;y -= 100)
{
for(x=0;x< 2600;x+=100)
{
// afxDump << x << ";" << y << "\n";
CRect rc1(x,y,x+45,y-65);
// dc.TextOut(x,y,s,s.GetLength()); //TextOut能正常工作
dc.DrawText(s,s.GetLength(),&rc1,DT_TOP|DT_LEFT);//为什么循环完成后什么文字都没看到,只看到满屏SetPixelV()输出的点
}
}
dc.RestoreDC(idc);
}
注意
CRect rc;
GetClientRect(rc);
dc.DPtoLP(rc); // rc {top=0 bottom=-1587 left=0 right=2540}