ImageRadioButton.cs
1 // Copyright (c) Microsoft Corporation 2 // The Microsoft Corporation licenses this file to you under the MIT license. 3 // See the LICENSE file in the project root for more information. 4 5 using System.ComponentModel; 6 using System.Drawing; 7 using System.Windows.Forms; 8 9 namespace MouseWithoutBorders 10 { 11 public partial class ImageRadioButton : RadioButton 12 { 13 private Image CheckImage => Checked ? CheckedImage : UncheckedImage; 14 15 private Point _imageLocation; 16 17 [Category("Appearance")] 18 [Description("The bounding rectangle of the check image in local co-ordinates")] 19 [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] 20 public Point ImageLocation 21 { 22 get => _imageLocation; 23 set 24 { 25 _imageLocation = value; 26 Refresh(); 27 } 28 } 29 30 private Point _textLocation; 31 32 [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] 33 public Point TextLocation 34 { 35 get => _textLocation; 36 set 37 { 38 _textLocation = value; 39 Refresh(); 40 } 41 } 42 43 public ImageRadioButton() 44 { 45 InitializeComponent(); 46 } 47 48 private Image _checkedImage; 49 50 [Category("Appearance")] 51 [Description("Image to show when Mouse is pressed on button")] 52 [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] 53 public Image CheckedImage 54 { 55 get => _checkedImage; 56 set 57 { 58 _checkedImage = value; 59 Refresh(); 60 } 61 } 62 63 private Image _uncheckedImage; 64 65 [Category("Appearance")] 66 [Description("Image to show when button is in normal state")] 67 [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] 68 public Image UncheckedImage 69 { 70 get => _uncheckedImage; 71 set 72 { 73 _uncheckedImage = value; 74 Refresh(); 75 } 76 } 77 78 protected override void OnPaint(PaintEventArgs pevent) 79 { 80 if (CheckImage == null) 81 { 82 base.OnPaint(pevent); 83 } 84 else 85 { 86 OnPaintBackground(pevent); 87 pevent.Graphics.DrawImage(CheckImage, ImageLocation.X, ImageLocation.Y, CheckImage.Width, CheckImage.Height); 88 if (!string.IsNullOrEmpty(Text)) 89 { 90 pevent.Graphics.DrawString(Text, Font, Brushes.White, TextLocation); 91 } 92 } 93 } 94 } 95 }