123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458 |
- #include "stdafx.h"
- #include "MyButton.h"
- #include "ButtonGroup.h"
- #include <atlimage.h>
- IMPLEMENT_DYNAMIC(CMyButton, CButton)
- CMyButton::CMyButton():
- btnState(0)
- ,colorState(0)
- ,lastColorState(0)
- ,chooseState(0)
- ,strInfo("")
- {
- }
- CMyButton::~CMyButton()
- {
- }
- BEGIN_MESSAGE_MAP(CMyButton, CButton)
- ON_CONTROL_REFLECT_EX(BN_CLICKED,OnClicked)
- ON_WM_MOUSEMOVE()
- ON_WM_LBUTTONDOWN()
- ON_WM_LBUTTONUP()
- ON_WM_PAINT()
- ON_MESSAGE(WM_MOUSELEAVE,OnMouseLeave)
- END_MESSAGE_MAP()
- void CMyButton::OnMouseMove(UINT nFlags, CPoint point)
- {
- CButton::OnMouseMove(nFlags, point);
- CRect rc;
- GetClientRect(&rc);
- if(rc.PtInRect(point))
- {
- TRACKMOUSEEVENT tme;
- tme.cbSize = sizeof(tme);
- tme.hwndTrack = m_hWnd;
- tme.dwFlags = TME_LEAVE | TME_HOVER;
- tme.dwHoverTime = 1;
- _TrackMouseEvent(&tme);
- if (btnState == 0 && chooseState == 0)
- {
- colorState = 0;//鼠标没有按下且没被选中时btn不改变颜色
- }
- else if (btnState == 1)
- {
- colorState = 1;
- }
- }
- else
- {
- colorState = 0;
- }
- if (lastColorState != colorState)
- {
- Invalidate( false );
- lastColorState = colorState;
- }
- }
- LRESULT CMyButton::OnMouseLeave(WPARAM wParam, LPARAM lParam)
- {
- if (type == 0)
- {
- colorState = 0;
- }
- else if (type == 1)
- {
- }
- if (lastColorState != colorState)
- {
- Invalidate( false );
- lastColorState = colorState;
- }
- return 0;
- }
- void CMyButton::OnLButtonDown(UINT nFlags, CPoint point)
- {
- btnState = 1;
- colorState = 1;
- CButton::OnLButtonDown(nFlags, point);
- }
- void CMyButton::OnLButtonUp(UINT nFlags, CPoint point)
- {
- btnState = 0;
- if (type == 0)
- {
- colorState = 0;
- }
- else if (type == 1)
- {
- CRect rc;
- GetClientRect(&rc);
- if(rc.PtInRect(point))
- {
- colorState = 1;
- }
- else
- {
- colorState = 0;
- }
- }
- CButton::OnLButtonUp(nFlags, point);
- }
- BOOL CMyButton::OnClicked()
- {
- pParent->handleClick(index);
- return FALSE;
- }
- void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
- {
- drawButton();
- }
- void CMyButton::drawButton()
- {
- CClientDC dc(this);
- dc.SetBkMode( TRANSPARENT );
- CRect rc;
- GetClientRect(rc);
- CFont BigFont, NozFont, ItemFont;
- CFont *pFont = NULL;
- int nBigFontH = 30;
- int nNozFontH = 19;
- int nItemFoldH = 16;
- if( 0 != NozFont.CreateFont( nNozFontH, 0,0,0, FW_NORMAL ,0,0,0,DEFAULT_CHARSET,
- OUT_STROKE_PRECIS,CLIP_STROKE_PRECIS,DRAFT_QUALITY,
- VARIABLE_PITCH|FF_SWISS, _T("微软雅黑") ) )
- {
- pFont = dc.SelectObject(&NozFont);
- }
- CPen bgPen;
- bgPen.CreatePen( PS_SOLID,1,RGB( 255,255,255 ) );
- CPen* pOldPen = dc.SelectObject( &bgPen );
- CBrush brh1;
- if (colorState == 0)
- {
- brh1.CreateSolidBrush(RGB(255,255,255));
- }
- else if (colorState == 1)
- {
- brh1.CreateSolidBrush(RGB(117,197,240));
- }
- CBrush *pOldBrush = dc.SelectObject( &brh1 );
- CBrush brh2;
- CFont font;
- font.CreateFont(-MulDiv(int(18),72,72),
- 0,0,0,FW_MEDIUM,0,0,0,GB2312_CHARSET,
- OUT_STROKE_PRECIS,CLIP_STROKE_PRECIS,PROOF_QUALITY,
- FIXED_PITCH|FF_SWISS, "微软雅黑" );
- dc.SelectObject(&font);
- COLORREF color;
- TRIVERTEX vert[2] ;
- GRADIENT_RECT gRect;
- vert [0] .x = rc.left;
- vert [0] .y = rc.top;
- vert [1] .x = rc.right;
- vert [1] .y = rc.bottom;
- if (colorState == 1 || strInfo.Find(_T("已选")) != -1)
- {
- //绿 (194,239,150) (118,195,44)
- // vert [0] .Red = 0xc200;
- // vert [0] .Green = 0xef00;
- // vert [0] .Blue = 0x9600;
- // vert [0] .Alpha = 0xff00;
- //
- // vert [1] .Red = 0x7600;
- // vert [1] .Green = 0xc300;
- // vert [1] .Blue = 0x2c00;
- // vert [1] .Alpha = 0xff00;
- //蓝2 (15,220,247) (0,87,127)
- vert [0] .Red = 0x0f00;
- vert [0] .Green = 0xdc00;
- vert [0] .Blue = 0xf700;
- vert [0] .Alpha = 0xff00;
- vert [1] .Red = 0x0000;
- vert [1] .Green = 0x5700;
- vert [1] .Blue = 0x7f00;
- vert [1] .Alpha = 0xff00;
- brh2.CreateSolidBrush(RGB(0,87,127));
-
- color = RGB(18,71,91);
- }
- else if (colorState == 0)
- {
- if (index == 0)
- {
- //橙 (243,196,107) (252,152,0)
- vert [0] .Red = 0xf300;
- vert [0] .Green = 0xc400;
- vert [0] .Blue = 0x6b00;
- vert [0] .Alpha = 0xff00;
- vert [1] .Red = 0xfc00;
- vert [1] .Green = 0x9800;
- vert [1] .Blue = 0x0000;
- vert [1] .Alpha = 0xff00;
- brh2.CreateSolidBrush(RGB(252,152,0));
- color = RGB(88,36,27);
- }
- else if (index >= 1 && index <= 2)
- {
- //蓝 (150,196,235) (39,148,228)
- vert [0] .Red = 0x9600;
- vert [0] .Green = 0xc400;
- vert [0] .Blue = 0xeb00;
- vert [0] .Alpha = 0xff00;
- vert [1] .Red = 0x2700;
- vert [1] .Green = 0x9400;
- vert [1] .Blue = 0xe400;
- vert [1] .Alpha = 0xff00;
- brh2.CreateSolidBrush(RGB(51,51,153));
- color = RGB(51,51,153);
- }
- else if (index >= 3 && index <= 5)
- {
- //黄 (239,240,113) (231,159,67)
- vert [0] .Red = 0xef00;
- vert [0] .Green = 0xf000;
- vert [0] .Blue = 0x7100;
- vert [0] .Alpha = 0xff00;
- vert [1] .Red = 0xe700;
- vert [1] .Green = 0xc300;
- vert [1] .Blue = 0x4300;
- vert [1] .Alpha = 0xff00;
- brh2.CreateSolidBrush(RGB(231,159,67));
- color = RGB(51,47,3);
- }
- else if (index >= 6 && index <= 9)
- {
- //深 (178,202,224) (137,160,175)
- vert [0] .Red = 0xb200;
- vert [0] .Green = 0xca00;
- vert [0] .Blue = 0xe000;
- vert [0] .Alpha = 0xff00;
- vert [1] .Red = 0x8900;
- vert [1] .Green = 0xa000;
- vert [1] .Blue = 0xaf00;
- vert [1] .Alpha = 0xff00;
- brh2.CreateSolidBrush(RGB(137,160,175));
- color = RGB(38,45,49);
- }
- else
- {
- //灰 229 169
- vert [0] .Red = 0xe500;
- vert [0] .Green = 0xe500;
- vert [0] .Blue = 0xe500;
- vert [0] .Alpha = 0xff00;
- vert [1] .Red = 0xa900;
- vert [1] .Green = 0xa900;
- vert [1] .Blue = 0xa900;
- vert [1] .Alpha = 0xff00;
- brh2.CreateSolidBrush(RGB(169,169,169));
- color = RGB(50,50,50);
- }
- }
- gRect.UpperLeft = 0;
- gRect.LowerRight = 1;
- GradientFill(dc.m_hDC,vert,2,&gRect,1,GRADIENT_FILL_RECT_V);
- pOldBrush = dc.SelectObject( &brh2 );
- CRgn rgn;
- rgn.CreateRoundRectRgn(rc.left,rc.top,rc.right,rc.bottom,15,15);
- dc.FrameRgn(&rgn,&brh2,1,1);
- CRgn rgn3;
- rgn3.CreateRoundRectRgn(rc.left-5,rc.top-5,rc.right+5,rc.bottom+5,15,15);
- CBrush brh3(RGB( 51,55,68));
- dc.FrameRgn(&rgn3,&brh3,5,5);
- CRect rcBlank(13,75,162,102);
- CBrush brh4(RGB( 255,255,255));
- pOldBrush = dc.SelectObject( &brh4 );
- if (type == 0)
- {
- dc.RoundRect(rcBlank,CPoint(8,8));
- }
- CBrush brh(RGB( 57,171,229));
- pOldBrush = dc.SelectObject( &brh );
- dc.SetTextColor(color);
- CRect rcStr(rc);
- if (type == 0)
- {
- rcStr.bottom = 74;
- if (strInfo != "")
- {
- CRect rcText(rcBlank);
- rcText.OffsetRect(0,2);
- dc.DrawText(strInfo,rcText,DT_CENTER|DT_VCENTER);
- }
- }
- drawlines(&dc,name,rcStr,14,3,22);
- dc.SelectObject( pOldBrush );
- dc.SelectObject( pOldPen );
-
- bgPen.DeleteObject();
- brh1.DeleteObject();
- brh2.DeleteObject();
- brh.DeleteObject();
- NozFont.DeleteObject();
- }
- void CMyButton::drawlines(CDC* pDC, CString str, CRect rc, UINT charNumOfLine, UINT linenum, UINT rowHeight)
- {
- vector<CString> vStrs;
- CString tmpStr = str;
- while (tmpStr!="" && vStrs.size() < linenum)
- {
- int pos = getDividePos(tmpStr,charNumOfLine);
- CString tmpStr1 = tmpStr.Left(pos);
- vStrs.push_back(tmpStr1);
- if (tmpStr1 == tmpStr)
- {
- tmpStr = "";
- }
- else
- {
- tmpStr = tmpStr.Mid(pos);
- }
- }
-
- UINT headAlign = (rc.bottom - rc.top - vStrs.size() * rowHeight) / 2;
- CRect rc1(rc);
- rc1.top += headAlign;
- rc1.left += 5;
- rc1.right -=5;
- for (size_t i = 0; i < vStrs.size(); i++)
- {
- rc1.bottom = rc1.top + rowHeight;
- pDC->DrawText(vStrs[i],rc1,DT_CENTER|DT_VCENTER);
- rc1.top += rowHeight;
- }
- }
- int CMyButton::getDividePos(CString str, int num)
- {
- if( str.GetLength() > num )
- {
- int numOfChineseChar = 0;
- for ( int i = 0; i < num; i ++ )
- {
- if ( str[i] < 0 || str[i] > 255 )
- {
- numOfChineseChar ++;
- }
- }
- if ( numOfChineseChar % 2 == 0 )
- {
- if (str.GetAt(num-1) == 'm')
- {
- return num - 1;
- }
- else
- {
- return num;
- }
- }
- else
- {
- if (str.GetAt(num-1) == 'm')
- {
- return num - 2;
- }
- else
- {
- return num-1;
- }
- }
- }
- else
- {
- return str.GetLength();
- }
- }
- void CMyButton::changeColor(bool change)
- {
- if (change)
- {
- chooseState = 1;
- colorState = 1;
- }
- else
- {
- chooseState = 0;
- colorState = 0;
- }
- if (lastColorState != colorState)
- {
- Invalidate( false );
- lastColorState = colorState;
- }
- }
- void CMyButton::OnPaint()
- {
- CButton::OnPaint();
- }
|