473,767 Members | 2,284 Online
Bytes | Software Development & Data Engineering Community
+ 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 7531
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(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
Dim hScreenDC As IntPtr = GetDC(IntPtr.Ze ro)
Dim g As Graphics = Graphics.FromHd c(hScreenDC)
Using (g)
Dim screenPt As Point = Me.PointToScree n(New Point(30, 50))
g.DrawString("h ello world", Me.Font, Brushes.Thistle ,
screenPt.X, screenPt.Y)
End Using

ReleaseDC(IntPt r.Zero, hScreenDC)
End Sub

Private Sub Button2_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) 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 invalidateChild Controls As New List(Of Control)
Private textRect As New Rectangle

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

Private Function GetChildControl s(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.Inter sect(ctrl.Bound s, rect)
If (Not result.IsEmpty) Then
ctrls.Add(ctrl)
End If
Next
Return ctrls
End Function

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) 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.Me asureText(text, fnt)
textRect.X = pt.X
textRect.Y = pt.Y
textRect.Width = textSize.Width
textRect.Height = textSize.Height
showText = text
showFnt = fnt
showPt = pt
invalidateChild Controls = GetChildControl s(textRect)
For Each ctrl As Control In invalidateChild Controls
AddHandler ctrl.Invalidate d, AddressOf
ChildControlInv alidated
Next
End If

Dim hScreenDC As IntPtr = GetDC(IntPtr.Ze ro)
Dim g As Graphics = Graphics.FromHd c(hScreenDC)
Using (g)
Dim screenPt As Point = Me.PointToScree n(pt)
g.DrawString(te xt, Me.Font, Brushes.Red, screenPt.X, screenPt.Y)
End Using

ReleaseDC(IntPt r.Zero, hScreenDC)
End Sub

Private Sub Button2_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button2.Click
showText = ""
For Each ctrl As Control In invalidateChild Controls
RemoveHandler ctrl.Invalidate d, AddressOf
ChildControlInv alidated
Next
invalidateChild Controls.Clear( )
Me.Refresh()
End Sub

Private Sub ChildControlInv alidated(ByVal sender As Object, ByVal e As
System.Windows. Forms.Invalidat eEventArgs)
If (showText <"") Then
DrawText(showTe xt, 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
TransparentLabe l 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

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

Similar topics

3
14734
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 anything. WHY?? And I try to draw by DC (get by GetDC), it's fail either.
1
1122
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 container control is the one I use in my form Anyway, I draw some fancy borders on the parent control (the container). Question: Is there any chance to draw on a upper layer that is visible over does controls?
3
10327
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 shapeType Case 1 : g.DrawRectangle(New Pen(Color.Black), 0, 0, 50, 10) Case 2 'draw a circle Case 3 'draw a triangle Case 4 'draw other shape Case 5 'draw other shape
2
3058
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 Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Panel1.AutoScroll = True Panel1.Width = 200 Panel1.Height = 200
9
6174
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 combo set it as a UserPaint and provided my own paint. This along with the standard Owner Draw for the list items works fine for the DropDownList version, the problems start with the versions with the edit box which are where the problems are. ...
1
3776
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 the rectangle using the right mouse button to represent the area of the chart they want to zoom on. I haev been able to draw the rectangle on a blank form but I cannot get it to draw on top of other controls. I have pasted in the code i am using ....
8
2772
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 example, change the title of some Label or something similar). The issues I met with are, 1. My application has several Forms/Windows. How to check whether the specific Form/Window (which I want to re-draw) is active? If the Form/Window
9
4027
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 reference to a control or a form. I guess it means that lines can't be draw on blank area between two forms. Can anybody guarantee this for me? Is there any method can realize this function? I mainly want to draw a line from a button in form1 to...
3
5084
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 pass the rect of the panel, it draws on that, and under the other controls. How can I force it to draw OVER everything else? Ideally this would also be trackable since the whole thing(panel and children) will be dragable. This is what i've got...
0
9404
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10168
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10009
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9838
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7381
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3929
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.