在直接使用设备上下文输出文本时,我们可以调用函数SetTextAlign来进行排版。
例如我们可以在对话框OnPaint函数中来进行测试
- void CGkbc8Dlg::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
- {
- CPaintDC dc(this);
- CRect rect;
- GetClientRect(rect);
- dc.MoveTo(rect.Width() / 2, 0);
- dc.LineTo(rect.Width() / 2, rect.Height() / 2);
- dc.MoveTo(0, 3 * rect.Height() / 4);
- dc.LineTo(rect.Width(), 3 * rect.Height() / 4);
- //设置透明背景
- dc.SetBkMode(TRANSPARENT);
- //设置左对齐
- dc.SetTextAlign(TA_LEFT);
- dc.TextOut(rect.Width() / 2, 0, _T("TA_LEFT"));
- //设置中间对齐
- dc.SetTextAlign(TA_CENTER);
- dc.TextOut(rect.Width() / 2, 25, _T("TA_CENTER"));
- //设置右对齐
- dc.SetTextAlign(TA_RIGHT);
- dc.TextOut(rect.Width() / 2, 50, _T("TA_RIGHT"));
- //设置基线对齐
- dc.SetTextAlign(TA_CENTER | TA_BASELINE);
- dc.TextOut(rect.Width() / 4, 3 * rect.Height() / 4, _T("TA_BASELINE"));
- //设置上对齐
- dc.SetTextAlign(TA_CENTER | TA_TOP);
- dc.TextOut(rect.Width() / 2, 3 * rect.Height() / 4, _T("TA_TOP"));
- //设置下对齐
- dc.SetTextAlign(TA_CENTER | TA_BOTTOM);
- dc.TextOut(3 * rect.Width() / 4, 3 * rect.Height() / 4, _T("TA_BOTTOM"));
- CDialog::OnPaint();
- }
- }
复制代码 可以看到我们多次调用 了SetTextAlign来进行排版。
上位机VC MFC程序开发精典实例大全源码与视频讲解配套下载408例 经历1年的编程与录制点击进入查看
如果您认可,可联系功能定制! 如果您着急,充值会员可直接联系发您资料!
|