📄 coolbar.cpp
字号:
// CoolBar.cpp : implementation file
//
#include "stdafx.h"
//#include "main.h"
#include "CoolBar.h"
//#include <math.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CCoolBar
CCoolBar::CCoolBar()
{
m_bMenu = FALSE;
m_bTool = FALSE;
m_hBitmap = NULL;
}
CCoolBar::~CCoolBar()
{
// if(m_hBitmap) DeleteObject(m_hBitmap);
}
/*
BEGIN_MESSAGE_MAP(CCoolBar, CReBar)
//{{AFX_MSG_MAP(CCoolBar)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
*/
/////////////////////////////////////////////////////////////////////////////
// CCoolCtrl message handlers
//////////////////////////////////////////////////
//创建CReBar工具条
//pWnd 主窗口指针
//nMenuID 菜单ID
//nToolID 工具条ID
//clrFore 渐变初始值
//clrBack 渐变结束值
//shadeID 填充方案
//nID 工具条使用ID,不知什么表述
//
BOOL CCoolBar::CreateEx(CWnd *pWnd,
UINT nMenuID,
UINT nToolID,
COLORREF clrFore,
COLORREF clrBack,
UINT nHeight,
UINT nID
)
{
if(!pWnd) return FALSE;
CBitmap bitmap;
CBitmap *pBmp = NULL;
/////////用渐变色生成Bitmap背景
if(m_hBitmap) DeleteObject(m_hBitmap);
m_hBitmap = CreateBmp3(1,nHeight,clrFore,clrBack);
// m_hBitmap = CreateBmp2(clrFore,clrBack, 100, 100);
if(m_hBitmap)
{
bitmap.Attach(m_hBitmap);
pBmp = &bitmap;
}
/*
COLORREF cl = RGB(205,205,205);
COLORREF cm = RGB(205,205,205);
COLORREF cr = RGB(100,0,0);
CDC dcSrc;
dcSrc.Attach(GetWindowDC(NULL));
// bitmap.CreateCompatibleBitmap(&dcSrc,10,100);
bitmap.CreateBitmap(10,100,1,24, NULL);
CreateGradientBMP(&bitmap, 1, cl, cm, cr);
dcSrc.Detach();
pBmp = &bitmap;
*/
/////////用Bitmap创建bar
BOOL bReturn = CreateEx(pWnd, nMenuID,nToolID,pBmp,nID);
bitmap.Detach();
return bReturn;
}
//////////////////////////////////////////////////
//创建CReBar工具条
//pWnd 主窗口指针
//nMenuID 导入菜单的ID
//nToolID 导入工具条的ID
//pBitmap 背景图片
//nID 工具条使用ID,不知什么表述
BOOL CCoolBar::CreateEx(CWnd *pWnd,
UINT nMenuID,
UINT nToolID,
CBitmap *pBitmap,
UINT nID
)
{
if(!pWnd) return FALSE;
//删除原菜单
CMenu *pMenu = pWnd->GetMenu();
if(pMenu)
{
pMenu->DestroyMenu();
pWnd->SetMenu(NULL);
}
//生成工具条
if(nToolID > 0)
{
if (!m_wndToolBar.Create(pWnd, AFX_IDW_TOOLBAR) ||
!m_wndToolBar.LoadToolBar(nToolID, AILS_NEW))
{
TRACE0("Failed to create toolbar\n");
return FALSE; // fail to create
}
m_bTool = TRUE;
}
//生成菜单
if(nMenuID > 0)
{
if (!m_wndMenuBar.Create(pWnd) ||
!m_wndMenuBar.LoadMenuBar(nMenuID, AILS_NEW))
{
TRACE0("Failed to create menubar\n");
return FALSE; // fail to create
}
m_bMenu = TRUE;
}
//导入工具条图标
if(nToolID > 0)
m_wndMenuBar.LoadToolBar(nToolID);
//生成CReBar
if (!m_BaseBar.Create(pWnd, RBS_BANDBORDERS,
WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN | WS_CLIPSIBLINGS | CBRS_ALIGN_TOP)
|| !m_BaseBar.AddBar(&m_wndMenuBar, NULL, (CBitmap*)pBitmap, RBBS_BREAK)
|| !m_BaseBar.AddBar(&m_wndToolBar, NULL, (CBitmap*)pBitmap, RBBS_BREAK))
{
TRACE0("Failed to create rebar\n");
return FALSE; // fail to create
}
return TRUE;
}
////////////////////////////////////////////////////////
//处理主窗口菜单相关信息
//此函数必须加在主窗口中
//例:
//BOOL CMainFrame::PreTranslateMessage(MSG* pMsg)
//{
// if(!m_wndReBar.PreMenuMessage(pMsg)) /////////先让bar处理信息
// return FALSE;
// return CFrameWnd::PreTranslateMessage(pMsg);
//}
BOOL CCoolBar::PreMenuMessage(MSG* pMsg)
{
if (m_bMenu)
return m_wndMenuBar.PreMenuMessage(pMsg);
else
return TRUE;
}
/////////////////////////////////////////////
//用渐变色生成HBITMAP句柄
HBITMAP CCoolBar::CreateBmp(DWORD dwWidth,
DWORD dwHeight,
COLORREF clrFore,
COLORREF clrBack,
UINT shadeID
)
{
DWORD dwLen; // size of memory block
CDC dcSrc;
BITMAPINFOHEADER bi;
dwLen = WIDTHBYTES(24 * dwWidth) * dwHeight;
// initialize BITMAPINFOHEADER
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = dwWidth; // fill in width from parameter
bi.biHeight = dwHeight; // fill in height from parameter
bi.biPlanes = 1; // must be 1
bi.biBitCount = 24; // from parameter
bi.biCompression = BI_RGB;
bi.biSizeImage = dwLen;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
BYTE* pDest = NULL, *pData;
pData = (BYTE *)malloc(dwLen); // alloc memory block to store our bitmap
if(!pData) return NULL;
memset(pData, 0, dwLen);
/////////////////////////////////////
//填充方案
long sXSize,sYSize,bytes,j,i,k;//,h;
BYTE *iDst ,*posDst;
sXSize = dwWidth;
sYSize = dwHeight;
COLORREF szPalette[256];
COLORREF hicr=GetSysColor(COLOR_BTNHIGHLIGHT); //get the button base colors
COLORREF midcr=GetSysColor(COLOR_BTNFACE);
COLORREF locr=GetSysColor(COLOR_BTNSHADOW);
long r,g,b; //build the shaded palette
for(i=0;i<129;i++){
r=((128-i)*GetRValue(locr)+i*GetRValue(midcr))/128;
g=((128-i)*GetGValue(locr)+i*GetGValue(midcr))/128;
b=((128-i)*GetBValue(locr)+i*GetBValue(midcr))/128;
szPalette[i] = RGB(r,g,b);
}
for(i=1;i<129;i++){
r=((128-i)*GetRValue(midcr)+i*GetRValue(hicr))/128;
g=((128-i)*GetGValue(midcr)+i*GetGValue(hicr))/128;
b=((128-i)*GetBValue(midcr)+i*GetBValue(hicr))/128;
szPalette[i+127] = RGB(r,g,b);
//m_dNormal.SetPaletteIndex((BYTE)(i+127),(BYTE)r,(BYTE)g,(BYTE)b);
}
// m_dNormal.BlendPalette(color,coloring); //color the palette
/////////////////////////////////////////////////////////////////////
r = GetRValue(clrFore);
g = GetGValue(clrFore);
b = GetBValue(clrFore);
BYTE r1, g1, b1;
//if (perc>100) perc=100;
static int perc = 55;
for(i=0;i<256;i++)
{
r1 = GetRValue(szPalette[i]);
g1 = GetGValue(szPalette[i]);
b1 = GetBValue(szPalette[i]);
szPalette[i] =RGB((BYTE)((r1*(100-perc)+b*perc)/100),
(BYTE)((g1*(100-perc)+g*perc)/100),
(BYTE)((b1*(100-perc)+r*perc)/100));
}
/*
// iDst=m_dh.GetBits(); //build the horiz. dotted focus bitmap
iDst=pDest;
j=(long)dwWidth;
for(i=0;i<j;i++){
// iDst[i]=64+127*(i%2); //soft
iDst[i]=255*(i%2); //hard
}
iDst=m_dv.GetBits(); //build the vert. dotted focus bitmap
j=(long)dwHeight;
for(i=0;i<j;i++){
// *iDst=64+127*(i%2); //soft
*iDst=255*(i%2); //hard
iDst+=4;
}
*/
// bytes = m_dNormal.GetLineWidth();
// iDst = m_dNormal.GetBits();
static int granularity = 8;
bytes = dwWidth;
iDst = pData;
posDst =iDst;
//long a,x,y,d,xs,
long a, idxmax,idxmin;
COLORREF rgb1;
int iInd;
int grainx2=RAND_MAX/(max(1,2*granularity));
idxmax=255-granularity;
idxmin=granularity;
switch(shadeID){
//----------------------------------------------------
/* case 8: //SHS_METAL
m_dNormal.Clear();
// create the strokes
k=40; //stroke granularity
for(a=0;a<200;a++){
x=rand()/(RAND_MAX/sXSize); //stroke postion
y=rand()/(RAND_MAX/sYSize); //stroke position
xs=rand()/(RAND_MAX/min(sXSize,sYSize))/2; //stroke lenght
d=rand()/(RAND_MAX/k); //stroke color
for(i=0;i<xs;i++){
if (((x-i)>0)&&((y+i)<sYSize))
m_dNormal.SetPixelIndex(x-i,y+i,(BYTE)d);
if (((x+i)<sXSize)&&((y-i)>0))
m_dNormal.SetPixelIndex(sXSize-x+i,y-i,(BYTE)d);
}
}
//blend strokes with SHS_DIAGONAL
posDst =iDst;
a=(idxmax-idxmin-k)/2;
for(i = 0; i < sYSize; i++) {
for(j = 0; j < sXSize; j++) {
d=posDst[j]+((a*i)/sYSize+(a*(sXSize-j))/sXSize);
posDst[j]=(BYTE)d;
posDst[j]+=rand()/grainx2;
}
posDst+=bytes;
}
break;
//----------------------------------------------------
case 7: // SHS_HARDBUMP
//set horizontal bump
for(i = 0; i < sYSize; i++) {
k=(255*i/sYSize)-127;
k=(k*(k*k)/128)/128;
k=(k*(128-granularity))/128+128;
for(j = 0; j < sXSize; j++) {
posDst[j]=(BYTE)k;
posDst[j]+=rand()/grainx2-granularity;
}
posDst+=bytes;
}
//set vertical bump
d=min(16,sXSize/6); //max edge=16
a=sYSize*sYSize/4;
posDst =iDst;
for(i = 0; i < sYSize; i++) {
y=i-sYSize/2;
for(j = 0; j < sXSize; j++) {
x=j-sXSize/2;
xs=sXSize/2-d+(y*y*d)/a;
if (x>xs) posDst[j]=idxmin+(BYTE)(((sXSize-j)*128)/d);
if ((x+xs)<0) posDst[j]=idxmax-(BYTE)((j*128)/d);
posDst[j]+=rand()/grainx2-granularity;
}
posDst+=bytes;
}
break;
//----------------------------------------------------
case 6: //SHS_SOFTBUMP
for(i = 0; i < sYSize; i++) {
h=(255*i/sYSize)-127;
for(j = 0; j < sXSize; j++) {
k=(255*(sXSize-j)/sXSize)-127;
k=(h*(h*h)/128)/128+(k*(k*k)/128)/128;
k=k*(128-granularity)/128+128;
if (k<idxmin) k=idxmin;
if (k>idxmax) k=idxmax;
posDst[j]=(BYTE)k;
posDst[j]+=rand()/grainx2-granularity;
}
posDst+=bytes;
}
break;
//----------------------------------------------------
case 5: // SHS_VBUMP
for(j = 0; j < sXSize; j++) {
k=(255*(sXSize-j)/sXSize)-127;
k=(k*(k*k)/128)/128;
k=(k*(128-granularity))/128+128;
for(i = 0; i < sYSize; i++) {
posDst[j+i*bytes]=(BYTE)k;
posDst[j+i*bytes]+=rand()/grainx2-granularity;
}
}
break;
//----------------------------------------------------
case 4: //SHS_HBUMP
for(i = 0; i < sYSize; i++) {
k=(255*i/sYSize)-127;
k=(k*(k*k)/128)/128;
k=(k*(128-granularity))/128+128;
for(j = 0; j < sXSize; j++) {
posDst[j]=(BYTE)k;
posDst[j]+=rand()/grainx2-granularity;
}
posDst+=bytes;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -