时间的痕迹
posts - 16, comments - 128, trackbacks - 0, articles - 261
C++博客
首页
新随笔
联系
管理
聚合
很好的button类 实现文件 3
HBITMAP CButtonST::CreateBitmapMask(HBITMAP hSourceBitmap, DWORD dwWidth, DWORD dwHeight, COLORREF crTransColor)
{
HBITMAP hMask
=
NULL;
HDC hdcSrc
=
NULL;
HDC hdcDest
=
NULL;
HBITMAP hbmSrcT
=
NULL;
HBITMAP hbmDestT
=
NULL;
COLORREF crSaveBk;
COLORREF crSaveDestText;
hMask
=
::CreateBitmap(dwWidth, dwHeight,
1
,
1
, NULL);
if
(hMask
==
NULL)
return
NULL;
hdcSrc
=
::CreateCompatibleDC(NULL);
hdcDest
=
::CreateCompatibleDC(NULL);
hbmSrcT
=
(HBITMAP)::SelectObject(hdcSrc, hSourceBitmap);
hbmDestT
=
(HBITMAP)::SelectObject(hdcDest, hMask);
crSaveBk
=
::SetBkColor(hdcSrc, crTransColor);
::BitBlt(hdcDest,
0
,
0
, dwWidth, dwHeight, hdcSrc,
0
,
0
, SRCCOPY);
crSaveDestText
=
::SetTextColor(hdcSrc, RGB(
255
,
255
,
255
));
::SetBkColor(hdcSrc,RGB(
0
,
0
,
0
));
::BitBlt(hdcSrc,
0
,
0
, dwWidth, dwHeight, hdcDest,
0
,
0
, SRCAND);
SetTextColor(hdcDest, crSaveDestText);
::SetBkColor(hdcSrc, crSaveBk);
::SelectObject(hdcSrc, hbmSrcT);
::SelectObject(hdcDest, hbmDestT);
::DeleteDC(hdcSrc);
::DeleteDC(hdcDest);
return
hMask;
}
//
End of CreateBitmapMask
//
//
Parameters:
//
[IN] bHasTitle
//
TRUE if the button has a text
//
[IN] rpItem
//
A pointer to a RECT structure indicating the allowed paint area
//
[IN/OUT]rpTitle
//
A pointer to a CRect object indicating the paint area reserved for the
//
text. This structure will be modified if necessary.
//
[IN] bIsPressed
//
TRUE if the button is currently pressed
//
[IN] dwWidth
//
Width of the image (icon or bitmap)
//
[IN] dwHeight
//
Height of the image (icon or bitmap)
//
[OUT] rpImage
//
A pointer to a CRect object that will receive the area available to the image
//
void
CButtonST::PrepareImageRect(BOOL bHasTitle, RECT
*
rpItem, CRect
*
rpTitle, BOOL bIsPressed, DWORD dwWidth, DWORD dwHeight, CRect
*
rpImage)
{
CRect rBtn;
rpImage
->
CopyRect(rpItem);
switch
(m_byAlign)
{
case
ST_ALIGN_HORIZ:
if
(bHasTitle
==
FALSE)
{
//
Center image horizontally
rpImage
->
left
+=
((rpImage
->
Width()
-
(
long
)dwWidth)
/
2
);
}
else
{
//
Image must be placed just inside the focus rect
rpImage
->
left
+=
m_ptImageOrg.x;
rpTitle
->
left
+=
dwWidth
+
m_ptImageOrg.x;
}
//
Center image vertically
rpImage
->
top
+=
((rpImage
->
Height()
-
(
long
)dwHeight)
/
2
);
break
;
case
ST_ALIGN_HORIZ_RIGHT:
GetClientRect(
&
rBtn);
if
(bHasTitle
==
FALSE)
{
//
Center image horizontally
rpImage
->
left
+=
((rpImage
->
Width()
-
(
long
)dwWidth)
/
2
);
}
else
{
//
Image must be placed just inside the focus rect
rpTitle
->
right
=
rpTitle
->
Width()
-
dwWidth
-
m_ptImageOrg.x;
rpTitle
->
left
=
m_ptImageOrg.x;
rpImage
->
left
=
rBtn.right
-
dwWidth
-
m_ptImageOrg.x;
//
Center image vertically
rpImage
->
top
+=
((rpImage
->
Height()
-
(
long
)dwHeight)
/
2
);
}
break
;
case
ST_ALIGN_VERT:
//
Center image horizontally
rpImage
->
left
+=
((rpImage
->
Width()
-
(
long
)dwWidth)
/
2
);
if
(bHasTitle
==
FALSE)
{
//
Center image vertically
rpImage
->
top
+=
((rpImage
->
Height()
-
(
long
)dwHeight)
/
2
);
}
else
{
rpImage
->
top
=
m_ptImageOrg.y;
rpTitle
->
top
+=
dwHeight;
}
break
;
case
ST_ALIGN_OVERLAP:
break
;
}
//
switch
//
If button is pressed then press image also
if
(bIsPressed
&&
m_bIsCheckBox
==
FALSE)
rpImage
->
OffsetRect(m_ptPressedOffset.x, m_ptPressedOffset.y);
}
//
End of PrepareImageRect
void
CButtonST::DrawTheIcon(CDC
*
pDC, BOOL bHasTitle, RECT
*
rpItem, CRect
*
rpCaption, BOOL bIsPressed, BOOL bIsDisabled)
{
BYTE byIndex
=
0
;
//
Select the icon to use
if
((m_bIsCheckBox
&&
bIsPressed)
||
(
!
m_bIsCheckBox
&&
(bIsPressed
||
m_bMouseOnButton)))
byIndex
=
0
;
else
byIndex
=
(m_csIcons[
1
].hIcon
==
NULL
?
0
:
1
);
CRect rImage;
PrepareImageRect(bHasTitle, rpItem, rpCaption, bIsPressed, m_csIcons[byIndex].dwWidth, m_csIcons[byIndex].dwHeight,
&
rImage);
//
Ole'!
pDC
->
DrawState( rImage.TopLeft(),
rImage.Size(),
m_csIcons[byIndex].hIcon,
(bIsDisabled
?
DSS_DISABLED : DSS_NORMAL),
(CBrush
*
)NULL);
}
//
End of DrawTheIcon
void
CButtonST::DrawTheBitmap(CDC
*
pDC, BOOL bHasTitle, RECT
*
rpItem, CRect
*
rpCaption, BOOL bIsPressed, BOOL bIsDisabled)
{
HDC hdcBmpMem
=
NULL;
HBITMAP hbmOldBmp
=
NULL;
HDC hdcMem
=
NULL;
HBITMAP hbmT
=
NULL;
BYTE byIndex
=
0
;
//
Select the bitmap to use
if
((m_bIsCheckBox
&&
bIsPressed)
||
(
!
m_bIsCheckBox
&&
(bIsPressed
||
m_bMouseOnButton)))
byIndex
=
0
;
else
byIndex
=
(m_csBitmaps[
1
].hBitmap
==
NULL
?
0
:
1
);
CRect rImage;
PrepareImageRect(bHasTitle, rpItem, rpCaption, bIsPressed, m_csBitmaps[byIndex].dwWidth, m_csBitmaps[byIndex].dwHeight,
&
rImage);
hdcBmpMem
=
::CreateCompatibleDC(pDC
->
m_hDC);
hbmOldBmp
=
(HBITMAP)::SelectObject(hdcBmpMem, m_csBitmaps[byIndex].hBitmap);
hdcMem
=
::CreateCompatibleDC(NULL);
hbmT
=
(HBITMAP)::SelectObject(hdcMem, m_csBitmaps[byIndex].hMask);
if
(bIsDisabled
&&
m_bShowDisabledBitmap)
{
HDC hDC
=
NULL;
HBITMAP hBitmap
=
NULL;
hDC
=
::CreateCompatibleDC(pDC
->
m_hDC);
hBitmap
=
::CreateCompatibleBitmap(pDC
->
m_hDC, m_csBitmaps[byIndex].dwWidth, m_csBitmaps[byIndex].dwHeight);
HBITMAP hOldBmp2
=
(HBITMAP)::SelectObject(hDC, hBitmap);
RECT rRect;
rRect.left
=
0
;
rRect.top
=
0
;
rRect.right
=
rImage.right
+
1
;
rRect.bottom
=
rImage.bottom
+
1
;
::FillRect(hDC,
&
rRect, (HBRUSH)RGB(
255
,
255
,
255
));
COLORREF crOldColor
=
::SetBkColor(hDC, RGB(
255
,
255
,
255
));
::BitBlt(hDC,
0
,
0
, m_csBitmaps[byIndex].dwWidth, m_csBitmaps[byIndex].dwHeight, hdcMem,
0
,
0
, SRCAND);
::BitBlt(hDC,
0
,
0
, m_csBitmaps[byIndex].dwWidth, m_csBitmaps[byIndex].dwHeight, hdcBmpMem,
0
,
0
, SRCPAINT);
::SetBkColor(hDC, crOldColor);
::SelectObject(hDC, hOldBmp2);
::DeleteDC(hDC);
pDC
->
DrawState( CPoint(rImage.left
/**/
/*
+1
*/
, rImage.top),
CSize(m_csBitmaps[byIndex].dwWidth, m_csBitmaps[byIndex].dwHeight),
hBitmap, DST_BITMAP
|
DSS_DISABLED);
::DeleteObject(hBitmap);
}
//
if
else
{
::BitBlt(pDC
->
m_hDC, rImage.left, rImage.top, m_csBitmaps[byIndex].dwWidth, m_csBitmaps[byIndex].dwHeight, hdcMem,
0
,
0
, SRCAND);
::BitBlt(pDC
->
m_hDC, rImage.left, rImage.top, m_csBitmaps[byIndex].dwWidth, m_csBitmaps[byIndex].dwHeight, hdcBmpMem,
0
,
0
, SRCPAINT);
}
//
else
::SelectObject(hdcMem, hbmT);
::DeleteDC(hdcMem);
::SelectObject(hdcBmpMem, hbmOldBmp);
::DeleteDC(hdcBmpMem);
}
//
End of DrawTheBitmap
void
CButtonST::DrawTheText(CDC
*
pDC, LPCTSTR lpszText, RECT
*
rpItem, CRect
*
rpCaption, BOOL bIsPressed, BOOL bIsDisabled)
{
//
Draw the button's title
//
If button is pressed then "press" title also
if
(m_bIsPressed
&&
m_bIsCheckBox
==
FALSE)
rpCaption
->
OffsetRect(m_ptPressedOffset.x, m_ptPressedOffset.y);
//
ONLY FOR DEBUG
//
CBrush brBtnShadow(RGB(255, 0, 0));
//
pDC->FrameRect(rCaption, &brBtnShadow);
//
Center text
CRect centerRect
=
rpCaption;
pDC
->
DrawText(lpszText,
-
1
, rpCaption, DT_WORDBREAK
|
DT_CENTER
|
DT_CALCRECT);
rpCaption
->
OffsetRect((centerRect.Width()
-
rpCaption
->
Width())
/
2
, (centerRect.Height()
-
rpCaption
->
Height())
/
2
);
/**/
/*
RFU
rpCaption->OffsetRect(0, (centerRect.Height() - rpCaption->Height())/2);
rpCaption->OffsetRect((centerRect.Width() - rpCaption->Width())-4, (centerRect.Height() - rpCaption->Height())/2);
*/
pDC
->
SetBkMode(TRANSPARENT);
/**/
/*
pDC->DrawState(rCaption->TopLeft(), rCaption->Size(), (LPCTSTR)sTitle, (bIsDisabled ? DSS_DISABLED : DSS_NORMAL),
TRUE, 0, (CBrush*)NULL);
*/
if
(m_bIsDisabled)
{
rpCaption
->
OffsetRect(
1
,
1
);
pDC
->
SetTextColor(::GetSysColor(COLOR_3DHILIGHT));
pDC
->
DrawText(lpszText,
-
1
, rpCaption, DT_WORDBREAK
|
DT_CENTER);
rpCaption
->
OffsetRect(
-
1
,
-
1
);
pDC
->
SetTextColor(::GetSysColor(COLOR_3DSHADOW));
pDC
->
DrawText(lpszText,
-
1
, rpCaption, DT_WORDBREAK
|
DT_CENTER);
}
//
if
else
{
if
(m_bMouseOnButton
||
m_bIsPressed)
{
pDC
->
SetTextColor(m_crColors[BTNST_COLOR_FG_IN]);
pDC
->
SetBkColor(m_crColors[BTNST_COLOR_BK_IN]);
}
//
if
else
{
if
(m_bIsFocused)
{
pDC
->
SetTextColor(m_crColors[BTNST_COLOR_FG_FOCUS]);
pDC
->
SetBkColor(m_crColors[BTNST_COLOR_BK_FOCUS]);
}
//
if
else
{
pDC
->
SetTextColor(m_crColors[BTNST_COLOR_FG_OUT]);
pDC
->
SetBkColor(m_crColors[BTNST_COLOR_BK_OUT]);
}
//
else
}
//
else
pDC
->
DrawText(lpszText,
-
1
, rpCaption, DT_WORDBREAK
|
DT_CENTER);
}
//
if
}
//
End of DrawTheText
//
This function creates a grayscale bitmap starting from a given bitmap.
//
The resulting bitmap will have the same size of the original one.
//
//
Parameters:
//
[IN] hBitmap
//
Handle to the original bitmap.
//
[IN] dwWidth
//
Specifies the bitmap width, in pixels.
//
[IN] dwHeight
//
Specifies the bitmap height, in pixels.
//
[IN] crTrans
//
Color to be used as transparent color. This color will be left unchanged.
//
//
Return value:
//
If the function succeeds, the return value is the handle to the newly created
//
grayscale bitmap.
//
If the function fails, the return value is NULL.
//
HBITMAP CButtonST::CreateGrayscaleBitmap(HBITMAP hBitmap, DWORD dwWidth, DWORD dwHeight, COLORREF crTrans)
{
HBITMAP hGrayBitmap
=
NULL;
HDC hMainDC
=
NULL, hMemDC1
=
NULL, hMemDC2
=
NULL;
HBITMAP hOldBmp1
=
NULL, hOldBmp2
=
NULL;
hMainDC
=
::GetDC(NULL);
if
(hMainDC
==
NULL)
return
NULL;
hMemDC1
=
::CreateCompatibleDC(hMainDC);
if
(hMemDC1
==
NULL)
{
::ReleaseDC(NULL, hMainDC);
return
NULL;
}
//
if
hMemDC2
=
::CreateCompatibleDC(hMainDC);
if
(hMemDC2
==
NULL)
{
::DeleteDC(hMemDC1);
::ReleaseDC(NULL, hMainDC);
return
NULL;
}
//
if
hGrayBitmap
=
::CreateCompatibleBitmap(hMainDC, dwWidth, dwHeight);
if
(hGrayBitmap)
{
hOldBmp1
=
(HBITMAP)::SelectObject(hMemDC1, hGrayBitmap);
hOldBmp2
=
(HBITMAP)::SelectObject(hMemDC2, hBitmap);
//
::BitBlt(hMemDC1, 0, 0, dwWidth, dwHeight, hMemDC2, 0, 0, SRCCOPY);
DWORD dwLoopY
=
0
, dwLoopX
=
0
;
COLORREF crPixel
=
0
;
BYTE byNewPixel
=
0
;
for
(dwLoopY
=
0
; dwLoopY
<
dwHeight; dwLoopY
++
)
{
for
(dwLoopX
=
0
; dwLoopX
<
dwWidth; dwLoopX
++
)
{
crPixel
=
::GetPixel(hMemDC2, dwLoopX, dwLoopY);
byNewPixel
=
(BYTE)((GetRValue(crPixel)
*
0.299
)
+
(GetGValue(crPixel)
*
0.587
)
+
(GetBValue(crPixel)
*
0.114
));
if
(crPixel
!=
crTrans)
::SetPixel(hMemDC1, dwLoopX, dwLoopY, RGB(byNewPixel, byNewPixel, byNewPixel));
else
::SetPixel(hMemDC1, dwLoopX, dwLoopY, crPixel);
}
//
for
}
//
for
::SelectObject(hMemDC1, hOldBmp1);
::SelectObject(hMemDC2, hOldBmp2);
}
//
if
::DeleteDC(hMemDC1);
::DeleteDC(hMemDC2);
::ReleaseDC(NULL, hMainDC);
return
hGrayBitmap;
}
//
End of CreateGrayscaleBitmap
//
This function creates a bitmap that is 25% darker than the original.
//
The resulting bitmap will have the same size of the original one.
//
//
Parameters:
//
[IN] hBitmap
//
Handle to the original bitmap.
//
[IN] dwWidth
//
Specifies the bitmap width, in pixels.
//
[IN] dwHeight
//
Specifies the bitmap height, in pixels.
//
[IN] crTrans
//
Color to be used as transparent color. This color will be left unchanged.
//
//
Return value:
//
If the function succeeds, the return value is the handle to the newly created
//
darker bitmap.
//
If the function fails, the return value is NULL.
//
HBITMAP CButtonST::CreateDarkerBitmap(HBITMAP hBitmap, DWORD dwWidth, DWORD dwHeight, COLORREF crTrans)
{
HBITMAP hGrayBitmap
=
NULL;
HDC hMainDC
=
NULL, hMemDC1
=
NULL, hMemDC2
=
NULL;
HBITMAP hOldBmp1
=
NULL, hOldBmp2
=
NULL;
hMainDC
=
::GetDC(NULL);
if
(hMainDC
==
NULL)
return
NULL;
hMemDC1
=
::CreateCompatibleDC(hMainDC);
if
(hMemDC1
==
NULL)
{
::ReleaseDC(NULL, hMainDC);
return
NULL;
}
//
if
hMemDC2
=
::CreateCompatibleDC(hMainDC);
if
(hMemDC2
==
NULL)
{
::DeleteDC(hMemDC1);
::ReleaseDC(NULL, hMainDC);
return
NULL;
}
//
if
hGrayBitmap
=
::CreateCompatibleBitmap(hMainDC, dwWidth, dwHeight);
if
(hGrayBitmap)
{
hOldBmp1
=
(HBITMAP)::SelectObject(hMemDC1, hGrayBitmap);
hOldBmp2
=
(HBITMAP)::SelectObject(hMemDC2, hBitmap);
//
::BitBlt(hMemDC1, 0, 0, dwWidth, dwHeight, hMemDC2, 0, 0, SRCCOPY);
DWORD dwLoopY
=
0
, dwLoopX
=
0
;
COLORREF crPixel
=
0
;
for
(dwLoopY
=
0
; dwLoopY
<
dwHeight; dwLoopY
++
)
{
for
(dwLoopX
=
0
; dwLoopX
<
dwWidth; dwLoopX
++
)
{
crPixel
=
::GetPixel(hMemDC2, dwLoopX, dwLoopY);
if
(crPixel
!=
crTrans)
::SetPixel(hMemDC1, dwLoopX, dwLoopY, DarkenColor(crPixel,
0.25
));
else
::SetPixel(hMemDC1, dwLoopX, dwLoopY, crPixel);
}
//
for
}
//
for
::SelectObject(hMemDC1, hOldBmp1);
::SelectObject(hMemDC2, hOldBmp2);
}
//
if
::DeleteDC(hMemDC1);
::DeleteDC(hMemDC2);
::ReleaseDC(NULL, hMainDC);
return
hGrayBitmap;
}
//
End of CreateDarkerBitmap
Posted on 2007-04-12 11:47
艾凡赫
阅读(624)
评论(0)
编辑
收藏
引用
所属分类:
MFC技术
只有注册用户
登录
后才能发表评论。
【推荐】100%开源!大型工业跨平台软件C++源码提供,建模,组态!
相关文章:
利用VC从DLL传递消息到EXE
CEdit 颜色设置
文本框字体色,对话框和静态文件背景色设置方法(OnCtlColor消息)
很好的button类 实现文件 4
很好的button类 实现文件 3
很好的button类 实现文件 2
很好的button类 实现文件 1
很好的button类 头文件
CBitmapDialog 的使用
CFormView 设置背景色
网站导航:
博客园
IT新闻
BlogJava
知识库
博问
管理
<
2024年11月
>
日
一
二
三
四
五
六
27
28
29
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
1
2
3
4
5
6
7
常用链接
我的随笔
我的评论
我参与的随笔
留言簿
(19)
给我留言
查看公开留言
查看私人留言
随笔分类
(12)
ADO(2)
Arithmetic
C/C++(1)
COM
DLL
MFC(4)
NET WORK(1)
ORACLE
OTL
OTL
SQL Server
STL
Thread
traits技术
设计模式
兴致所至(4)
随笔档案
(16)
2008年9月 (1)
2007年1月 (8)
2006年12月 (1)
2006年10月 (1)
2006年3月 (1)
2005年9月 (4)
文章分类
(295)
ADO.NET(1)
ADO编程技术(19)
C++(71)
COM(11)
C语言(5)
DLL 技术(13)
Linux(2)
MFC技术(41)
ORACLE(17)
P2P(7)
SqlServer(9)
win32 sdk 编程(7)
Windows Mobile开发(8)
多线程(8)
共享内存
好 玩(1)
基础知识(16)
加密解密(1)
其 他(2)
设计模式
手机编程
数据库(14)
数据类型(7)
算 法(12)
网络编程(23)
文章档案
(261)
2007年12月 (1)
2007年11月 (3)
2007年9月 (1)
2007年4月 (40)
2007年3月 (4)
2007年1月 (1)
2006年12月 (4)
2006年11月 (15)
2006年10月 (4)
2006年9月 (50)
2006年7月 (1)
2006年4月 (5)
2006年1月 (20)
2005年12月 (47)
2005年11月 (56)
2005年10月 (9)
收藏夹
美文
C#
COM
MFC
【 Visual C++ 教 程 】
MFC 教程
VC编程技巧
VC编程网
VC学习
Web
基础知识
PE文件格式详解
汇编语言
数据结构
网络教学
开发学习
天新网
移动开发网
网络
注册过的论坛网站
最新随笔
1. 麦兜故事
2. 转载--地主与长工
3. 呵呵,还不怎么会用,差点出事
4. 第一次
5. 呵呵!开业大吉!!!
搜索
积分与排名
积分 - 482419
排名 - 42
最新评论
1. re: 哈夫曼编码的源代码[未登录]
有没有联系方式,大神求带,本人qq1984425565,大神求带
--无
2. re: 消除回溯算法的程序实现
设计内容及要求:构造一程序,实现:消除文法每一条产生式候选式的公共左因子。对于用户任意输入的文法G,输出一个无回溯的等价文法,可显示输出,或输出到指定文件中。
--王康
3. re: MFC规则DLL 简单的例子
这个文章是从课本上复制的不?
--张亚成
4. re: _variant_t 到 CString 转换
帮了我一把!拜谢
--苦想者
5. re: ADO连接字符串
评论内容较长,点击标题查看
--BestEssays rewiew
阅读排行榜
1. 使用wxWidgets开发跨平台的GUI程序(转)(4453)
2. CRichEditCtrl (3461)
3. 转载--地主与长工 (2630)
4. VC中自动改变控件位置和大小的对话框类(转)(2195)
5. VC6.0中使用Stream Object读取数据中流文件并显示Bmp,JPG等图片 (转)(1747)
评论排行榜
1. 呵呵!开业大吉!!!(5)
2. 转载--地主与长工 (4)
3. 麦兜故事(1)
4. 第一次(1)
5. 呵呵,还不怎么会用,差点出事(1)