⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 listbox.cpp

📁 SimpleGraphicOperatingSystem 32位图形化操作系统 多进程 支持FAT32 详见www.sgos.net.cn
💻 CPP
字号:
#include <osdef.h>//WM_**
#include <System.h>
#include <Api.h>	//Sgos api

namespace System{

	int ListBox::SetVisualPosition( int x, int y ){
		if(x<0)x=0;
		if(y<0)y=0;
        if(y>vScroll->VScrollBar::Max())
            y=vScroll->VScrollBar::Max();
		vX = x;
		vY = y;
		DrawAllItems();
		return true;
	}

	int ListBox::OnSize( int w, int h ){
		Width = w;
		Height = h;
		rows = ( Height - midSpace*2 ) / ch;
        vScroll->Move( Width-vScroll->BaseWindow::Width(), 0, vScroll->BaseWindow::Width(), Height );
        SetScrollBar();
		BaseWindow::OnSize(w,h);
	}

	ListBox::~ListBox(){
		if(vScroll)
			delete vScroll;
	}

	int ListBox::RedrawList()
	{
		DrawAllItems();
	}

	ListBox::ListBox( BaseWindow* parent, string text, int style ):
		BaseWindow( parent, text, style )
	{
		selectColor=Bitmap::MakeColor(0,0,0);	//
		backColor=Bitmap::MakeColor(222,222,222);	//

		textColor1=Bitmap::MakeColor(222,222,222);	//激活时文字颜色
		textColor2=Bitmap::MakeColor(0,0,0);	//非激活时文字颜色

		//获取字符长宽占多少象素
		Bitmap::CalculateTextSize( "Z", 1, FONT_MINI_SIZE, 0, &cw, &ch );
		ch+=1;	//为了好看
        rows=1;
        vY = 0;
        midSpace = 3;
        midSpace = 3;
        selectedIndex = -1;

		this->style = style;

		type = TypeListBox;

		Width = 160;
		Height = 200;

        vScroll = new MyVScroll( this, 0 );
        vScroll->Move( Width-vScroll->BaseWindow::Width(), 0, vScroll->BaseWindow::Width(), Height );
        vScroll->Show();
		SetSize( Width, Height );

		AddItem( string("1") );
		AddItem( string("2") );
		AddItem( string("3") );
		AddItem( string("4") );
		AddItem( string("5") );
        content.Clear();
		//content
	}

	//Count how many lines there are
	int ListBox::GetListLines(){
		return content.Size();
	}

	int ListBox::AddItem( string str )
	{
        AddItem( -1, str );
        return true;
	}

	int ListBox::AddItem( int preIndex, string str )
	{
        content.Add( preIndex, str );
        DrawAllItems();
        SetScrollBar();
        return true;
	}

	int ListBox::RemoveItem( int index )
	{
        content.Remove( index );
        DrawAllItems();
	}

	int ListBox::Clear()
	{
        content.Clear();
        DrawAllItems();
	    return true;
	}

	string ListBox::Item( int index )
	{
	    if( index >=0 && index < content.Size() )
            return content[index];
	}

	int ListBox::EditItem( int index, string str )
	{
	    if( index >=0 && index < content.Size() )
	    {
	        content[index] = str;
            DrawItem( index );
            return true;
        }else{
            return false;
        }
	}

