MFC画图 :画数字

源代码: 

画图部分在一个线程中:
1,创建线程:
HANDLE m_hThread;
DWORD   m_nID; 
int   index;
m_hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadProc , (LPVOID)this , 0, &m_nID);


2,线程函数:
void ThreadProc(CDlgSplitDlg* pdlg)
{
// while(TRUE)
{
Sleep(100);

int nWidthBetween = 80;
pdlg->index = 12;
while(pdlg->index--)
{
for (int i=0; i<pdlg->index; i++)
{
CRect rc;
rc.left = 10 + nWidthBetween*i;
rc.top = 0 + 10;
rc.right = rc.left + 100;
rc.bottom = rc.top + 180;
CString txt;
txt.Format(_T("%X"), i);
pdlg->OnDrawMyImage(rc, txt);
Sleep(50);
}
Sleep(1000);
pdlg->OnDrawBkWnd();
}
}
}


3,
//画输入的字符
void CDlgSplitDlg::OnDrawMyImage(CRect rc, CString txt)
{
CDC* pDC;
pDC = GetDC();


CPen* pOldPen;
CPen newPen;
newPen.CreatePen(PS_DASH, 10, RGB(255,0,0));
pOldPen = pDC->SelectObject(&newPen);

//选择画刷
CBrush* pOldBrush;
CBrush newBrush;
CBrush newBrush1;
newBrush.CreateSolidBrush(RGB(110,120,30));
pOldBrush = pDC->SelectObject(&newBrush);
newBrush1.CreateSolidBrush(RGB(110,120,230));
pOldBrush = pDC->SelectObject(&newBrush1);

//画框框 
CRect rcImage1 = rc; //CRect(210,20,310,200)
pDC->FillRect(rcImage1, &newBrush);
rcImage1.InflateRect(-10,-10);
pDC->FillRect(rcImage1, &newBrush1);

//画字母
CFont* pOldFont;
CFont newFont;
newFont.CreatePointFont(1200, "宋体");
pOldFont = pDC->SelectObject(&newFont);
pDC->SetTextColor(RGB(0,0,255));
pDC->TextOut(rcImage1.left, rcImage1.top,txt);
DeleteObject(newPen);
DeleteObject(newBrush);
DeleteObject(newBrush1);
DeleteObject(newFont);
}


4,//画默认背景
void CDlgSplitDlg::OnDrawBkWnd()
{
CDC* pDC;
pDC = GetDC();

//获取背景颜色
COLORREF gbBKColor;
gbBKColor = pDC->GetBkColor();

//选择画刷
CBrush* pOldBrush;
CBrush newBrush;
newBrush.CreateSolidBrush(gbBKColor);
pOldBrush = pDC->SelectObject(&newBrush);

//画框框 
CRect rc;
GetClientRect(&rc);
pDC->FillRect(rc, &newBrush);
DeleteObject(newBrush);
}



你可能感兴趣的:(null,mfc)