button.cpp

来自「SimpleGraphicOperatingSystem 32位图形化操作系统 」· C++ 代码 · 共 107 行

CPP
107
字号
#include <osdef.h>//WM_**
#include <System.h>
#include <Api.h>	//Sgos api

namespace System{
	
	
	int Button::OnSize( int w, int h ){
		Width = w;
		Height = h;
		Label::OnSize(w,h);
	}
	Button::~Button(){
	} 
	
	Button::Button( BaseWindow* form, string text, int style ):
		Label( form, text, LABEL_NOBACKGROUND|LABEL_NOFRAME )
	{
		frameColor1=Bitmap::MakeColor(0,0,0);       //激活时窗体边框颜色
		frameColor2=Bitmap::MakeColor(0,0,0);       	//非激活时窗体边框颜色
		frameColor3=Bitmap::MakeColor(0,0,0);       	//按下时窗体边框颜色
		backColor1=Bitmap::MakeColor(188,188,188);	//激活时窗体背景颜色
		backColor2=Bitmap::MakeColor(188,188,188);	//非激活时窗体背景颜色
		backColor3=Bitmap::MakeColor(188,188,188);	//按下时窗体背景颜色
		textColor1=Bitmap::MakeColor(188,188,188);	//激活时文字颜色
		textColor2=Bitmap::MakeColor(188,188,188);	//非激活时文字颜色
		textColor3=Bitmap::MakeColor(188,188,188);	//按下时文字颜色
		
		strText =text;
		
		type = TypeButton;
		Width = strText.Length()*8+12;
		Height = ch+7;
		mid_space = 4;
		SetSize( Width, Height );
	}
	
	int Button::DrawButton(int type){
		switch( type ){
		case 0:
			Bitmap::RectFill( bitmap, 0, 0, bitmap->Width-1, bitmap->Height-1, backColor2 );


                        Bitmap::Line( bitmap, bitmap->Width-2, 0, bitmap->Width-2, bitmap->Height, Bitmap::MakeColor(150,150,150));//右

                        Bitmap::Line( bitmap, 0, bitmap->Height-2, bitmap->Width, bitmap->Height-2, Bitmap::MakeColor(150,150,150));//下
                        Bitmap::Line( bitmap, 1, 0, bitmap->Width-2, 0, frameColor2 );//上
                        Bitmap::Line( bitmap, 0, 1, 0, bitmap->Height-2, frameColor2 );//左
                        Bitmap::Line( bitmap, bitmap->Width-1, 1, bitmap->Width-1, bitmap->Height-2, frameColor2 );//右
                        Bitmap::Line( bitmap, 1, bitmap->Height-1, bitmap->Width-2, bitmap->Height-1, frameColor2 );//下

			break;
		case 1:
			Bitmap::RectFill( bitmap, 0, 0, bitmap->Width-1, bitmap->Height-1, backColor1 );


                        Bitmap::Line( bitmap, bitmap->Width-2, 0, bitmap->Width-2, bitmap->Height, Bitmap::MakeColor(150,150,150));//右

                        Bitmap::Line( bitmap, 0, bitmap->Height-2, bitmap->Width, bitmap->Height-2, Bitmap::MakeColor(150,150,150));//下
                        Bitmap::Line( bitmap, 1, 0, bitmap->Width-2, 0, frameColor2 );//上
                        Bitmap::Line( bitmap, 0, 1, 0, bitmap->Height-2, frameColor2 );//左
                        Bitmap::Line( bitmap, bitmap->Width-1, 1, bitmap->Width-1, bitmap->Height-2, frameColor2 );//右
                        Bitmap::Line( bitmap, 1, bitmap->Height-1, bitmap->Width-2, bitmap->Height-1, frameColor2 );//下


			break;
		case 2:

			Bitmap::RectFill( bitmap, 0, 0, bitmap->Width-1, bitmap->Height-1, backColor3 );

                        Bitmap::Line( bitmap, 0, 1, bitmap->Width, 1, Bitmap::MakeColor(150,150,150));//上
                        Bitmap::Line( bitmap, 1, 0, 1, bitmap->Height, Bitmap::MakeColor(150,150,150));//左
                        Bitmap::Line( bitmap, 1, 0, bitmap->Width-2, 0, frameColor2 );//上
                        Bitmap::Line( bitmap, 0, 1, 0, bitmap->Height-2, frameColor2 );//左
                        Bitmap::Line( bitmap, bitmap->Width-1, 1, bitmap->Width-1, bitmap->Height-2, frameColor2 );//右
                        Bitmap::Line( bitmap, 1, bitmap->Height-1, bitmap->Width-2, bitmap->Height-1, frameColor2 );//下
			break;
		}
		DrawLabel(0);
	}
	
	int Button::OnPaint(){
		DrawButton(0);
		Label::OnPaint();
	}
	
	int Button::OnMouseHover( int button ){
		if( button==1 )
			DrawButton(2);
		else
			DrawButton(1);
	}
	
	int Button::OnMouseLeave( int button ){
		DrawButton(0);
	}
	
	int Button::OnMouseDown( int button, Point* p ){
		if( button==1)
			DrawButton(2);
	}
	
	int Button::OnMouseUp( int button, Point* p ){
		DrawButton(1);
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?