#pragma once
#include "stdafx.h"
enum btn_state{nomal,down,up,over};
#define WM_BTN_UP WM_USER+1
#define WM_BTN_DOWN WM_USER+2
#define WM_BTN_MOVE WM_USER+3
class CUS_BTN
{
public:
CUS_BTN(void);
CUS_BTN(int left,int top,int width,int height,Gdiplus::Image* img,HWND parent_hwnd,UINT id);
HWND parent_hwnd;
btn_state CUS_STATE;///按钮状态
Image *img_src;
UINT id;
RECT rect;
bool create_btn(int left,int top,int width,int height,Gdiplus::Image* img,HWND m_hwd,UINT id);
bool create_btn(int left,int top,Gdiplus::Image* img,HWND m_hwd,UINT id);
UINT get_button_id();
btn_state get_button_state();//得到按钮状态
bool set_button_state(btn_state sta); //设置状态
int get_button_width();//得到宽度
int get_button_height();//得到高度
bool set_button_width(int width_num);//得到宽度
bool set_button_height(int height_num);//得到高度
int get_button_left();
int get_button_top();
int get_button_right();
int get_button_bottom();
RECT get_button_rect();
void DRAW_UI(Gdiplus::Graphics& g);
void on_lbutton_down(POINT& pt);
void on_lbutton_up(POINT& pt);
void on_mouse_move(POINT& pt);
~CUS_BTN(void);
};
.cpp
#include "StdAfx.h"
#include "US_BTN.h"
CUS_BTN::CUS_BTN(void)
{
id = 0;
rect.left = 0;
rect.top = 0;
rect.bottom = 0;
rect.right = 0;
img_src = NULL;
parent_hwnd = NULL;
}
CUS_BTN::CUS_BTN(int left,int top,int width,int height,Gdiplus::Image* img,HWND m_hwd,UINT id)
{
rect.left = left;
rect.top = top;
rect.bottom = top+height;
rect.right = left+width;
img_src = img;
parent_hwnd = m_hwd;
this->id = id;
}
CUS_BTN::~CUS_BTN(void)
{
}
bool CUS_BTN::create_btn(int left,int top,int width,int height,Gdiplus::Image* img,HWND m_hwd,UINT id)
{
if (img != NULL)
{
rect.left = left;
rect.top = top;
rect.bottom = top+height;
rect.right = left+width;
img_src = img;
parent_hwnd = m_hwd;
this->id = id;
return TRUE;
}
return false;
}
bool CUS_BTN::create_btn(int left,int top,Gdiplus::Image* img,HWND m_hwd,UINT id)
{
if (img != NULL)
{
rect.left = left;
rect.top = top;
if (img->GetWidth() > img->GetHeight())
{
rect.bottom = img->GetHeight()+rect.top;
rect.right = img->GetWidth()/4+rect.left;
}else
{
rect.bottom = img->GetHeight()/4+rect.top;
rect.right = img->GetWidth()+rect.left;
}
img_src = img;
parent_hwnd = m_hwd;
this->id = id;
return TRUE;
}
return false;
}
btn_state CUS_BTN::get_button_state()//得到按钮状态
{
return CUS_STATE;
}
bool CUS_BTN::set_button_state(btn_state sta)
{
CUS_STATE = sta;
return true;
}
int CUS_BTN::get_button_width()
{
return rect.right-rect.left;
}
int CUS_BTN::get_button_height()
{
return rect.bottom-rect.top;
}
int CUS_BTN::get_button_left()
{
return rect.left;
}
int CUS_BTN::get_button_top()
{
return rect.top;
}
int CUS_BTN::get_button_right()
{
return rect.right;
}
int CUS_BTN::get_button_bottom()
{
return rect.bottom;
}
void CUS_BTN::DRAW_UI(Gdiplus::Graphics& g)
{
if (get_button_state() == nomal)
{
if (img_src->GetWidth() > img_src->GetHeight())
{
g.DrawImage(img_src,rect.left,rect.top,img_src->GetWidth()/4*0,0,img_src->GetWidth()/4,img_src->GetHeight(),Gdiplus::UnitPixel);
}else
{
g.DrawImage(img_src,rect.left,rect.top,0,img_src->GetHeight()/4*0,img_src->GetWidth(),img_src->GetHeight()/4,Gdiplus::UnitPixel);
}
}else if (get_button_state() == down)
{
if (img_src->GetWidth() > img_src->GetHeight())
{
g.DrawImage(img_src,rect.left,rect.top,img_src->GetWidth()/4*2,0,img_src->GetWidth()/4,img_src->GetHeight(),Gdiplus::UnitPixel);
}else
{
g.DrawImage(img_src,rect.left,rect.top,0,img_src->GetHeight()/4*2,img_src->GetWidth(),img_src->GetHeight()/4,Gdiplus::UnitPixel);
}
}else if (get_button_state() == up)
{
if (img_src->GetWidth() > img_src->GetHeight())
{
g.DrawImage(img_src,rect.left,rect.top,img_src->GetWidth()/4*3,0,img_src->GetWidth()/4,img_src->GetHeight(),Gdiplus::UnitPixel);
}else
{
g.DrawImage(img_src,rect.left,rect.top,0,img_src->GetHeight()/4*3,img_src->GetWidth(),img_src->GetHeight()/4,Gdiplus::UnitPixel);
}
}else if (get_button_state() == over)
{
if (img_src->GetWidth() > img_src->GetHeight())
{
g.DrawImage(img_src,rect.left,rect.top,img_src->GetWidth()/4*1,0,img_src->GetWidth()/4,img_src->GetHeight(),Gdiplus::UnitPixel);
}else
{
g.DrawImage(img_src,rect.left,rect.top,0,img_src->GetHeight()/4*1,img_src->GetWidth(),img_src->GetHeight()/4,Gdiplus::UnitPixel);
}
}
return;
}
RECT CUS_BTN::get_button_rect()
{
return this->rect;
}
bool CUS_BTN::set_button_width(int width_num)
{
rect.right = rect.left+width_num;
return TRUE;
}
bool CUS_BTN::set_button_height(int height_num)
{
rect.bottom = rect.top+height_num;
return TRUE;
}
void CUS_BTN::on_lbutton_down(POINT& pt)
{
if (::PtInRect(&get_button_rect(),pt))
{
if (get_button_state() != down)
{
set_button_state(down);
::InvalidateRect(parent_hwnd,&get_button_rect(),TRUE);
::SendMessage(parent_hwnd,WM_BTN_DOWN,id,0);
}
}
}
void CUS_BTN::on_lbutton_up(POINT& pt)
{
if (::PtInRect(&get_button_rect(),pt))
{
if (get_button_state() != up)
{
set_button_state(up);
::InvalidateRect(parent_hwnd,&get_button_rect(),TRUE);
::SendMessage(parent_hwnd,WM_BTN_UP,id,0);
}
}
if (!::PtInRect(&get_button_rect(),pt))
{
if (get_button_state() != nomal)
{
set_button_state(nomal);
::InvalidateRect(parent_hwnd,&get_button_rect(),TRUE);
}
}
}
void CUS_BTN::on_mouse_move(POINT& pt)
{
if (::PtInRect(&get_button_rect(),pt))
{
if (get_button_state() != over)
{
if (get_button_state() != down)
{
set_button_state(over);
::InvalidateRect(parent_hwnd,&get_button_rect(),TRUE);
::SendMessage(parent_hwnd,WM_BTN_MOVE,id,0);
}
}
}else
{
if (get_button_state() != nomal)
{
if (get_button_state() != down)
{
set_button_state(nomal);
::InvalidateRect(parent_hwnd,&get_button_rect(),TRUE);
}
}
}
}
UINT CUS_BTN::get_button_id()
{
return id;
}