	int ListBox::DrawItem( int index )
	{
	    string text;
	    if( index >=0 && index < content.Size() )
	    {
	        text = content[index];
	    }
	    int begin = vY;
	    if( index>=begin && index<begin+rows )
	    {
	        int x = midSpace<<1, y = midSpace;
	        y += ( index - begin ) * ch;

	        int w = Width-(midSpace<<2) - vScroll->BaseWindow::Width();
	        Rect rect;
	        rect.left = midSpace;
            rect.top = y;
            rect.bottom = y + ch;
            rect.right = rect.left + w + midSpace;
            if( index <0 || index >= content.Size() ){
                Bitmap::RectFill( bitmap, rect.left, rect.top,
                    rect.right - 1, rect.bottom - 1, backColor  );
            }
            else{
                if( index == selectedIndex ){
                    Bitmap::RectFill( bitmap, rect.left, rect.top,
                        rect.right - 1, rect.bottom - 1, selectColor );
                    Bitmap::DrawTextEx( bitmap, text, text.Length(),
                        x, y, &rect, FONT_MINI_SIZE, textColor1, 0 );
                }else{
                    Bitmap::RectFill( bitmap, rect.left, rect.top,
                        rect.right - 1, rect.bottom - 1, backColor  );
                    Bitmap::DrawTextEx( bitmap, text, text.Length(),
                        x, y, &rect, FONT_MINI_SIZE, textColor2, 0 );
                }
            }
            Refresh(rect);
	        return true;
	    }
	}

	int ListBox::DrawAllItems()
	{
        int begin = vY;
        int end = begin + rows - 1;
        for( int i=begin; i<=end; i++ )
        {
            DrawItem( i );
        }
	}


	int ListBox::Remove( int index )
	{
	    if( index >=0 && index < content.Size() )
	    {
	        content.Remove(index);
	    }
	}

	int ListBox::SetSelectedItem( int index )
	{
        int oldIndex = selectedIndex;
        selectedIndex = index;
        DrawItem( oldIndex );
        DrawItem( selectedIndex );
        OnSelect( selectedIndex );
	}

	int ListBox::ItemCount()
	{
	    return content.Size();
	}

	int ListBox::SelectedIndex()
	{
	    return selectedIndex;
	}

	int ListBox::DrawListBox(int type){
		switch( type ){
		case 0:
            Bitmap::RectFill( bitmap, 0, 0, bitmap->Width-1, bitmap->Height-1, Bitmap::MakeColor( 222,222,222 ) );
            Bitmap::Rectangle( bitmap, 0, 0, bitmap->Width-1, bitmap->Height-1, Bitmap::MakeColor( 0, 0, 0 ) );
			break;
		case 1:
            Bitmap::RectFill( bitmap, 0, 0, bitmap->Width-1, bitmap->Height-1, Bitmap::MakeColor( 222,222,222 ) );
            Bitmap::Rectangle( bitmap, 0, 0, bitmap->Width-1, bitmap->Height-1, Bitmap::MakeColor( 0,0,0 ) );
			break;
		}

		if(vScroll){
			vScroll->OnPaint();
		}

		DrawAllItems();
	}

	int ListBox::SetScrollBar(){
		int lines;
		lines = GetListLines();

		int t = lines/rows;
		if(t>=1)
			vScroll->Set(0,lines-rows, 1, rows);
		else
			vScroll->Set(0,0, 0, 0);
		vScroll->SetValue( vY );
	}

	int ListBox::OnChange(){
	}

	int ListBox::OnSelect(int index){

	}

	int ListBox::OnPaint(){
		DrawListBox(0);
		Refresh();
		BaseWindow::OnPaint();
	}

	int ListBox::OnMessage( Message* msg ){
		BaseWindow::OnMessage(msg);
	}


	int ListBox::IndexFromPos(int x, int y ){
		int r=( (short)y - midSpace ) / ch;

		if(r<0){
			r=0;
		}
		if(r>=rows){
			r=rows-1;
		}

		r = r + vY;

		if(r>=content.Size()){
			r=content.Size()-1;
		}
		return r;
	}

	int ListBox::OnMouseDown( int button, Point* p ){
		if(button==1){
		    SetSelectedItem( IndexFromPos( p->x, p->y ) );
		}
		BaseWindow::OnMouseDown(button, p);
	}

	int ListBox::OnMouseUp( int button, Point* p ){
		BaseWindow::OnMouseUp(button, p);
	}

	int ListBox::OnMouseMove( Point* p ){
	}

	int ListBox::OnKeyDown( int code, char ascii, int flag ){
		BaseWindow::OnKeyDown(code,ascii,flag);
	}

	int ListBox::OnKeyUp( int code, char ascii, int flag ){
	}
}

⌨️ 快捷键说明

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