绘制图形
CRect rectDlg;
GetClientRect(rectDlg);
int pointWidth = rectDlg.Width();
int pointHeight = rectDlg.Height();
RedrawWindow(CRect(0, 0, pointWidth, pointHeight));
int x = 200;
int y = 100;
CDC* pDC = GetDC();
pDC->Ellipse(x - 8, y - 8, x + 8, y + 8);
CString cStr;
cStr.Format(_T("%d"), "文本以图形形式显示");
pDC->TextOut(x - 4, y - 8, cStr);
pDC->MoveTo(x, y);
pDC->LineTo(x +10, y + 10);
ReleaseDC(pDC);
二叉树绘制实例
void CMFCBinaryTreeDlg::OnBnClickedButtonInsert()
{
BinaryTree tree(root);
CRect rectDlg;
GetClientRect(rectDlg);
int pointWidth = rectDlg.Width();
int pointHeight = rectDlg.Height();
RedrawWindow(CRect(0, 0, pointWidth, pointHeight));
CRect rectL;
GetDlgItem(IDC_PIC1)->GetWindowRect(&rectL);
ScreenToClient(rectL);
int height = rectL.bottom-rectL.top;
int width = rectL.right-rectL.left;
int depth = tree.GetHeight(tree.root);
CDC* pDC = GetDC();
paintTree(pDC, tree.root, 750, 250, 0, 0, depth);
ReleaseDC(pDC);
}
void CMFCBinaryTreeDlg::paintTree(CDC* pDC, AvlNode<int>*& node, int x, int y, int c, int d, int depth) {
const int width = 10;
const int height = 30;
int interal = pow(2, double(depth) - c) * width / 2;
int ix, iy;
if (node != NULL) {
pDC->Ellipse(x - 8, y - 8, x + 8, y + 8);
CString cStr;
cStr.Format(_T("%d"), node->data);
pDC->TextOut(x - 4, y - 8, cStr);
if (node->left != NULL)
{
pDC->MoveTo(x, y);
ix = x + interal;
iy = y + height;
pDC->LineTo(ix, iy);
paintTree(pDC, node->left, x - interal, y + height, c + 1, -1, depth);
}
if (node->right != NULL) {
pDC->MoveTo(x, y);
ix = x - interal;
iy = y + height;
pDC->LineTo(ix, iy);
paintTree(pDC, node->right, x + interal, y + height, c + 1, 1, depth);
}
}
}