2012-03-26 1 views
1

PictureBox에서 작은 그림을 페인트하고 싶습니다 (4 x 32px 이미지를 연속으로). 그래서 OnPaint 메서드를 재정의하거나 PictureBox를 확장하는 새 구성 요소를 만들어야합니까? 난 안 여기 자바에서 근무하는,하지만이 시도 : InitializeComponent 메서드의C#에서 재정의 된 OnPaint 메서드가있는 PictureBox

 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)); 
      } 
     } 

전체 코드 :

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); 

    } 
+0

이 코드는 문제없이 작동합니다. – ionden

+0

이것을 InitializeComponent 메소드에 추가했습니다. –

+8

@ PiotrŁużecki : 'InitializeComponent'에 아무 것도 추가하지 마십시오. 변경 될 때마다 디자이너가이 메서드를 덮어 씁니다. "after"를 추가 할 수 있습니다. "InitializeComponent"가 생성자에서 호출됩니다. – ChrFin

답변

3

를 코딩하는 base.OnPaint을 추가했다. OnPaint()와 같은 가상 메서드를 재정의하는 것은 좋지만 PictureBox에서 파생 된 클래스에서만 그렇게 할 수 있습니다. 잘 작동하면 컴파일 한 후에 새 컨트롤이 자동으로 도구 상자에 추가되어 양식에 넣을 수 있습니다.

그러나 이것은 필수 사항은 아닙니다. 컨트롤의 Paint 이벤트를 간단히 구현할 수 있습니다. 이미했는데, 이름을 pictureBox1_Paint()라고했습니다. 코드를 거기로 옮기십시오.

기타 중요한 포인터 : 절대 InitializeComponent()를 편집하십시오. 디자이너가 자동 생성합니다. 양식의 디자인을 수정하자마자 작성한 코드가 손실됩니다. 또한 폼을 디자인 할 수 없도록 만드는 좋은 방법이기 때문에 디자이너가 폼을로드 할 때 예외가 발생합니다. OnPaint()를 재정의하면 base.OnPaint()를 호출하는 것이 중요합니다. 그래서 정상적인 PictureBox 배관이 계속 작동합니다. 이미지 페인팅 및 페인트 이벤트 인상. 적어도 튜토리얼을 따르거나 Winforms 프로그래밍에 대한 책을 읽으십시오. 그렇지 않은 경우 많은 재판과 대부분 오류가 발생합니다.

2

나는 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을 사용할 수 있습니다.

편집 :이 그냥 유효 C# 코드하지 않습니다 예를

+0

그래서 새로운 수업을 만들지 않고는이 일을 할 수 없습니까? OnPaint 또는 다른 것과 같은 일부 이벤트를 호출해야합니까? Toolbox에 이것을 추가 할 수 있습니까? –

+0

새로운 클래스를 만들지 않고도이 작업을 수행 할 수 있지만 특수 기능을 추가하기 위해 상속 된 일반적인 OOP 보안 주체이므로 권장하지 않습니다. 새로운 클래스의 동작은 OnPaint 메서드의 새로운 구현을 제외하고는 이전과 완전히 동일합니다. 프로젝트를 컴파일 한 후 도구 상자 내에서 새 컨트롤을 사용할 수 있습니다. – MatthiasG

+0

한 가지 더 : OnPaint 메서드를 재정의하면 그림 상자가 이전과 같이 그림을 그리지 않습니다. 논리를 오버라이드했기 때문입니다. 자신을 추가하기 전에 PB가 자체 논리를 실행하게하려면 재정의 된 메소드의 첫 번째 호출로 base.OnPaint (e)를 실행해야합니다. – MatthiasG