Link to home
Start Free TrialLog in
Avatar of Aryo1
Aryo1Flag for Indonesia

asked on

Draw a Line over child control such PictureBox or Group Box using C#

Hi Experts,

How do I draw line over a control ?
Let say like this, I have a Windows Forms, and top of it there are GroupBox and on top of GroupBox there are PictureBox.
And how do I draw a line over GroupBox and PictureBox ?
Avatar of bungHoc
bungHoc
Flag of Viet Nam image

Try this:

System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Red);
 
System.Drawing.Graphics formGraphics;
formGraphics = this.CreateGraphics();
formGraphics.DrawLine(pen, 50, 50, 100, 100);
pen.Dispose();
formGraphics.Dispose();

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Aryo1

ASKER

@Idle_Mind

Thx, it seems your solutions can answers my problems.

BTW, Do you have suggestion where should I put this code ?
On Form_Paint or everytime there is change in UI (such as moving some control) ?
Well...a "catch all" approach would be to use the IMessageFilter() Interface and repaint the line whenever ANY control in your app gets a WM_PAINT message.  If your app is partially obscured though you could end up drawing a line over another apps window since we are drawing with the desktop DC.

Here is an example of IMessageFilter being used:
https://www.experts-exchange.com/questions/22696110/Move-label-with-cursor-location.html