Advertisement
WerriorCheats

Ellipse Control

Jun 17th, 2025
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.07 KB | None | 0 0
  1. using System;
  2. using System.ComponentModel;
  3. using System.Drawing;
  4. using System.Runtime.InteropServices;
  5. using System.Windows.Forms;
  6.  
  7. namespace Ellipse_Control
  8. {
  9.     internal class EllipseTool : Component
  10.     {
  11.         [DllImport("Gdi32.dll", SetLastError = true)]
  12.         private static extern IntPtr CreateRoundRectRgn(
  13.     int nLeftRect,
  14.     int nTopRect,
  15.     int nRightRect,
  16.     int nBottomRect,
  17.     int nWidthEllipse,
  18.     int nHeightEllipse);
  19.  
  20.         private Control _targetControl;
  21.         private int _cornerRadius = 30;
  22.         private readonly EventHandler _sizeChangedHandler;
  23.  
  24.         public EllipseTool()
  25.         {
  26.             _sizeChangedHandler = new EventHandler(OnControlSizeChanged);
  27.         }
  28.  
  29.         [Category("Appearance")]
  30.         public Control TargetControl
  31.         {
  32.             get => _targetControl;
  33.             set
  34.             {
  35.                 if (_targetControl != null)
  36.                 {
  37.                     _targetControl.SizeChanged -= _sizeChangedHandler;
  38.                 }
  39.                 _targetControl = value;
  40.                 if (_targetControl != null)
  41.                 {
  42.                     _targetControl.SizeChanged += _sizeChangedHandler;
  43.                     UpdateRegion();
  44.                 }
  45.             }
  46.         }
  47.         [Category("Appearance")]
  48.         public int CornerRadius
  49.         {
  50.             get => _cornerRadius;
  51.             set
  52.             {
  53.                 _cornerRadius = value;
  54.                 UpdateRegion();
  55.             }
  56.         }
  57.  
  58.         private void OnControlSizeChanged(object sender, EventArgs e)
  59.         {
  60.             UpdateRegion();
  61.         }
  62.  
  63.         private void UpdateRegion()
  64.         {
  65.             if (_targetControl == null || _targetControl.IsDisposed) return;
  66.  
  67.             IntPtr regionHandle = CreateRoundRectRgn(
  68.                 0, 0,
  69.                 _targetControl.Width + 1,
  70.                 _targetControl.Height + 1,
  71.                 _cornerRadius,
  72.                 _cornerRadius);
  73.             _targetControl.Region = Region.FromHrgn(regionHandle);
  74.         }
  75.     }
  76. }
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement