- 声望
- 0 点
- 西工币
- 538 枚
- 贡献值
- 0 点
- 好评度
- 0 点
- 最后登录
- 2008-7-7
- 注册时间
- 2008-5-29
- 帖子
- 53
- 精华
- 0
- 积分
- 53
- 阅读权限
- 30
- UID
- 15738
 
该用户从未签到 - 西工币
- 538 枚
- 好评度
- 0 点
- 声望
- 0 点
- 注册时间
- 2008-5-29
- 帖子
- 53
- 积分
- 53
- UID
- 15738
|
给大家写了一个C++的代码,供参考,VC++2005编译:
//ShowString.h
#pragma once
class ShowString
{
private:
CStringList m_strlist;
int m_nCount,m_nHeight,m_nCountLine;
const static int m_csnDec;
const static int m_csnMaxFont;
const static int m_csnMinFont;
POSITION m_Pos;
bool m_bIsFull;
public:
ShowString(void);
ShowString(CString strshow[],int nCount);
void Show(CDC * pdc,CRect rcShow);
private:
void ShowText(CDC *pdc,int nleft,int nBottom,CString str,int nHeight,COLORREF col);
int GetFontHeight(int nIndex);
int GetLengthMoveRight(CRect rcShow);
int GetLeft(int nIndex,CRect rcShow);
int GetBottom(int nIndex);
CString GetString();
public:
~ShowString(void);
};
//ShowString.cpp
#include "StdAfx.h"
#include "ShowString.h"
const int ShowString::m_csnDec=2;
const int ShowString::m_csnMaxFont=40;
const int ShowString::m_csnMinFont=2;
ShowString::ShowString(void)
{
m_strlist.RemoveAll();
m_nCount=0;
m_nHeight=0;
m_nCountLine=0;
m_Pos=NULL;
m_bIsFull=false;
}
ShowString::ShowString(CString strshow[],int nCount)
{
m_strlist.RemoveAll();
for(int i=0;i<nCount;i++)
{
m_strlist.AddTail(strshow);
}
m_nCount=nCount;
m_nHeight=0;
m_nCountLine=0;
m_Pos=NULL;
m_bIsFull=false;
}
ShowString::~ShowString(void)
{
}
void ShowString::Show(CDC * pdc,CRect rcShow)
{
int nBottom=0;
if(m_nCount<=0)
{
return;
}
if(!m_bIsFull)
{
m_nCountLine++;
}
CDC dc/*MemDC*/;
dc.CreateCompatibleDC(pdc);
CBitmap bmp;
bmp.CreateCompatibleBitmap(pdc, rcShow.Width(), rcShow.Height());
CBitmap *bmpOld=dc.SelectObject(&bmp);
for(int i=m_nCountLine;i>0;i--)
{
nBottom=GetBottom(i);
if(nBottom>=rcShow.Height())
{
m_bIsFull=true;
}
ShowText(&dc,GetLeft(i,rcShow),rcShow.Height()-nBottom,this->GetString(),this->GetFontHeight(i),RGB(255,255,0));
}
BOOL iff=pdc->BitBlt(0,0,rcShow.Width(),rcShow.Height(),&dc,0,0, SRCCOPY);
//恢复老配置
dc.SelectObject(bmpOld);
dc.DeleteDC();
}
CString ShowString::GetString()
{
CString str=_T("");
if(m_Pos==NULL)
{
m_Pos=m_strlist.GetHeadPosition();
}
if(m_Pos!=NULL)
{
str=m_strlist.GetAt(m_Pos);
m_strlist.GetNext(m_Pos);
}
return str;
}
//显示字符串
//str【in】要显示的字符串
void ShowString::ShowText(CDC *pdc,int nleft,int nBottom,CString str,int nHeight,COLORREF col)
{
if((nHeight<=0)||(str==_T("")))
{
return;
}
COLORREF oldclor=pdc->SetTextColor(col);
//创建字体
CFont font;
VERIFY(font.CreateFont(
nHeight, // nHeight
0, // nWidth
0, // nEscapement
0, // nOrientation
FW_LIGHT, // nWeight
FALSE, // bItalic
FALSE, // bUnderline
0, // cStrikeOut
ANSI_CHARSET, // nCharSet
OUT_DEFAULT_PRECIS, // nOutPrecision
CLIP_DEFAULT_PRECIS, // nClipPrecision
DEFAULT_QUALITY, // nQuality
/*FF_DONTCARE|*//*DEFAULT_PITCH |*/ FF_SWISS, // nPitchAndFamily
_T("Arial"))); // lpszFacename
//显示字符串TRANSPARENT
pdc->SetBkMode(0);
//创建白色画笔
CPen whitePen,*Oldpen;
BOOL b=whitePen.CreatePen(PS_SOLID,5,RGB(255,255,255));
//将字符串居中显示
//int n=str.GetLength();
CFont * pfontOld=pdc->SelectObject(&font);
//CString strTemp=_T("中");
CSize sz=pdc->GetTextExtent(str);
int nTop=nBottom+sz.cy;
Oldpen=pdc->SelectObject(&whitePen);
BOOL bc=pdc->ExtTextOut(nleft,nTop,ETO_CLIPPED,CRect(nleft,nTop,nleft+sz.cx,nTop+sz.cy),str,NULL);
pdc->SelectObject(pfontOld);
pdc->SelectObject(Oldpen);
pdc->SetTextColor(oldclor);
Oldpen=NULL;
font.DeleteObject();
}
int ShowString::GetFontHeight(int nIndex)
{
int nHeight=m_csnMaxFont-nIndex*m_csnDec;
if(nHeight<m_csnMinFont)
{
nHeight=0;
}
return (nHeight);
}
int ShowString::GetLengthMoveRight(CRect rcShow)
{
int nlineMax=(m_csnMaxFont-m_csnMinFont)/m_csnDec;
if(nlineMax==0)
{
return 0;
}
return (rcShow.Width()/nlineMax);
}
int ShowString::GetLeft(int nIndex,CRect rcShow)
{
return (GetLengthMoveRight(rcShow)*nIndex);
}
int ShowString::GetBottom(int nIndex)
{
int nFontheight=GetFontHeight(nIndex);
return ((m_csnMaxFont+nFontheight)*nIndex/2+nIndex*2-2);
}
//ShowDlg.h
// ShowDlg.h : header file
//
#pragma once
#include "ShowString.h"
// CShowDlg dialog
class CShowDlg : public CDialog
{
// Construction
public:
CShowDlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
enum { IDD = IDD_SHOW_DIALOG };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
HICON m_hIcon;
// Generated message map functions
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
private:
ShowString * m_pshow;
UINT_PTR m_timer;
private:
afx_msg void OnTimer(UINT_PTR nIDEvent);
afx_msg void OnClose();
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
};
//ShowDlg.cpp
// ShowDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Show.h"
#include "ShowDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()
// CShowDlg dialog
CShowDlg::CShowDlg(CWnd* pParent /*=NULL*/)
: CDialog(CShowDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_timer=2;
CString str[]={_T("西工大"),_T("体坛风云"),_T("编程兴趣小组"),_T("诚邀有共同兴趣同学加入!!")};
m_pshow=new ShowString(str,4);
}
void CShowDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CShowDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_WM_TIMER()
ON_WM_CLOSE()
ON_WM_ERASEBKGND()
END_MESSAGE_MAP()
// CShowDlg message handlers
BOOL CShowDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 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
// TODO: Add extra initialization here
this->SetTimer(m_timer,100,NULL);
return TRUE; // return TRUE unless you set the focus to a control
}
void CShowDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// 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 CShowDlg::OnPaint()
{
CPaintDC dc(this); // device context for painting
if (IsIconic())
{
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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
{
CRect rc;
this->GetClientRect(rc);
m_pshow->Show(&dc,rc);
CDialog::OnPaint();
}
}
// The system calls this function to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CShowDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CShowDlg::OnTimer(UINT_PTR nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if(m_timer==nIDEvent)
{
this->Invalidate();
}
CDialog::OnTimer(nIDEvent);
}
void CShowDlg::OnClose()
{
// TODO: Add your message handler code here and/or call default
delete m_pshow;
m_pshow=NULL;
CDialog::OnClose();
}
BOOL CShowDlg::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
return FALSE;
//return CDialog::OnEraseBkgnd(pDC);
} |
|