バイト終了

休み明けのリハビリも兼ねて、午前中はバイトの仕事(・∀・)
バーコードスキャンとカメラ撮影をするだけの、単純なサンプルを作成。


ちなみに、ボタンのイメージはOffice 2007の図形を元に作っていたり。
画面イメージがExcelであったので、そのビットマップを取って利用しただけですが。
ボタン押下された時のイメージは、テキスト編集時の平べったいイメージを使用。
Officeの図形って、意外と小物イメージを作るのに使えるよね(・∀・)


後、テキスト表示の所について、背景をグラデーション表示している都合上、背景色は透過にしたいのでPaintイベントで描画しているんですが。
でも、個別に座標を指定するのはタルイし、位置決めはデザインモードでやりたいよね、っということで、こんなコントロールを用意してみました(・∀・)

public class LabelBlock : Control
{
    private Control prevParent;
    private Point location = new Point( 0, 0 );
    private Size size = new Size( 0, 0 );
    private ContentAlignmentEx align = ContentAlignmentEx.MiddleCenter;

    public new Point Location
    {
        get
        {
            if( DesignMode.IsTrue )
            {
                return base.Location;
            }
            else
            {
                return this.location;
            }
        }
        set
        {
            if( DesignMode.IsTrue )
            {
                base.Location = value;
            }
            else
            {
                this.location = value;
            }
            
        }
    }

    public new Size Size
    {
        get
        {
            if( DesignMode.IsTrue )
            {
                return base.Size;
            }
            else
            {
                return this.size;
            }
        }
        set
        {
            if( DesignMode.IsTrue )
            {
                base.Size = value;
            }
            else
            {
                this.size = value;
            }
        }
    }

    public ContentAlignmentEx TextAlign
    {
        get { return this.align; }
        set { this.align = value; }
    }

    public new string Text
    {
        get { return base.Text; }
        set
        {
            base.Text = value;
            UpdateText();
        }
    }

    public new Font Font
    {
        get { return base.Font; }
        set
        {
            base.Font = value;
            UpdateText();
        }
    }

    public new Color ForeColor
    {
        get { return base.ForeColor; }
        set
        {
            base.ForeColor = value;
            UpdateText();
        }
    }

    public LabelBlock()
    {
        if( !DesignMode.IsTrue )
        {
            base.Location = new Point( 0, 0 );
            base.Size = new Size( 0, 0 );
        }
    }

    protected override void Dispose(bool disposing)
    {
        if( disposing )
        {
        }
        base.Dispose( disposing );
    }

    protected override void OnParentChanged(EventArgs e)
    {
        if( !DesignMode.IsTrue )
        {
            if( this.prevParent != null )
            {
                this.prevParent.Paint -= OnParentPaint;
            }

            this.prevParent = this.Parent;

            this.Parent.Paint += OnParentPaint;
        }

        base.OnParentChanged( e );
    }

    void OnParentPaint(object sender, PaintEventArgs e)
    {
        e.Graphics.DrawText( base.Text, base.Font, base.ForeColor, this.location.X, this.location.Y, this.size.Width, this.size.Height, this.align );
    }

    private void UpdateText()
    {
        if( DesignMode.IsTrue )
        {
            Invalidate();
        }
        else
        {
            if( this.prevParent != null )
            {
                this.prevParent.Invalidate( new Rectangle( this.location.X, this.location.Y, this.size.Width, this.size.Height ) );
            }
        }
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        if( DesignMode.IsTrue )
        {
            e.Graphics.DrawText( base.Text, base.Font, base.ForeColor, ClientRectangle, this.align );
        }
    }
}

ContentAlignmentEx、DesignModeクラスとか、Graphics.DrawText拡張メソッドだとか固有の処理がありますが、内容は名前から類推できるであろうのと、言いたいことの本質ではないのでそこは省略(´д`)
このControlが何をしているのかと言えば、デザイン時は普通のControl(ラベル)のように振る舞い、実行時は自身のサイズは0にして自身のクライアント領域への描画は行わず、親のPaintに処理を追加して、デザイン時に指定された位置に文字のみを描画することにより、あたかも背景が透過のラベルのように振る舞うというようなことをやっています。
まあ、手抜きの小技みたいなもんですかね(・ω・)?


っで、午後からはWebの人に戻ります(・ω・)