label.cpp

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

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

namespace System{
	int Label::OnSize( int w, int h ){
		Width = w;
		Height = h;
		BaseWindow::OnSize(w,h);
	}
	Label::~Label(){
	}

	Label::Label( BaseWindow* parent, string text, int style ):
		BaseWindow( parent, text, style )
	{
		//获取字符长宽占多少象素
		Bitmap::CalculateTextSize( "a", 1, FONT_MINI_SIZE, 0, &cw, &ch );
		ch+=1;	//为了好看
		mid_space = 2;

		strText =text;
		this->style = style;
		type = TypeLabel;

		Width = strText.Length()*8+10;
		Height = 21;
		SetSize( Width, Height );
		//Show();
	}

	int Label::DrawLabel(int type){
		if( this->style&LABEL_TRANSPARENT ){
			Bitmap::RectFill( bitmap, 0, 0, bitmap->Width-1, bitmap->Height-1, Bitmap::MAGENTA );
		}
		switch( type ){
		case 0:
			if(!(style&LABEL_NOBACKGROUND))
				Bitmap::RectFill( bitmap, 0, 0, bitmap->Width-1, bitmap->Height-1,
                    winStyle.active.backColor);
			if(!(style&LABEL_NOFRAME))
				Bitmap::Rectangle( bitmap, 0, 0, bitmap->Width-1, bitmap->Height-1,
                    winStyle.active.borderColor);
			break;
		case 1:
			if(!(style&LABEL_NOBACKGROUND))
				Bitmap::RectFill( bitmap, 0, 0, bitmap->Width-1, bitmap->Height-1,
                    winStyle.inactive.backColor);
			if(!(style&LABEL_NOFRAME))
				Bitmap::Rectangle( bitmap, 0, 0, bitmap->Width-1, bitmap->Height-1,
                    winStyle.inactive.borderColor);
			break;
		}

		//文字输出
		int textColor = winStyle.active.textColor;
		int boxw = Width;
		const char* buf = strText.GetData();
		int len = strText.Length ();
		int row=0, col=0, x, y;
		if( cw==0 ) cw = 16;
		for( int i=0; i<len; i++ )
		{
			if( col>=boxw/cw )
			{
				col=0;
				row++;
			}
			x = mid_space+col*cw;
			y = mid_space+row*ch;
			col++;
			switch( buf[i] )
			{
				case '\t':
					Bitmap::DrawText( bitmap, " ", 1, x, y, FONT_MINI_SIZE, textColor, 0 );
					break;
				case '\r':
				case '\n':
					col=0;
					row++;
					break;
				default:
					if( (unsigned char)buf[i]>0xA0  &&  (unsigned char)buf[i+1]>0xA0 ){
						if( col>=boxw/cw-1 )
						{
							col=0;
							row++;
							x = mid_space+col*cw;
							y = mid_space+row*ch;
							col++;
						}
						Bitmap::DrawText( bitmap, &buf[i], 2, x, y, FONT_MINI_SIZE, textColor, 0 );
						i++;
						col++;
					}else{
						Bitmap::DrawText( bitmap, &buf[i], 1, x, y, FONT_MINI_SIZE, textColor, 0 );
					}
			}
		}
		Refresh();
	}

	int Label::OnPaint(){
		DrawLabel(0);
		BaseWindow::OnPaint();
	}

	int Label::OnMessage( Message* msg ){
		switch(msg->message){
		case WM_SETTEXT:
			strText = (char*)msg->param[0];
			OnPaint();
		}
		BaseWindow::OnMessage(msg);
	}
}

⌨️ 快捷键说明

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