Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class CustomPictureBox : PictureBox
- {
- bool hovered = false;
- Button btnClear;
- public CustomPictureBox()
- {
- this.MouseEnter += new EventHandler(CustomPictureBox_MouseEnter);
- this.MouseLeave += new EventHandler(CustomPictureBox_MouseLeave);
- btnClear = new Button();
- btnClear.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- btnClear.BackColor = System.Drawing.Color.White;
- btnClear.Width = 20;
- btnClear.Height = 20;
- btnClear.Location = new Point(this.Width - btnClear.Width + 1, -1);
- btnClear.Visible = false;
- btnClear.Text = "X";
- this.Controls.Add(btnClear);
- }
- void CustomPictureBox_MouseLeave(object sender, EventArgs e)
- {
- Invalidate();
- hovered = false;
- btnClear.Visible = false;
- }
- void CustomPictureBox_MouseEnter(object sender, EventArgs e)
- {
- Invalidate();
- hovered = true;
- this.Cursor = System.Windows.Forms.Cursors.Hand;
- btnClear.Visible = true;
- }
- protected override void OnPaint(PaintEventArgs e)
- {
- //base.OnPaint(e);
- this.BorderStyle = System.Windows.Forms.BorderStyle.None;
- if (hovered)
- {
- Pen pen = new Pen(Color.DarkGray, 2);
- pen.Alignment = System.Drawing.Drawing2D.PenAlignment.Inset;
- pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
- Rectangle rectangle = new Rectangle(0, 0, this.Width, this.Height);
- e.Graphics.DrawRectangle(pen, rectangle);
- }
- else if (!hovered)
- {
- Pen pen = new Pen(Color.Gray, 1);
- pen.Alignment = System.Drawing.Drawing2D.PenAlignment.Inset;
- pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
- Rectangle rectangle = new Rectangle(0, 0, this.Width -1, this.Height -1);
- e.Graphics.DrawRectangle(pen, rectangle);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement