Title is No Title

not very good here!

导航

use MyButton is simple,only add two file and then add some code just bellow in any dlg src declear the button control and will ok!

only two places.
//1:
public:
    //DWORD WINAPI  threadProc();
 //long __stdcall threadProc();
 CAboutDlg();

// Dialog Data
 //{{AFX_DATA(CAboutDlg)
 enum { IDD = IDD_ABOUTBOX };
 MyButton m_print;
 MyButton m_testColor;
 MyButton    m_ok;
 //}}AFX_DATA
/////2:

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
 CDialog::DoDataExchange(pDX);
 //{{AFX_DATA_MAP(CAboutDlg)
 DDX_Control(pDX, IDC_Print, m_print);
 DDX_Control(pDX, IDC_testButtonColor, m_testColor);
 DDX_Control(pDX,IDOK,m_ok);
 //}}AFX_DATA_MAP
}
//every thing else not change!!
//but the two files are;
1:
#if !defined(AFX_MYBUTTON_H__C9DA924B_1770_490E_A2C7_597060CC6EB1__INCLUDED_)
#define AFX_MYBUTTON_H__C9DA924B_1770_490E_A2C7_597060CC6EB1__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// MyButton.h : header file
//

/////////////////////////////////////////////////////////////////////////////
// MyButton window

class MyButton : public CButton
{
// Construction
public:
 MyButton();

// Attributes
public:

// Operations
public:
// Operations
public:
 inline void SetNormalTopColor(COLORREF color)
 {
  m_NormalColorTop = color;
 }
 inline void SetNormalBottomColor(COLORREF color)
 {
  m_NormalColorBottom = color;
 }
 inline void SetActiveTextColor(COLORREF color)
 {
  m_ActiveTextColor = color;
 }
 inline void SetNormalTextColor(COLORREF color)
 {
  m_NormalTextColor = color;
 }
 inline void SetSelectTextColor(COLORREF color)
 {
  m_SelectTextColor = color;
 }
 inline void SetFrameColor(COLORREF color)
 {
  m_FrameColor = color;
 }
 inline void SetActiveColor(COLORREF color)
 {
  m_ActiveColor = color;
 }
  // Overrides
 // ClassWizard generated virtual function overrides
 //{{AFX_VIRTUAL(CXPButton)
 protected:
 virtual void PreSubclassWindow();
 //}}AFX_VIRTUAL

// Overrides
 // ClassWizard generated virtual function overrides
 //{{AFX_VIRTUAL(MyButton)
 //}}AFX_VIRTUAL

// Implementation
public:
 virtual ~MyButton();

 // Generated message map functions
protected:
protected:
 //{{AFX_MSG(CXPButton)
 afx_msg void OnMouseMove(UINT nFlags, CPoint point);
 //}}AFX_MSG

 void DrawFace(COLORREF Top, COLORREF Bottom, CRect& rc, CRect CalRc, CDC* pDC);
 void DrawFrame(COLORREF HeightLight, COLORREF ShadowLight, COLORREF FrameColor, CRect& rc, CDC* pDC);
 void DrawItem( LPDRAWITEMSTRUCT lpDrawItemStruct );
 LONG OnMouseLeave(WPARAM, LPARAM);
 BOOL m_bIsMouseMove;

 COLORREF m_FrameColor;
 COLORREF m_ActiveColor;

 COLORREF m_ActiveTextColor;
 COLORREF m_NormalTextColor;
 COLORREF m_SelectTextColor;

 COLORREF m_FrameHeight;
 COLORREF m_FrameShadow;

 COLORREF m_NormalColorTop;
 COLORREF m_NormalColorBottom;


 DECLARE_MESSAGE_MAP()
};

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_MYBUTTON_H__C9DA924B_1770_490E_A2C7_597060CC6EB1__INCLUDED_)
//2:
// MyButton.cpp : implementation file
//

#include "stdafx.h"
#include "testPint2.h"
#include "MyButton.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// MyButton

MyButton::MyButton()
{
 m_bIsMouseMove = 0;

 m_NormalTextColor = RGB(255, 255, 255);
 m_SelectTextColor = RGB(255, 255, 255);
 m_ActiveTextColor = RGB(255, 255, 255);

 m_ActiveColor  = RGB(255, 120, 80);

 m_NormalColorTop = RGB(255, 255, 255);
 m_NormalColorBottom = RGB(0, 128, 255);

 m_FrameColor  = RGB(0, 64, 128);
 m_FrameHeight  = RGB(230, 230, 230);
 m_FrameShadow  = RGB(128, 128, 128);

}

MyButton::~MyButton()
{
}


BEGIN_MESSAGE_MAP(MyButton, CButton)
 //{{AFX_MSG_MAP(MyButton)
  // NOTE - the ClassWizard will add and remove mapping macros here.
 //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// MyButton message handlers
/////////////////////////////////////////////////////////////////////////////
// MyButton message handlers

