绘制其他控件 [英] Draw over other controls

查看:70
本文介绍了绘制其他控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制一些文本,这些文本将出现在任何其他控件之前,

但是没有完全掩盖它们。如果我在'

形式的处理程序中使用DrawString,文本总是在后面,而不是在其他控件的前面。

在VB6中我会简单地使用标签控件并将其设置为透明,

但这在.NET中不起作用,标签的背景可以设置为

''透明'',但是这并没有显示其他控件,而是它使用表单背景填充后台的
。我肯定必须有一个简单的方法来实现这个目标,但我现在找不到它。

任何想法都会受到赞赏。

谢谢

菲尔。

(VB2005Express)

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)

推荐答案


谢谢,我会放手一搏。

干杯,

Phil
Thanks, I''ll give this a go.
Cheers,
Phil



不幸的是因为表格是TopMost,这意味着它不仅会在现有表格的控件前显示
,还会显示在

所有其他窗口的前面,包括可能运行的其他应用程序。

另外设置ShowInTaskbar = False会停止在任务栏中显示的图标,

但是当按Alt + Tab切换

windows时,仍会显示表格的图标。

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.


Hi Phil,


感谢您的及时回复!


我找到了另一个解决方案你的问题,即将文字绘制到

屏幕。要做到这一点,我们需要获取屏幕的句柄,然后在屏幕上绘制一个

文本。


我将用一个来说明我的新解决方案例。它要求你在表单上添加两个按钮。当您单击Button1时,屏幕上会出现文本hello world

。如果表格被另一个表格覆盖,并且此表格再次显示,则表示hello world字样。之前在屏幕上绘制的将被删除。如果单击Button2,则显示文本hello world。擦除。


如果需要,您可以修改示例代码以完成您想要的操作。


以下是示例代码。


公共类Form1

声明自动功能GetDC Lib" user32.dll" (ByVal hwnd As IntPtr)As

IntPtr

声明自动功能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)处理Button1.Click

Dim hScreenDC As IntPtr = GetDC(IntPtr.Zero)

Dim g As Graphics = Graphics.FromHdc(hScreenDC )

使用(g)

Dim screenPt As Point = Me.PointToScreen(New Point(30,50))

g.DrawString( hello world,Me.Font,Brushes.Thistle,

screenPt.X,screenPt.Y)

结束使用


ReleaseDC(IntPtr.Zero,hScreenDC)

End Sub


Private Sub Button2_Click(ByVal sender As System.Object,ByVal e As

System.EventArgs)处理Button2.Click

Me.Refresh()

End Sub

结束班


请尝试我的新解决方案,看它是否符合您的要求并让我

知道结果。


此致,

Linda Liu

Micros在线社区支持

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



请尝试我的新解决方案,看它是否符合您的要求,让我

知道结果。
Please try my new solution to see if it meets your requirement and let me
know the result.



谢谢,我试过了。看起来这种方法可能很有前途,但是还有一些问题仍然存在。

如果我从表单上再切换回来,文本会消失,

虽然我可以通过将代码放在Form Paint事件中来解决这个问题。

还有任何重绘自己的控件(例如当你将鼠标悬停在
上时的按钮)
鼠标)将在文本上绘制自己。另一个问题是如果有另外一个窗口部分模糊了窗体,那么文本仍然可以在另一个窗口的顶部写入

。我想知道是否有可能将抽绳的

输出剪辑到表格的当前可见区域?


再次感谢您的帮助。非常感谢。

Phil。

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.


这篇关于绘制其他控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