WEB开发网
开发学院软件开发VC 为你的程序换个皮肤 阅读

为你的程序换个皮肤

 2010-07-06 20:42:50 来源:WEB开发网   
核心提示:这样就对按钮的消息进行了挂钩处理了,就可以重新来绘制按钮了,为你的程序换个皮肤(2),紧接着就是给出按钮控件的绘制方法,我是用一个类来实现的,是不是很简单啊,我只是做了一个简单的实现,都是使用的静态函数直接调用的,#define STATUS_BUTTON_NORMAL0x00000000#define STATUS_

这样就对按钮的消息进行了挂钩处理了,就可以重新来绘制按钮了。紧接着就是给出按钮控件的绘制方法,我是用一个类来实现的,都是使用的静态函数直接调用的。

#define STATUS_BUTTON_NORMAL  0x00000000
#define STATUS_BUTTON_HOVER    0x00000001
#define STATUS_BUTTON_DOWN    0x00000002
class CButtonExt
{
public:
  CButtonExt() {}
  ~CButtonExt() {}
  static UINT    m_nStatus;
  static WNDPROC  m_cWndProc;
  static LRESULT DefWindowProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
  {
    CWnd  *pWnd = NULL;
    CPoint  point;
    pWnd = CWnd::FromHandle( hWnd );
    switch( message )
    {
    case WM_PAINT:
      return OnPaint( pWnd );
      break;
    case WM_LBUTTONDOWN:
      point.x = LOWORD(lParam);
      point.y = HIWORD(lParam);
      return OnLButtonDown( pWnd, 0, point );
      break;
    case WM_LBUTTONUP:
      point.x = LOWORD(lParam);
      point.y = HIWORD(lParam);
      return OnLButtonUp( pWnd, 0, point );
      break;
    case WM_LBUTTONDBLCLK:
      point.x = LOWORD(lParam);
      point.y = HIWORD(lParam);
      return OnLButtonDblClk( pWnd, 0, point );
      break;
    case WM_MOUSEMOVE:
      point.x = LOWORD(lParam);
      point.y = HIWORD(lParam);
      return OnMouseMove( pWnd, 0, point );
      break;
    default:
      break;
    }
    return CallWindowProc( m_cWndProc, hWnd, message, wParam, lParam );
  }
  static LRESULT OnLButtonDown( CWnd *pWnd, UINT nFlags, CPoint point ) {
    m_nStatus = STATUS_BUTTON_DOWN;
    pWnd->Invalidate();
    pWnd->UpdateWindow();
    return TRUE;
  }
  static LRESULT OnLButtonUp( CWnd *pWnd, UINT nFlags, CPoint point ) {
    if( m_nStatus != STATUS_BUTTON_NORMAL ) {
      m_nStatus = STATUS_BUTTON_NORMAL;
      pWnd->Invalidate();
      pWnd->UpdateWindow();
      SendMessage( pWnd->GetParent()->m_hWnd,
             WM_COMMAND,
             pWnd->GetDlgCtrlID(),
             (LPARAM) (pWnd->m_hWnd) );
    }
    return TRUE;
  }
  static LRESULT OnLButtonDblClk( CWnd *pWnd, UINT nFlags, CPoint point ) {
    return TRUE;
  }
  static LRESULT OnMouseMove( CWnd *pWnd, UINT nFlags, CPoint point ) {
    HRGN hRgn = CreateRectRgn( 0, 0, 0, 0 );
    pWnd->GetWindowRgn( hRgn );
    BOOL bIn = PtInRegion( hRgn, point.x, point.y );
    if( bIn ) {
      if( m_nStatus == STATUS_BUTTON_DOWN ) return TRUE;
      if( m_nStatus != STATUS_BUTTON_HOVER ) {
        m_nStatus = STATUS_BUTTON_HOVER;
        pWnd->Invalidate();
        pWnd->UpdateWindow();
        pWnd->SetCapture();
      }
    } else {
      if ( m_nStatus == STATUS_BUTTON_HOVER ) {
        m_nStatus = STATUS_BUTTON_NORMAL;
        pWnd->Invalidate();
        pWnd->UpdateWindow();
        ReleaseCapture();
      }
    }
    DeleteObject( hRgn );
    return TRUE;
  }
  static LRESULT OnPaint( CWnd *pWnd ) {
    CPaintDC  dc(pWnd);
    CString    cs;
    RECT    rc;
    CFont    Font;
    CFont    *pOldFont;
    CBrush    Brush;
    CBrush    *pOldBrush;
    CPen    Pen;
    CPen    *pOldPen;
    POINT    pt;
    pt.x = 2;
    pt.y = 2;
    dc.SetBkMode( TRANSPARENT );
    Font.CreateFont( 12, 0, 0, 0, FW_HEAVY, 0, 0, 0, ANSI_CHARSET, \
        OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, \
        VARIABLE_PITCH | FF_SWISS, "MS Sans Serif" );
    pOldFont = dc.SelectObject( &Font );
    if( m_nStatus == STATUS_BUTTON_DOWN ) {
      Brush.CreateSolidBrush( RGB( 160, 160, 160 ) );
      Pen.CreatePen( PS_SOLID, 1, RGB( 100, 100, 100 ) );
      dc.SetTextColor( RGB( 50, 50, 250 ) );
    } else if( m_nStatus == STATUS_BUTTON_HOVER ) {
      Brush.CreateSolidBrush( RGB( 60, 60, 180 ) );
      Pen.CreatePen( PS_SOLID, 1, RGB( 0, 0, 0 ) );
      dc.SetTextColor( RGB( 250, 250, 50 ) );
    } else if( m_nStatus == STATUS_BUTTON_NORMAL ) {
      Brush.CreateSolidBrush( RGB( 240, 240, 240 ) );
      Pen.CreatePen( PS_SOLID, 1, RGB( 120, 120, 120 ) );
      dc.SetTextColor( RGB( 50, 50, 50 ) );
    }
    pOldBrush = dc.SelectObject( &Brush );
    pOldPen = dc.SelectObject( &Pen );
    pWnd->GetClientRect( &rc );
    dc.RoundRect( &rc, pt );
    HRGN hRgn = CreateRectRgn( rc.left, rc.top, rc.right, rc.bottom );
    pWnd->SetWindowRgn( hRgn, TRUE );
    DeleteObject( hRgn );
    pWnd->GetWindowText( cs );
    dc.DrawText( cs, &rc, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
    dc.SelectObject( pOldFont );
    dc.SelectObject( pOldBrush );
    dc.SelectObject( pOldPen );
    return TRUE;
  }
  static LRESULT OnEraseBkgnd( CWnd *pWnd, CDC *pDC ) {
    return TRUE;
  }
};
UINT  CButtonExt::m_nStatus = STATUS_BUTTON_NORMAL;
WNDPROC  CButtonExt::m_cWndProc = NULL

程序结构还是很清楚的,这时做一些简单的说明。m_nStatus用来标志按钮的状态,m_cWndProc用来保存系统的消息处理函数地址。其他就不用说了吧。 最后,就是如何在程序中使用的问题了。调用方法其实很简单,在CSkinApp类的InitInstance()函数中加入这样的一句话:

IRStartup( GetModuleHandle( NULL ), GetCurrentThreadId() );

在CSkinApp类的ExitInstance()中加入这样的一句话:

IRComplete();

这样就实现了对按钮的换肤,是不是很简单啊,我只是做了一个简单的实现,还有很多工作需要大家一起来做,希望有兴趣的朋友一起来做

本文配套源码

上一页  1 2 

Tags:程序 皮肤

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接