1

PictureBox(4 x 32px画像)に小さな写真をペイントしたいので、オンペイント方法をオーバーライドする必要がありますか、それともPictureBoxを拡張する新しいコンポーネントを作成する必要がありますか?私はこれを試しましたが、これはJavaで働いていましたが、ここではそうではありません。

        this.pictureBox1 = new System.Windows.Forms.PictureBox()
        {
            protected override void OnPaint(PaintEventArgs e)
            {
               // If there is an image and it has a location, 
               // paint it when the Form is repainted.
                Graphics g = e.Graphics;

                // Draw a string on the PictureBox.
                g.DrawString("Test, is that working?",
                new Font("Arial", 10), System.Drawing.Brushes.Blue, new Point(30, 30));
             }
        }

InitializeComponent メソッドの完全なコード:

   private void InitializeComponent()
    {
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Tools));
        this.pictureBox1 = new System.Windows.Forms.PictureBox()
        {
            protected override void OnPaint(PaintEventArgs e)
            {
               // If there is an image and it has a location, 
               // paint it when the Form is repainted.
                Graphics g = e.Graphics;

                // Draw a string on the PictureBox.
                g.DrawString("Test, is that working?",
                new Font("Arial", 10), System.Drawing.Brushes.Blue, new Point(30, 30));
             }
        }
        this.vscrollb = new System.Windows.Forms.VScrollBar();
        this.vScrollBar1 = new System.Windows.Forms.VScrollBar();
        ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
        this.SuspendLayout();
        // 
        // pictureBox1
        // 
        this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Left;
        this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
        this.pictureBox1.InitialImage = null;
        this.pictureBox1.Location = new System.Drawing.Point(0, 0);
        this.pictureBox1.Name = "pictureBox1";
        this.pictureBox1.Size = new System.Drawing.Size(264, 262);
        this.pictureBox1.TabIndex = 0;
        this.pictureBox1.TabStop = false;
        this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
        // 
        // vscrollb
        // 
        this.vscrollb.Location = new System.Drawing.Point(0, 0);
        this.vscrollb.Name = "vscrollb";
        this.vscrollb.Size = new System.Drawing.Size(20, 80);
        this.vscrollb.TabIndex = 0;
        // 
        // vScrollBar1
        // 
        this.vScrollBar1.Dock = System.Windows.Forms.DockStyle.Right;
        this.vScrollBar1.Location = new System.Drawing.Point(267, 0);
        this.vScrollBar1.Name = "vScrollBar1";
        this.vScrollBar1.Size = new System.Drawing.Size(17, 262);
        this.vScrollBar1.TabIndex = 1;
        this.vScrollBar1.Scroll += new System.Windows.Forms.ScrollEventHandler(this.HandleScroll);
        // 
        // Tools
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.BackColor = System.Drawing.Color.Black;
        this.ClientSize = new System.Drawing.Size(284, 262);
        this.Controls.Add(this.vScrollBar1);
        this.Controls.Add(this.pictureBox1);
        this.Name = "Tools";
        this.Text = "Tools";
        ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
        this.ResumeLayout(false);

    }
4

2 に答える 2

5

これは有効なC#コードではありません。OnPaint()のような仮想メソッドをオーバーライドすることは問題ありませんが、PictureBoxから派生したクラスでのみオーバーライドできます。これはうまく機能します。コンパイルすると、新しいコントロールがツールボックスに自動的に追加されるので、フォームに配置できます。

ただし、これは必要ありません。コントロールのPaintイベントを実装するだけです。あなたはすでにそうしました、あなたはそれをpictureBox1_Paint()と名付けました。コードをそこに移動するだけです。

その他の重要なポインタ:InitializeComponent()は絶対に編集しないでください。設計者によって自動生成されます。フォームのデザインを変更するとすぐに、そこに書き込んだコードはすべて失われます。これは、フォームを設計不能にし、設計者がフォームをロードしたときに例外をトリガーするための非常に優れた方法でもあります。また、OnPaint()をオーバーライドする場合は、base.OnPaint()を呼び出すことが重要です。通常のPictureBox配管が引き続き機能するようにします。イメージのペイントとペイントイベントの発生を含みます。少なくともチュートリアルに従うか、Winformsプログラミングに関する本を読むようにしてください。そうしないと、多くの試行錯誤が発生し、ほとんどの場合エラーが発生します。

于 2012-03-26T12:50:29.473 に答える
1

PictureBoxそこからロジックを継承して追加することをお勧めします。したがって、それが属していない場所(親コントロール)にロジックを追加する必要はありません。

public class SpecialPictureBox : PictureBox
{
    protected override void OnPaint(PaintEventArgs e)
    {
        // if you want to execute the original PaintBox logic before you execute your own code, use the next line of code.
        base.OnPaint(e);

        // now do whatever you want

    }
}

その後、SpecialPictureBoxどこでも好きな場所で使用できます。

編集:base.OnPaintコード例に追加

于 2012-03-26T12:08:39.120 に答える