Drupal Consultant
Started my career as a drupal8 developer in EM Solutions . I love learning Web technologies like HTML, CSS, PHP, Jquery Ajax and Drupal backend . Currently working as a drupal backend developer.
Để tạo một form hoặc điều kiểm không phải hình chữ nhật, trước hết bạn cần định nghĩa hình dạng mình muốn. Cách tiếp cận dễ nhất là sử dụng đối tượng System.Drawing.Drawing2D.GraphicsPath, nó có thể điều tiết bất kỳ sự kết hợp nào của các hình ellipse, chữ nhật, và cung khép kín.
Bạn có thể thêm các shape vào một đối tượng GraphicsPath bằng các phương thức như AddEllipse, AddRectangle, và AddClosedCurve. Một khi đã hoàn tất việc định nghĩa hình dạng như mong muốn, bạn có thể tạo một đối tượng Region từ GraphicsPath này—chỉ cần trình ra GraphicsPath trong phương thức khởi dựng của lớp Region. Cuối cùng, bạn có thể gán Region vào thuộc tính Form.Region hoặc Control.Region.
Ví dụ dưới đây trình bày cách tạo một form có hình dáng bất thường bằng hai cung tròn (hai cung này được chuyển thành một figure khép kín bằng phương thức GraphicsPath.CloseAllFigures).
using System; using System.Windows.Forms; using System.Drawing; using System.Drawing.Drawing2D; public class IrregularForm : System.Windows.Forms.Form { private System.Windows.Forms.Button cmdClose; private System.Windows.Forms.Label label1; // (Bỏ qua phần mã designer.) private void IrregularForm_Load(object sender, System.EventArgs e) { GraphicsPath path = new GraphicsPath(); Point[] pointsA = new Point[] {new Point(0, 0),new Point(40, 60), new Point(this.Width - 100, 10)}; path.AddCurve(pointsA); Point[] pointsB = new Point[]{new Point(this.Width - 40, this.Height - 60),new Point(this.Width, this.Height),new Point(10, this.Height)}; path.AddCurve(pointsB); path.CloseAllFigures(); this.Region = new Region(path); } private void cmdClose_Click(object sender, System.EventArgs e) { this.Close(); } }