0

I'm trying to make something similar to Osu's flashlight mode, but on your desktop using Windows Form App. I don't know of any ways of making one specific part of the form transparent. I've tried making a second form that is transparent and putting that on top but, that just shows the form underneath. I was thinking maybe i change the opacity based on distance to mouse but i don't know of any ways in doing that. I've looked all over for a way to do this to no prevail, so thanks in advance!

MaxieDev
  • 1
  • 3
  • 1
    win forms or wpf? You probably need to make your form an "irregular shape". – Jeremy Lakeman Aug 06 '20 at 02:36
  • i'm sorry i'm still new to this. what do you mean by "win forms or wpf"? – MaxieDev Aug 06 '20 at 02:44
  • https://www.codeproject.com/Questions/780311/How-do-I-cut-a-hole-on-a-Form vs https://www.c-sharpcorner.com/UploadFile/mahesh/shaped-windows-in-wpf/ – Jeremy Lakeman Aug 06 '20 at 03:06
  • What type of project did you start with when you created it? The answers you'll get to this question will vary greatly based on the type of app... – Idle_Mind Aug 06 '20 at 03:06
  • ah im sorry, i was using Windows Form App – MaxieDev Aug 06 '20 at 03:17
  • Winforms have a [transparencykey](https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.form.transparencykey?view=netcore-3.1) property that declares a color that shall be rendered transparent. Set it to eg magenta, then [paint a magenta circle on the form](https://docs.microsoft.com/en-us/dotnet/framework/winforms/advanced/how-to-draw-a-filled-ellipse-on-a-windows-form), then make it follow the mouse. This last part might be the hard bit because transparent areas of forms transfer mouse events to the windows beneath them so painting aroun the mouse might prevent you from seeing... – Caius Jard Aug 06 '20 at 04:58
  • ...mouse move events. ie if you're hoping to wire it up to mousemove event (try it, I don't know) you might find your circle jumps around every time the mouse touches the edge of the hole. It might be necessary to poll for the absolute mouse position and work out where it is on your form – Caius Jard Aug 06 '20 at 04:58
  • I'm running into a similar issue as earlier where if i draw the circle on top it just displays the black background underneath, when i changed the transparencykey to the correct color my ellipse just disappeared – MaxieDev Aug 06 '20 at 05:25
  • Example: `public Form1() { InitializeComponent(); TransparencyKey = Color.Pink; DoubleBuffered = true; } Rectangle r = Rectangle.Empty; private void Form1_Paint(object sender, PaintEventArgs e) { Point pt = PointToClient( Control.MousePosition); Graphics g = e.Graphics; g.FillEllipse(SystemBrushes.Control, r); g.FillEllipse(Brushes.Pink, pt.X - 50, pt.Y - 50, 100, 100); r = new Rectangle(pt.X - 50, pt.Y - 50, 100, 100); } private void Form1_MouseMove(object sender, MouseEventArgs e) { Invalidate();}` – TaW Aug 06 '20 at 10:10
  • Some colors will make the area click trough other won't compare Fuchsia with a rando RGB color! - You may want to test for a mousebutton so you can leave the hole in place when moving the mouse off the form..: `if (e.Button.HasFlag(MouseButtons.Left))Invalidate();` - Note that transparency s all or nothing here, no soft edges. – TaW Aug 06 '20 at 10:13

0 Answers0