Pergunta

I need to a draw common dotted focus rectangle.

I would like to know if there is an equivalent to

Public Declare Function DrawFocusRect Lib "user32" (ByVal hdc As Long, lpRect As RECT) As Long

in the GDI+ classes / System.Drawing of VB.NET that is managed and that will accomplish the same.

Thank you!

I need to edit my post: For some reason, ControlPaint.DrawFocusRectangle does not persist on a bitmap.

I would therefore like to ask if there is a different method that persists on a bitmap as well.

Foi útil?

Solução

The ControlPaint class has many useful tools for painting common control-related graphical elements. It includes a DrawFocusRectangle method, which should do what you are looking for. From the MSDN:

Draws a focus rectangle on the specified graphics surface and within the specified bounds.
...
A focus rectangle is a dotted rectangle that Windows uses to indicate what control has the current keyboard focus.

It's a Shared method, so you can call it without creating an instance of the ControlPaint class, for instance:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    ControlPaint.DrawFocusRectangle(Button1.CreateGraphics(), Button1.ClientRectangle)
End Sub

There is also an overload to the method which allows you to specify the foreground and background color.

It's in the System.Windows.Forms namespace, defined by the System.Windows.Forms.dll library, so it's not technically part of the System.Drawing library, like you requested, but you should be able to reference and use that library even outside of a WinForm application, if necessary.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top