Solved

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

Posted on 2007-11-28
5
2,828 Views
Last Modified: 2013-12-03
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 ?
0
Comment
Question by:Aryo1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 7

Expert Comment

by:bungHoc
ID: 20365004
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

0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20366222
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 20366927
To draw "above" multiple controls, get a DC to the Desktop Window and create a Graphics from that...

In VB.Net:
Public Class Form1
 
    Public Declare Function GetDesktopWindow Lib "user32" () As IntPtr
    Public Declare Function GetWindowDC Lib "user32" (ByVal handle As IntPtr) As IntPtr
    Public Declare Function ReleaseDC Lib "user32" Alias "ReleaseDC" (ByVal handle As IntPtr, ByVal hDc As IntPtr) As Integer
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim desktopHandle As IntPtr = GetDesktopWindow
        Dim desktopDC As IntPtr = GetWindowDC(desktopHandle)
        Dim g As Graphics = Graphics.FromHdc(desktopDC)
 
        Dim ptA As Point = Me.PointToScreen(New Point(0, 0))
        Dim ptB As Point = Me.PointToScreen(New Point(Me.ClientRectangle.Width, Me.ClientRectangle.Height))
        g.DrawLine(Pens.Black, ptA, ptB)
 
        g.Dispose()
        ReleaseDC(desktopDC, desktopHandle)
    End Sub
 
End Class

Open in new window

0
 

Author Comment

by:Aryo1
ID: 20378801
@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) ?
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 20379580
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:
http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_22696110.html
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question