2014-05-19 32 views
1

我需要一種方法來使TextBox看起來像一個平行四邊形,但我無法弄清楚如何做到這一點。我目前有這樣的代碼:畫一個文本框

private void IOBox_Paint(object sender, PaintEventArgs e) 
{ 
    Graphics g = e.Graphics; 
    Point cursor = PointToClient(Cursor.Position); 
    Point[] points = { cursor, new Point(cursor.X + 50, cursor.Y), new Point(cursor.X + 30, cursor.Y - 20), 
         new Point(cursor.X - 20, cursor.Y - 20) }; 
    Pen pen = new Pen(SystemColors.MenuHighlight, 2); 
    g.DrawLines(pen, points); 
} 

但顯然它不工作。要麼我錯位/誤用它,要麼我沒有做正確的事情。 這是我用來添加它的方法。

int IOCounter = 0; 
private void inputOutput_Click(object sender, EventArgs e) 
{ 
    IOBox box = new IOBox(); 
    box.Name = "IOBox" + IOCounter; 
    IOCounter++; 
    box.Location = PointToClient(Cursor.Position); 
    this.Controls.Add(box); 
} 

任何想法如何解決它? IOBox是由我製作的包含TextBox的UserControl。這是合法的嗎?

+1

這並不容易。你可以看看[這裏](http://www.codeproject.com/Articles/17453/Textbox-with-rounded-corners)。您還可以考慮使用WPF技術製作應用程序,以便您輕鬆更改控件的設計 – Tinwor

+0

TextBox是低級別Windows組件,無法使用GDI +輕鬆進行重新設置。改變任何標準Windows控件的支持有限,一般而言,我希望看到任何嘗試的解決方案的圖形人工製品。正如先前的建議,如果你想要擺脫標準的WinForms控件,我認爲WPF是更好的選擇。 – PhillipH

+0

與[此問題]非常相似(http://stackoverflow.com/questions/4360301/can-a-background-image-be-set-on-a-winforms-textbox)。同樣的答案:不。 –

回答

1

如果可能的話,你應該使用WPF製作你的應用程序。 WPF旨在完成您正在嘗試執行的操作。

但是,它可以在WinForms中完成,雖然不容易。您需要創建一個繼承TextBox WinForm控件的新類。這是使一個TextBox看起來像一個圓形的例子:

public class MyTextBox : TextBox 
{ 
    public MyTextBox() : base() 
    { 
     SetStyle(ControlStyles.UserPaint, true); 
     Multiline = true; 
     Width = 130; 
     Height = 119; 
    } 

    public override sealed bool Multiline 
    { 
     get { return base.Multiline; } 
     set { base.Multiline = value; } 
    } 

    protected override void OnPaintBackground(PaintEventArgs e) 
    { 
     var buttonPath = new System.Drawing.Drawing2D.GraphicsPath(); 
     var newRectangle = ClientRectangle; 

     newRectangle.Inflate(-10, -10); 
     e.Graphics.DrawEllipse(System.Drawing.Pens.Black, newRectangle); 
     newRectangle.Inflate(1, 1); 
     buttonPath.AddEllipse(newRectangle); 
     Region = new System.Drawing.Region(buttonPath); 

     base.OnPaintBackground(e); 
    }  
} 

請記住,您仍然可以做其他事情,如裁剪文本等等。但是這應該讓你開始。