void MyButton::DrawItem( LPDRAWITEMSTRUCT lpDrawItemStruct )
{
 //*
 CDC* pDC      = CDC::FromHandle(lpDrawItemStruct->hDC);
 DWORD nState  = lpDrawItemStruct->itemState;
 DWORD nAction = lpDrawItemStruct->itemAction;
 CRect rc   = lpDrawItemStruct->rcItem;
 UINT uStyle   = DFCS_BUTTONPUSH;

 pDC->SetBkMode(TRANSPARENT);
 CString strText;
 GetWindowText(strText);

 if( nState & ODS_SELECTED )
 {
  m_bIsMouseMove = 0;

  DrawFace(m_NormalColorBottom, m_NormalColorTop, rc, rc, pDC);
  DrawFrame(m_FrameShadow, m_FrameShadow, m_FrameColor, rc, pDC); 
  //pDC->Draw3dRect(rc, RGB(0,0,0), RGB(0,0,0));

  pDC->SetTextColor(m_SelectTextColor);
 }
 else //Normal
 {
  DrawFace(m_NormalColorTop, m_NormalColorBottom, rc, rc, pDC);
  DrawFrame(m_FrameHeight, m_FrameShadow, m_FrameColor, rc, pDC);

  pDC->SetTextColor(m_NormalTextColor);
 }

 if( m_bIsMouseMove )
 {
  CRect rc2(rc);
  rc2.DeflateRect(2, 2, 2, 2);

  DrawFace(RGB(GetRValue(m_ActiveColor), GetGValue(m_ActiveColor)+100, GetBValue(m_ActiveColor)+100),
   m_ActiveColor, rc, rc, pDC);
  DrawFace(m_NormalColorTop, m_NormalColorBottom, rc2, rc, pDC);

  CBrush NullBrush;
  NullBrush.CreateStockObject(NULL_BRUSH);
  CBrush* pOldBrush = pDC->SelectObject(&NullBrush);
  
  CPen Pen;
  Pen.CreatePen(PS_SOLID, 1, m_FrameColor);
  CPen* pOldPen = pDC->SelectObject(&Pen);
  rc.InflateRect(1,1,1,1);
  pDC->RoundRect(rc, CPoint(3, 3));
  //rc.DeflateRect(1, 1, 1, 1);
  //pDC->Draw3dRect(rc, HeightLight, ShadowLight);
  
  pDC->SelectObject(pOldPen);
  pDC->SelectObject(pOldBrush);

  pDC->SetTextColor(m_ActiveTextColor);
 }
 
 pDC->DrawText(strText, strText.GetLength(),
  &lpDrawItemStruct->rcItem, DT_SINGLELINE|DT_VCENTER|DT_CENTER);
 //*///
}

void MyButton::OnMouseMove(UINT nFlags, CPoint point)
{
 // TODO: Add your message handler code here and/or call default
 if( m_bIsMouseMove == 0 )
 {
  m_bIsMouseMove = 1;
  Invalidate();
  
  TRACKMOUSEEVENT trackmouseevent;
  trackmouseevent.cbSize = sizeof(trackmouseevent);
  trackmouseevent.dwFlags = TME_LEAVE;
  trackmouseevent.hwndTrack = GetSafeHwnd();
  trackmouseevent.dwHoverTime = HOVER_DEFAULT;
  _TrackMouseEvent(&trackmouseevent);
 }
 
 CButton::OnMouseMove(nFlags, point);
}

LONG MyButton::OnMouseLeave(WPARAM, LPARAM)
{
 m_bIsMouseMove = 0;
 Invalidate();

 return 0;
}

void MyButton::PreSubclassWindow()
{
 // TODO: Add your specialized code here and/or call the base class
 UINT nBS = GetButtonStyle();

 // Add BS_OWNERDRAW style
 SetButtonStyle(nBS | BS_OWNERDRAW);

 CButton::PreSubclassWindow();
}

void MyButton::DrawFace(COLORREF Top, COLORREF Bottom, CRect& rc, CRect CalRc, CDC* pDC)
{
 CPen Pen;
 CPen* pOldPen = pDC->SelectObject(&Pen);
 
 int R, G, B;
 R = (GetRValue(Top) - GetRValue(Bottom)) / CalRc.Height();
 G = (GetGValue(Top) - GetGValue(Bottom)) / CalRc.Height();
 B = (GetBValue(Top) - GetBValue(Bottom)) / CalRc.Height();
 
 //R = R>0 ? R : -R;
 //G = G>0 ? G : -G;
 //B = B>0 ? B : -B;

 int ColR = GetRValue(Top), ColG = GetGValue(Top), ColB = GetBValue(Top);
 COLORREF ColMax = Top > Bottom ? Top : Bottom;
 COLORREF ColMin = Top > Bottom ? Bottom: Top;

 for(int i=0; i<rc.Height(); i++)
 {
  ColR -= R;
  ColG -= G;
  ColB -= B;

  /*
  if( ColR >= GetRValue(ColMax) || ColR <= GetRValue(ColMin) ||
   ColG >= GetGValue(ColMax) || ColG <= GetGValue(ColMin) ||
   ColB >= GetBValue(ColMax) || ColB <= GetBValue(ColMin) )
  {
   R = G = B = 0;
  }///*/


  Pen.DeleteObject();
  Pen.CreatePen(PS_SOLID, 1, RGB(ColR, ColG, ColB));
    
  pDC->SelectObject(&Pen);
  
  pDC->MoveTo(rc.left, rc.top+i);
  pDC->LineTo(rc.right, rc.top+i);
 }

 pDC->SelectObject(pOldPen);
}

void MyButton::DrawFrame(COLORREF HeightLight, COLORREF ShadowLight, COLORREF FrameColor, CRect& rc, CDC* pDC)
{
 CBrush NullBrush;
 NullBrush.CreateStockObject(NULL_BRUSH);
 CBrush* pOldBrush = pDC->SelectObject(&NullBrush);

 CPen Pen;
 Pen.CreatePen(PS_SOLID, 1, FrameColor);
 CPen* pOldPen = pDC->SelectObject(&Pen);
 
 pDC->RoundRect(rc, CPoint(3, 3));
 rc.DeflateRect(1, 1, 1, 1);
 pDC->Draw3dRect(rc, HeightLight, ShadowLight);

 pDC->SelectObject(pOldPen);
 pDC->SelectObject(pOldBrush);
}
========================all are end.

posted on 2004-03-21 21:50  abraham  阅读(664)  评论(0编辑  收藏  举报