VC自绘对话框
2010-07-01 20:42:04 来源:WEB开发网//创建图
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<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,&GradBitInfo,DIB_RGB_COLORS);
free(pGradBits);
return hBmp;
}
接下来在CtestDlg类的OnPaint()中添加代码:
void CTestDlg::OnPaint()
{
if (IsIconic())
{
//省略源码……….
}
else
{
this->DrawView(); //此句为自添加
CDialog::OnPaint();
}
}
好了,现在编译运行即可。
祝各位编程快乐!
本文配套源码
更多精彩
赞助商链接