Christian
Christian

Reputation: 4375

drawing a string on the screen C#

I would like to simply draw a string (if possible in a specific font and size) on the screen (at a specific location). I am within a C# windows forms application. Unfortunately, I could not found any hint on how to do this in the web.

Please help!

Christian

Upvotes: 3

Views: 8557

Answers (3)

Mikael
Mikael

Reputation: 453

doing what you are asking for is not really recommended, see e.g. Link

If you really want to do something like this; here is a creepy way to do it:

    [DllImport("User32.dll")]
    public static extern IntPtr GetDC(IntPtr hwnd);

    [DllImport("User32.dll")]
    public static extern void ReleaseDC(IntPtr dc);

    protected override void OnPaint(PaintEventArgs e)
    {
        IntPtr desktopDC = GetDC(IntPtr.Zero);

        Graphics g = Graphics.FromHdc(desktopDC);

        g.DrawString("Test", new Font(FontFamily.GenericSerif, 12), Brushes.Blue, 300, 300);
        g.Dispose();

        ReleaseDC(desktopDC);
    }

Please note that I DON'T recommend anyone doing this as I don't think applications should be doing stuff like this. If you want to draw something you should do it on your own form/controls.

Upvotes: 3

priyanka.sarkar
priyanka.sarkar

Reputation: 26538

Check this out.

Or may be you are looking for DrawString method

Hope this will help

Upvotes: 0

Daniel Mošmondor
Daniel Mošmondor

Reputation: 19986

To draw a string outside of your window, you'll have to CREATE a new window, set it's mask to some color (say magenta) and then draw text onto it - you can use simple label here.

Set your window border style to None, and there you go.

In other words, there is no way of displaying 'free text' without window attached.

For masking color, use 'transparency color' or similar property (I will look up into it later - have no VS at hand)

Upvotes: 3

Related Questions