473,466 Members | 1,511 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Draw over other controls

I would like to draw some text which will be in front of any other controls,
but without obscuring them completely. If I use DrawString in the form's
Paint handler the text is always behind, not in front of the other controls.
In VB6 I would have simply used a label control and set it to transparent,
but this doesn't work in .NET, the background of a label can be set to
'transparent', but this does not show the other controls through, instead it
fills in the background with the form background. I'm sure there must be a
simple way to acheive this, but I can't find it at the moment.
Any ideas appreciated.
Thanks
Phil.
(VB2005Express)
Sep 11 '07 #1
10 7505
Thanks, I'll give this a go.
Cheers,
Phil
Unfortunately because the form is TopMost, this means not only does it
display in front of the controls on my existing form, it also displays in
front of all other windows, including other applications that might be
running.
Also setting ShowInTaskbar = False stops an icon showing in the task bar,
but an icon for the form is still shown when pressing Alt+Tab to switch
windows.

Sep 12 '07 #2
Hi Phil,

Thank you for your prompt reply!

I work out another solution to your problem, i.e. draw the text to the
screen. To do this, we need to get the handle of the screen and then draw a
text to the screen.

I will illustrate my new solution with an example. It requires you to add
two Buttons on the form. When you click the Button1, a text "hello world"
appears on the screen. If the form is covered by another form and this form
is shown again, the text "hello world" previously drawn on the screen will
be erased. If you click the Button2, the text "hello world" is erased.

You can modify the sample code to accomplish what you want, if necessary.

The following is the sample code.

