2013-12-21 19 views
0

我做了一個自定義的可移動控件,並且像素計算有問題。爲什麼PenAlignment.Inset在用戶控制圖中生效?

控件大小爲100,100,填充和頁邊距都是0,0,0,0。

我認爲從控制寬度(和高度)減去筆寬兩倍會做,但當我做 從DrawRectangle的底線和右線部分或全部剪裁,取決於pen.Width值和尺寸減量因子。寬度size.Height

protected override void OnPaint(PaintEventArgs e) 
    { 
     base.OnPaint(e); 

     var pen = new Pen(Color.Black); 
     pen.Width = 16; 
     pen.Alignment = System.Drawing.Drawing2D.PenAlignment.Inset; 
     var brush = Brushes.Aquamarine; 
     var size = this.Size; 
     size.Width -= (int)(pen.Width * 2); 
     size.Height -= (int)(pen.Width * 2); 
     var rec = new Rectangle(this.Location, this.Size); 
     e.Graphics.FillRectangle(brush, rec); 
     e.Graphics.DrawRectangle(pen, rec); 
    } 

好像整個圖像外(90,90)點,無論選擇什麼pen.Width的價值裁剪。

編輯:我剛剛發現,當control.Location設置(0,0)時,unwatned剪輯不會發生。

public partial class Form1 : Form 
{ 
    MTC control; 
    public Form1() 
    { 
     InitializeComponent(); 

     control = new MTC(); 
     control.Parent = panel1; 
     control.Width = 100; control.Height = 200; 
     //control.Left = 100; control.Top = 100; 
     control.Location = new Point(0, 0); 
     panel1.Controls.Add(control); 
+0

繪畫是相對於控件的客戶區域。因此你必須使用'var rec = new Rectangle(Point.Empty,size);' –

回答

0

線寬可以是可變的,但是整條粗線的中心處的確切1像素線是沿着矩形繪製的。所以,你應該計算矩形這樣的:

protected override void OnPaint(PaintEventArgs e) 
{ 
    base.OnPaint(e); 

    var pen = new Pen(Color.Black); 
    pen.Width = 16; 
    pen.Alignment = System.Drawing.Drawing2D.PenAlignment.Inset; 
    var brush = Brushes.Aquamarine; 
    var halfPenWidth = pen.Width/2; 
    var rec = new RectangleF(Location, Size); 
    rec.Width -= pen.Width; 
    rec.Height -= pen.Width; 
    rec.X += halfPenWidth; 
    rec.Y += halfPenWidth; 
    e.Graphics.FillRectangle(brush, rec); 
    //Graphics.DrawRectangle does not support RectangleF directly 
    e.Graphics.DrawRectangle(pen, rec.Left, rec.Top, rec.Width, rec.Height); 
} 
0

通過引入另一面板控制並將其添加到真正容器解決了這個。

public class Movable : Control 
{ 
    public Panel ContainerPanel { get; set; }  
    public Movable(Point location, Size size) : base() 
    { 
     var pan = new Panel(); 
     pan.Margin = new Padding(0); 
     pan.Padding = new Padding(0); 
     pan.Location = location; 
     pan.Size = size; 
     this.ContainerPanel = pan; 
     this.Location = new Point(0, 0); 
     this.Size = size; 
     pan.Controls.Add(this); 
     pan.Height = size.Height; // Resize again after addition of 'content' 
     this.movable = isMovable; 
    } 
    // codes for OnMouseDown, OnMouseUp, OnMouseMove, ... 
} 

public class MovableTest : Movable 
{ 
    public MovableTest(Point location, Size size) 
     : base(location, size) { } 
    protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) 
    { 
     base.OnPaint(e); 
     // user drawing code comes here 
    } 
} 

public partial class Form1 : Form 
{ 
    MovableTest ctrl2; 
    ctrl2 = new MovableTest(new Point(75, 50), new Size(100, 100)); 

    // add .ContainerPanel, NOT ctrl2 itself! 
    this.Controls.Add(ctrl2.ContainerPanel); 
} 
相關問題