分类: C/C++
2008-08-07 17:36:36
程序的实现也不复杂,接下来就跟着我一步一步做。
首先,创建一个基于对话框的应用程序。
接下来,在CTestDlg类中添加两个成员函数:
// In CTestDlg.h public: void DrawView(); HBITMAP CreateGradientBMP(HDC hDC,COLORREF cl1,COLORREF cl2,int nWidth,int nHeight,int nDir,int nNumColors);函数的具体内容如下:
// In CtestDlg.cpp void CTestDlg::DrawView() { CPaintDC dc(this); CRect rect; CString strText="TEST. HEllo WoRLD! 你好,VCKBASE!"; //文本内容 GetClientRect(rect); rect.right=27; //自绘区域宽度 COLORREF m_clrSideBarStart; //自绘区域起始处颜色 COLORREF m_clrSideBarEnd; //自绘区域结束处颜色 m_clrSideBarStart=RGB(205,205,205); //灰色 m_clrSideBarEnd=RGB(100,0,0); //褐色 //创建图及绘制区域 HBITMAP bmpBar = CreateGradientBMP( dc.m_hDC, m_clrSideBarStart, m_clrSideBarEnd, rect.Width(), rect.Height(), 0, 256); if (bmpBar) { CDC memDC; memDC.CreateCompatibleDC(&dc); HBITMAP hOldBmp = (HBITMAP)::SelectObject(memDC.m_hDC, bmpBar); dc.BitBlt(rect.left, rect.top, rect.Width(), rect.Height(), &memDC, 0, 0, SRCCOPY); ::SelectObject(memDC, hOldBmp); ::DeleteObject(bmpBar); } //绘制文本 CFont vertFont; //字体 vertFont.CreateFont(16, 0, 900, 900, FW_BOLD, 0, 0, 0, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, "Arial"); CFont *pOldFont = dc.SelectObject(&vertFont); COLORREF oldColor = dc.GetTextColor(); dc.SetTextColor(RGB(255, 255, 255)); //文本颜色 dc.SetBkMode(TRANSPARENT); dc.TextOut(rect.left 2, rect.bottom-4, strText); //文本起始位置 dc.SetTextColor(oldColor); dc.SelectObject(pOldFont); vertFont.DeleteObject(); }//创建图
HBITMAP CTestDlg::CreateGradientBMP(HDC hDC, COLORREF cl1, COLORREF cl2, int nWidth, int nHeight, int nDir, int nNumColors) { if(nNumColors > 256) nNumColors = 256; COLORREF PalVal[256]; memset(PalVal, 0, sizeof(COLORREF)*256); int nIndex; BYTE peRed=0,peGreen=0,peBlue=0; int r1=GetRValue(cl1); int r2=GetRValue(cl2); int g1=GetGValue(cl1); int g2=GetGValue(cl2); int b1=GetBValue(cl1); int b2=GetBValue(cl2); for (nIndex = 0; nIndex < nNumColors; nIndex ) { peRed = (BYTE) (r1 MulDiv((r2-r1),nIndex,nNumColors-1)); peGreen = (BYTE) (g1 MulDiv((g2-g1),nIndex,nNumColors-1)); peBlue = (BYTE) (b1 MulDiv((b2-b1),nIndex,nNumColors-1)); PalVal[nIndex]=(peRed << 16) | (peGreen << 8) | (peBlue); } int x,y,w,h; w=nWidth; h=nHeight; LPDWORD pGradBits; BITMAPINFO GradBitInfo; pGradBits=(DWORD*) malloc(w*h*sizeof(DWORD)); memset(&GradBitInfo,0,sizeof(BITMAPINFO)); GradBitInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER); GradBitInfo.bmiHeader.biWidth=w; GradBitInfo.bmiHeader.biHeight=h; GradBitInfo.bmiHeader.biPlanes=1; GradBitInfo.bmiHeader.biBitCount=32; GradBitInfo.bmiHeader.biCompression=BI_RGB; if(nDir==0) { for(y=0;y接下来在CtestDlg类的OnPaint()中添加代码: void CTestDlg::OnPaint() { if (IsIconic()) { //省略源码………. } else { this->DrawView(); //此句为自添加 CDialog::OnPaint(); } }
好了,现在编译运行即可。
祝各位编程快乐! 下载本文示例代码