自绘对话框
作者:Alon
下载示例源代码
闲来无事,写了一个简单的对话框界面,希望给需要的朋友有所帮助。
界面如下:
界面很单调,我只是想说明一下如何实现界面的自绘。如果有需要,也可以绘制更复杂漂亮的界面。
程序的实现也不复杂,接下来就跟着我一步一步做。
首先,创建一个基于对话框的应用程序。
接下来,在CTestDlg类中添加两个成员函数:
// In CTestDlg.hpublic:void DrawView();HBITMAP CreateGradientBMP(HDC hDC,COLORREF cl1,COLORREF cl2,int nWidth,int nHeight,int nDir,int nNumColors);函数的具体内容如下:
// In CtestDlg.cppvoid CTestDlg::DrawView(){CPaintDC dc(this); CRect rect;CString strText="TEST. HEllo WoRLD! 你好,VCKBASE!"; //文本内容GetClientRect(rect);rect.right=27; //自绘区域宽度COLORREFm_clrSideBarStart; //自绘区域起始处颜色COLORREFm_clrSideBarEnd; //自绘区域结束处颜色m_clrSideBarStart=RGB(205,205,205); //灰色m_clrSideBarEnd=RGB(100,0,0); //褐色//创建图及绘制区域HBITMAPbmpBar = 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);}//绘制文本CFontvertFont;//字体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;LPDWORDpGradBits;BITMAPINFOGradBitInfo;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<h;y++) {for(x=0;x<w;x++) {*(pGradBits+(y*w)+x)=PalVal[MulDiv(nNumColors,y,h)];}}}else if(nDir==1) {for(y=0;y<h;y++){int l,r;l=MulDiv((nNumColors/2),y,h);r=l+(nNumColors/2)-1;for(x=0;x<w;x++){*(pGradBits+(y*w)+x)=PalVal[l+MulDiv((r-l),x,w)];}}}else if(nDir==2){for(x=0;x<w;x++){for(y=0;y<h;y++){*(pGradBits+(y*w)+x)=PalVal[MulDiv(nNumColors,x,w)];}}}else if(nDir==3){for(y=0;y<h;y++){int l,r;r=MulDiv((nNumColors/2),y,h);l=r+(nNumColors/2)-1;for(x=0;x<w;x++){*(pGradBits+(y*w)+x)=PalVal[l+MulDiv((r-l),x,w)];}}}HBITMAP hBmp = CreateDIBitmap(hDC,&GradBitInfo.bmiHeader,CBM_INIT,pGradBits,&Grad