Public Class Form1
Declare Auto Function GetDC Lib "user32.dll" (ByVal hwnd As IntPtr) As
IntPtr
Declare Auto Function ReleaseDC Lib "user32.dll" (ByVal hwnd 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 hScreenDC As IntPtr = GetDC(IntPtr.Zero)
Dim g As Graphics = Graphics.FromHdc(hScreenDC)
Using (g)
Dim screenPt As Point = Me.PointToScreen(New Point(30, 50))
g.DrawString("hello world", Me.Font, Brushes.Thistle,
screenPt.X, screenPt.Y)
End Using

ReleaseDC(IntPtr.Zero, hScreenDC)
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Me.Refresh()
End Sub
End Class

Please try my new solution to see if it meets your requirement and let me
know the result.

Sincerely,
Linda Liu
Microsoft Online Community Support

Sep 13 '07 #3
Please try my new solution to see if it meets your requirement and let me
know the result.
Thanks, I tried it. It looks like this approach may be promising, but there
are still a couple of problems.
If I switch away from the form and back again, the text dissappears,
although I can get around this by placing the code in the Form Paint event.
Also any control that redraws itself (e.g. a button when you hover over with
the mouse) will draw itself over the text. Another problem is if there is
another window partly obscuring the form, the text can still get written
over the top of this other window. I wonder if it is possible to clip the
output of drawstring to the currently visible area of the form?

Thanks again for your help. It is very much appreciated.
Phil.
Sep 14 '07 #4
Hi Phil,

Thank you for your reply!
Also any control that redraws itself (e.g. a button when you hover over
with the mouse) will draw itself over the text.

We can solve this problem by handling the child control's Invalidated event
and draw the text on the screen again.
Another problem is if there is another window partly obscuring the form,
the text can still get written over the top of this other window.

I think we could redraw the text in the Activated event handler of the
form.

The following is the modified sample code.

Public Class Form1
Declare Auto Function GetDC Lib "user32.dll" (ByVal hwnd As IntPtr) As
IntPtr
Declare Auto Function ReleaseDC Lib "user32.dll" (ByVal hwnd As IntPtr,
ByVal hdc As IntPtr) As Integer

Private showFnt As Font
Private showText As String
Private showPt As Point
Private invalidateChildControls As New List(Of Control)
Private textRect As New Rectangle

Private Sub Form1_Activated(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Activated
If (showText <"") Then
DrawText(showText, showFnt, showPt)
End If
End Sub

Private Function GetChildControls(ByVal rect As Rectangle) As List(Of
Control)
Dim ctrls As New List(Of Control)
For Each ctrl As Control In Me.Controls
Dim result As Rectangle = Rectangle.Intersect(ctrl.Bounds, rect)
If (Not result.IsEmpty) Then
ctrls.Add(ctrl)
End If
Next
Return ctrls
End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
DrawText("hello world", Me.Font, New Point(30, 50))
End Sub

Private Sub DrawText(ByVal text As String, ByVal fnt As Font, ByVal pt
As Point)
Me.Refresh()
If (showText = "") Then
Dim textSize As Size = TextRenderer.MeasureText(text, fnt)
textRect.X = pt.X
textRect.Y = pt.Y
textRect.Width = textSize.Width
textRect.Height = textSize.Height
showText = text
showFnt = fnt
showPt = pt
invalidateChildControls = GetChildControls(textRect)
For Each ctrl As Control In invalidateChildControls
AddHandler ctrl.Invalidated, AddressOf
ChildControlInvalidated
Next
End If

Dim hScreenDC As IntPtr = GetDC(IntPtr.Zero)
Dim g As Graphics = Graphics.FromHdc(hScreenDC)
Using (g)
Dim screenPt As Point = Me.PointToScreen(pt)
g.DrawString(text, Me.Font, Brushes.Red, screenPt.X, screenPt.Y)
End Using

ReleaseDC(IntPtr.Zero, hScreenDC)
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
showText = ""
For Each ctrl As Control In invalidateChildControls
RemoveHandler ctrl.Invalidated, AddressOf
ChildControlInvalidated
Next
invalidateChildControls.Clear()
Me.Refresh()
End Sub

Private Sub ChildControlInvalidated(ByVal sender As Object, ByVal e As
System.Windows.Forms.InvalidateEventArgs)
If (showText <"") Then
DrawText(showText, showFnt, showPt)
End If
End Sub
End Class

Hope it is what you want.
Sincerely,
Linda Liu
Microsoft Online Community Support

Sep 14 '07 #5
>Also any control that redraws itself (e.g. a button when you hover over
with the mouse) will draw itself over the text.

We can solve this problem by handling the child control's Invalidated
event
and draw the text on the screen again.
I had thought of that, but didn't want to have to add a separate handler for
every child control on the form. I see your code identifies all the child
controls at runtime, so that gets around this problem.
>
>Another problem is if there is another window partly obscuring the form,
the text can still get written over the top of this other window.

I think we could redraw the text in the Activated event handler of the
form.
I had thought of that, but a form can become visible without becoming
activated. e.g. if another window is moved across it. This is why I put the
code into the Paint event, but then, it can still draw over the other
window, if the form is still partially covered.

I'll try your new code, and see how it works.

Thanks,
Phil.
Sep 17 '07 #6
Hi Phil,

How about the problem now?

If there's any question, please feel free to let me know.

Thank you for using our MSDN Managed Newsgroup Support Service!

Sincerely,
Linda Liu
Microsoft Online Community Support

Sep 19 '07 #7
Hi Phil,
>
How about the problem now?
Hello,

Sorry for not getting back to you, been busy with other stuff.
I did try your updated version, and it is better, but as I suspected it does
still suffer from the problem with the text not showing when the form is
visible but inactive. I've got a couple of different uses for this, it'll
probably be OK for one of them, and there may be another way around it for
the other application.
I did think I might have a go at building this into a custom
TransparentLabel control which I could use for both but that may have to
wait until I have a bit more time.
I did have one idea: As you are now identifying all the controls that will
be overwritten, I wonder if it might be better to get a graphics object for
each of these and write to that rather than writing to the screen. This
could be done in the paint event, without the danger of overwriting other
windows. I haven't had chance to try this out yet though.

Cheers,
Phil.
Sep 19 '07 #8
Hi Phil,

Thank you for your reply!
I wonder if it might be better to get a graphics object for each of these
and write to that rather than writing to the screen.

If the text is going to be painted on one child control fully, you can get
the graphics object for this child control and draw the text on this
control.

However, if the text couldn't be painted on one control fully, e.g. the
text is long and would be painted in an area that covers several child
controrls, it's difficult to draw the text using the graphics object of
each related child control, because each graphics object should only paint
a part of the text and the part is difficult to determine.
Sincerely,
Linda Liu
Microsoft Online Community Support

Sep 21 '07 #9
>I wonder if it might be better to get a graphics object for each of these
and write to that rather than writing to the screen.

If the text is going to be painted on one child control fully, you can get
the graphics object for this child control and draw the text on this
control.
Yes, that will be the case for one particular application I have in mind,
but in another case, my text will likely cover a number of other controls,
>
However, if the text couldn't be painted on one control fully, e.g. the
text is long and would be painted in an area that covers several child
controrls, it's difficult to draw the text using the graphics object of
each related child control, because each graphics object should only paint
a part of the text and the part is difficult to determine.
I haven't tried this yet, but presumably I could just paint the text to all
of them (and to the form). Presumably anything I try to paint outside of the
control's visible rectangle, would not be shown anyway, so this could work.
Sep 21 '07 #10
Hi Phil,

I am reviewing this post and would like to know the status of this issue.

If you have any question, please feel free to let me know.

Thank you for using our MSDN Managed Newsgroup Support Service!

Sincerely,
Linda Liu
Microsoft Online Community Support

Sep 26 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: °Ë´óɽÈË | last post by:
Hi, I create a graphics object for desktop window by following step: IntPtr hWnd = GetDesktopWindow(); // API Graphics g = Graphics.FromHWnd(hWnd); then draw the screen by g, but I can't draw...
1
by: Crirus | last post by:
I know subject is confusing... This is the ideea: I have 3 controls..custom. I have a control that act as a container for them.. does 3 containers fill completly the container one... That...
3
by: Colin McGuire | last post by:
Hi there. I have written a small procedure to draw various shapes on things. A bit of it is shown below. Private Sub drawShape(ByVal shapeType As Integer, ByRef g As Graphics) Select Case...
2
by: John | last post by:
I created a number of pictureboxes in a panel, and want to draw lines in those pictureboxes but I cannot. Please see the following code and make corrections. Thanks. Private Sub...
9
by: Duncan Barnes-Ceeney | last post by:
I’m having problems with a custom Combo box. The main problem is that I want to modify the look of the combo which includes the size of the button. To do this I have inherited from the standard...
1
by: Jeff Waskiewicz | last post by:
Hello All, I'm trying to solve a nagging problem. The goal is to draw a rectangle over the top of all the other controls on a form. Specifically, over a ChartFX control. The user would draw...
8
by: George | last post by:
Hello everyone, I am using C# on a Pocket PC 2003 project based on .Net Compact Framework of Visual Studio 2005. I want to re-draw some controls of a Form (Window) at a regular interval (for...
9
by: zhaow | last post by:
Hi, All Greetings! I want to develop as appllication that requires a line-drawing function in the blank area between two forms. I have looked up the MSDN, it says that a graphics object need a...
3
by: Brandon Arnold | last post by:
I have a panel that contains a label and a picturebox. When clicked, I want to draw a rectangle over everything. I can draw the rect and set the color, no big deal. The problem is because I have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.