Chào mừng đến với Diễn đàn lập trình - Cộng đồng lập trình.
Kết quả 1 đến 7 của 7
  1. #1
    Ngày tham gia
    Sep 2015
    Bài viết
    0

  2. #2
    Ngày tham gia
    Sep 2015
    Bài viết
    0

    Tạo control có hình dạng như ý muốn

    giả sử cần thiết kế 1 form có hình dạng như thế này

    công việc đầu tiên là thiết kế 1 cái hình mong muốn = photoshop
    màu hồng là phần ta muốn cắt bỏ
    code như sau:
    tạo 1 class BitmapRegion.cs
    Mã nguồn PHP:
    using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; namespace BitmapRegionTest { /// <summary> /// Summary description for BitmapRegion. /// </summary> public class BitmapRegion { public BitmapRegion() {} /// <summary> /// Create and apply the region on the supplied control /// </summary> /// <param name="control">The Control object to apply the region to</param> /// <param name="bitmap">The Bitmap object to create the region from</param> public static void CreateControlRegion(Control control, Bitmap bitmap) { // Return if control and bitmap are null if(control == null || bitmap == null) return; // Set our control's size to be the same as the bitmap control.Width = bitmap.Width; control.Height = bitmap.Height; // Check if we are dealing with Form here if(control is System.Windows.Forms.Form) { // Cast to a Form object Form form = (Form)control; // Set our form's size to be a little larger that the bitmap just // in case the form's border style is not set to none in the first place form.Width += 15; form.Height += 35; // No border form.FormBorderStyle = FormBorderStyle.None; // Set bitmap as the background image form.BackgroundImage = bitmap; // Calculate the graphics path based on the bitmap supplied GraphicsPath graphicsPath = CalculateControlGraphicsPath(bitmap); // Apply new region form.Region = new Region(graphicsPath); } // Check if we are dealing with Button here else if(control is System.Windows.Forms.Button) { // Cast to a button object Button button = (Button)control; // Do not show button text button.Text = ""; // Change cursor to hand when over button button.Cursor = Cursors.Hand; // Set background image of button button.BackgroundImage = bitmap; // Calculate the graphics path based on the bitmap supplied GraphicsPath graphicsPath = CalculateControlGraphicsPath(bitmap); // Apply new region button.Region = new Region(graphicsPath); } } /// <summary> /// Calculate the graphics path that representing the figure in the bitmap /// excluding the transparent color which is the top left pixel. /// </summary> /// <param name="bitmap">The Bitmap object to calculate our graphics path from</param> /// <returns>Calculated graphics path</returns> private static GraphicsPath CalculateControlGraphicsPath(Bitmap bitmap) { // Create GraphicsPath for our bitmap calculation GraphicsPath graphicsPath = new GraphicsPath(); // Use the top left pixel as our transparent color Color colorTransparent = bitmap.GetPixel(0, 0); // This is to store the column value where an opaque pixel is first found. // This value will determine where we start scanning for trailing opaque pixels. int colOpaquePixel = 0; // Go through all rows (Y axis) for(int row = 0; row < bitmap.Height; row ++) { // Reset value colOpaquePixel = 0; // Go through all columns (X axis) for(int col = 0; col < bitmap.Width; col ++) { // If this is an opaque pixel, mark it and search for anymore trailing behind if(bitmap.GetPixel(col, row) != colorTransparent) { // Opaque pixel found, mark current position colOpaquePixel = col; // Create another variable to set the current pixel position int colNext = col; // Starting from current found opaque pixel, search for anymore opaque pixels // trailing behind, until a transparent pixel is found or minimum width is reached for(colNext = colOpaquePixel; colNext < bitmap.Width; colNext ++) if(bitmap.GetPixel(colNext, row) == colorTransparent) break; // Form a rectangle for line of opaque pixels found and add it to our graphics path graphicsPath.AddRectangle(new Rectangle(colOpaquePixel, row, colNext - colOpaquePixel, 1)); // No need to scan the line of opaque pixels just found col = colNext; } } } // Return calculated graphics path return graphicsPath; } } }  
    code Form1.cs

    Mã nguồn PHP:
    using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; //using System.Runtime.InteropServices; namespace BitmapRegionTest { /// <summary> /// Summary description for Form1. /// </summary> public class Form1 : System.Windows.Forms.Form { // [DllImport("user32.dll")] // private static extern bool PostMessage(IntPtr hWnd, int msg, long wParam, long lParam); private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; private System.Windows.Forms.Button button3; private System.Windows.Forms.Button button4; /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null; // Load our bitmaps private Bitmap bmpFrmBack = new Bitmap(typeof(Form1), "back.bmp"); private Bitmap bmpBob = new Bitmap(typeof(Form1), "bob.bmp"); private Bitmap bmpBobSay = new Bitmap(typeof(Form1), "bobsay.bmp"); private Bitmap bmpSmiles = new Bitmap(typeof(Form1), "smiles.bmp"); private Bitmap bmpSmilesAngry = new Bitmap(typeof(Form1), "smilesangry.bmp"); private Bitmap bmpGreenBtnUp = new Bitmap(typeof(Form1), "greenbtnup.bmp"); private Bitmap bmpGreenBtnDown = new Bitmap(typeof(Form1), "greenbtndown.bmp"); private Bitmap bmpX = new Bitmap(typeof(Form1), "x.bmp"); private Bitmap bmpXSmile = new Bitmap(typeof(Form1), "xsmile.bmp"); // To store the location of previous mouse left click in the form // so that we can use it to calculate the new form location during dragging private Point prevLeftClick; // To determine if it is the first time entry for every dragging of the form private bool isFirst = true; // Acts like a gate to do allow or deny private bool toBlock = true; public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); // Make our bitmap region for the form BitmapRegion.CreateControlRegion(this, bmpFrmBack); // Make our bitmap regions for the buttons BitmapRegion.CreateControlRegion(button1, bmpBob); BitmapRegion.CreateControlRegion(button2, bmpSmiles); BitmapRegion.CreateControlRegion(button3, bmpGreenBtnUp); BitmapRegion.CreateControlRegion(button4, bmpX); } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.button3 = new System.Windows.Forms.Button(); this.button4 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(104, 88); this.button1.Name = "button1"; this.button1.TabIndex = 0; this.button1.Text = "button1"; this.button1.Click += new System.EventHandler(this.button1_Click); this.button1.MouseEnter += new System.EventHandler(this.button1_MouseEnter); this.button1.MouseLeave += new System.EventHandler(this.button1_MouseLeave); // // button2 // this.button2.Location = new System.Drawing.Point(328, 80); this.button2.Name = "button2"; this.button2.TabIndex = 1; this.button2.Text = "button2"; this.button2.Click += new System.EventHandler(this.button2_Click); this.button2.MouseEnter += new System.EventHandler(this.button2_MouseEnter); this.button2.MouseLeave += new System.EventHandler(this.button2_MouseLeave); // // button3 // this.button3.Location = new System.Drawing.Point(184, 200); this.button3.Name = "button3"; this.button3.TabIndex = 2; this.button3.Text = "button3"; this.button3.Click += new System.EventHandler(this.button3_Click); this.button3.MouseEnter += new System.EventHandler(this.button3_MouseEnter); this.button3.MouseLeave += new System.EventHandler(this.button3_MouseLeave); // // button4 // this.button4.Location = new System.Drawing.Point(344, 232); this.button4.Name = "button4"; this.button4.TabIndex = 3; this.button4.Text = "button4"; this.button4.Click += new System.EventHandler(this.button4_Click); this.button4.MouseEnter += new System.EventHandler(this.button4_MouseEnter); this.button4.MouseLeave += new System.EventHandler(this.button4_MouseLeave); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(432, 270); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.button4, this.button3, this.button2, this.button1}); this.Name = "Form1"; this.Text = "Form1"; this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove); this.ResumeLayout(false); } #endregion /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } private void button1_MouseEnter(object sender, System.EventArgs e) { // Make bitmap region for button BitmapRegion.CreateControlRegion(button1, bmpBobSay); } private void button1_MouseLeave(object sender, System.EventArgs e) { // Make bitmap region for button BitmapRegion.CreateControlRegion(button1, bmpBob); } private void button2_MouseEnter(object sender, System.EventArgs e) { // Make bitmap region for button BitmapRegion.CreateControlRegion(button2, bmpSmilesAngry); } private void button2_MouseLeave(object sender, System.EventArgs e) { // Make bitmap region for button BitmapRegion.CreateControlRegion(button2, bmpSmiles); } private void button3_MouseEnter(object sender, System.EventArgs e) { // Make bitmap region for button BitmapRegion.CreateControlRegion(button3, bmpGreenBtnDown); } private void button3_MouseLeave(object sender, System.EventArgs e) { // Make bitmap region for button BitmapRegion.CreateControlRegion(button3, bmpGreenBtnUp); } private void button4_MouseEnter(object sender, System.EventArgs e) { // Make bitmap region for button BitmapRegion.CreateControlRegion(button4, bmpXSmile); } private void button4_MouseLeave(object sender, System.EventArgs e) { // Make bitmap region for button BitmapRegion.CreateControlRegion(button4, bmpX); } private void button1_Click(object sender, System.EventArgs e) { MessageBox.Show("bob"); } private void button2_Click(object sender, System.EventArgs e) { MessageBox.Show("smiles"); } private void button3_Click(object sender, System.EventArgs e) { MessageBox.Show("green button"); } private void button4_Click(object sender, System.EventArgs e) { MessageBox.Show("Exiting now..."); Close(); } /* protected override void OnMouseDown(MouseEventArgs e) { if(e.Button == MouseButtons.Left) { const int HTCAPTION = 2; const int WM_NCLBUTTONDOWN = 161; int ix = e.X & 0xffff; int iy = e.Y & 0xffff; long ll = ix | iy << 16; PostMessage(this.Handle, WM_NCLBUTTONDOWN, HTCAPTION, ll); } } */ private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) { // Check if dragging of the form has occurred if(e.Button == MouseButtons.Left) { // If this is the first mouse move event for left click dragging of the form, // store the current point clicked so that we can use it to calculate the form's // new location in subsequent mouse move events due to left click dragging of the form if(isFirst == true) { // Store previous left click position prevLeftClick = new Point(e.X, e.Y); // Subsequent mouse move events will not be treated as first time, until the // left mouse click is released or other mouse click occur isFirst = false; } // On subsequent mouse move events with left mouse click down. (During dragging of form) else { // This flag here is to allow alternate processing for dragging the form because it // causes serious flicking when u allow every such events to change the form's location. // You can try commenting this out to see what i mean if(toBlock == false) this.Location = new Point(this.Location.X + e.X - prevLeftClick.X, this.Location.Y + e.Y - prevLeftClick.Y); // Store new previous left click position prevLeftClick = new Point(e.X, e.Y); // Allow or deny next mouse move dragging event toBlock = !toBlock; } } // This is a new mouse move event so reset flag else isFirst = true; } } }  
    kết quả là thế này

  3. #3
    còn cách nữa là set ngay transparent của form = màu muốn cắt => cách này ngắn nhưng có nhược điểm lớn :
    1. thỉnh thoảng làm lác form
    2. nó tác động lên mọi thứ trên form,hễ có màu hồng là nó transparent vô tội vạ ,
    còn cách trên thì ta dùng control nào thì nó chỉ transparent control đó thôi

  4. #4
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Hay đấy nhưng khá giống bài viết bên này: http://www.codeproject.com/KB/graphi...?display=Print
    [IMG]images/smilies/biggrin.png[/IMG]

  5. #5
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Trích dẫn Gửi bởi Xcross87
    Hay đấy nhưng khá giống bài viết bên này: http://www.codeproject.com/KB/graphi...?display=Print
    [IMG]images/smilies/biggrin.png[/IMG]
    chính xác , tớ đâu nói tớ viết [IMG]images/smilies/biggrin.png[/IMG]

  6. #6
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Trích dẫn Gửi bởi zstar
    còn cách nữa là set ngay transparent của form = màu muốn cắt => cách này ngắn nhưng có nhược điểm lớn :
    1. thỉnh thoảng làm lác form
    2. nó tác động lên mọi thứ trên form,hễ có màu hồng là nó transparent vô tội vạ ,
    còn cách trên thì ta dùng control nào thì nó chỉ transparent control đó thôi
    1. Không phải thỉnh thoảng mà là cách viết này mỗi khi repaint nó sẽ thực hiện rất nhiều tính toán và tốn memory, điều đó sẽ dẫn đến trạng thái lag. Ví dụ nếu bạn di chuyển cái form.
    2. Không phải cứ màu hồng là nó transparent tội vạ đâu bạn. Thì bạn chọn cái màu khác.

  7. #7
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    bạn có thể lấy vidu với một cái button thui được không ah
    mình mới học c# nên còn nhiều cái chưa hiểu>>> thanhk<<<
    nếu bạn có soures về game tập gõ bàn phim thì có thể share cho mình được không?.mình đang phải làm bài đấy[IMG]images/smilies/waiting.gif[/IMG]

 

 

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •