www.pudn.com > RichDrawText_demo.zip > RichDrawExampleDlg.cpp
// RichDrawExampleDlg.cpp : implementation file
//
#include "stdafx.h"
#include "RichDrawExample.h"
#include "RichDrawExampleDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CRichDrawExampleDlg dialog
CRichDrawExampleDlg::CRichDrawExampleDlg(CWnd* pParent /*=NULL*/)
: CDialog(CRichDrawExampleDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CRichDrawExampleDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CRichDrawExampleDlg, CDialog)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_WM_SIZE()
END_MESSAGE_MAP()
// CRichDrawExampleDlg message handlers
BOOL CRichDrawExampleDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// Set up the example rich text
m_RichText.SetText(
L"The quick brown fox jumped over the lazy dog. "
L"The quick brown fox jumped over the lazy dog. "
L"The quick brown fox jumped over the lazy dog. "
L"The quick brown fox jumped over the lazy dog. "
L"The quick brown fox jumped over the lazy dog. "
L"The quick brown fox jumped over the lazy dog. "
L"The quick brown fox jumped over the lazy dog. ");
COLORREF colours[] = {
RGB(0xFF,0x00,0x00),
RGB(0x00,0xFF,0x00),
RGB(0x00,0x00,0xFF),
RGB(0xFF,0xFF,0x00),
RGB(0xFF,0x00,0xFF),
RGB(0x00,0xFF,0xFF),
RGB(0x00,0x00,0x00)
};
for (int i = 0; i < sizeof colours / sizeof colours[0]; i++)
{
CComPtr range;
m_RichText.Range(i*46,(i+1)*46,&range);
CComPtr font;
range->GetFont(&font);
font->SetForeColor(colours[i]);
font->SetSize((float)(9+i));
}
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CRichDrawExampleDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, reinterpret_cast(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);
// Work out a suitable space for the text
CRect rect;
GetClientRect(rect);
rect.DeflateRect(16,10);
// Work out how much vertical space the text needs
m_RichText.SizeText(dc,rect);
// Fill the text background
dc.FillSolidRect(rect,RGB(255,255,255));
// Draw the text
m_RichText.DrawText(dc,rect);
// Draw a gripper to show that the dialog can be resized
GetClientRect(rect);
rect.left = rect.right - ::GetSystemMetrics(SM_CXHSCROLL);
rect.top = rect.bottom - ::GetSystemMetrics(SM_CYVSCROLL);
dc.DrawFrameControl(rect,DFC_SCROLL,DFCS_SCROLLSIZEGRIP);
}
}
// The system calls this function to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CRichDrawExampleDlg::OnQueryDragIcon()
{
return static_cast(m_hIcon);
}
void CRichDrawExampleDlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
Invalidate();
}