Search code examples
c#formsvisual-studio-2010visual-studiowindows

How to give a different color to my form text programatically


i would like to give the my username in red color in the centre of the my form. Please see my image. I already have my application name on the left hand side and my username in the centre.. I just need to make my username in red color

enter image description here

How can i give this??

Codes:-

    var someNewStr1 = new string(' ', 100);
    this.Text = "Publisher 1.0.0.0" + someNewStr1 + "USERNAME1";
    //i need the user name to be shown in red colour how can i do this

Solution

  • This is an extremely fiddly problem and can't be done out of the of the box.

    You'll either have to try and draw on the non-client area of the form and capture all the redraw messages, which I'm not sure will even work 100% (however it might).

    Or resort to even more extreme tactics and use a borderless form and redraw the title bar from and border from scratch.

    Additionally the second approach is prone to all sorts of issues with system settings and implementing the standard title bar features, i.e. you'll have to implement all the default windows styles and default behaviors.

    At this point you really do have to ask your self whether your coding time is going to be better spent in other areas, or with a slightly different solution.

    I.e. Why not put the client name in a status bar, or right aligned in a tool bar.

    However just for academic purposes, there is a Codeplex article that does show you all the hacks needed to achieve what you desire (there are too many and too much code for the purposes of this answer).

    Please note: while the below link is slightly off topic it does touch the areas you need to do both scenarios.

    Extending Form with Non-Client Area Painting

    I hope this convinces you, it's probably not worth your while.