Click here to Skip to main content
Click here to Skip to main content

Extended Use of .NET RichControl Class - C# CLabel 1.0beta

By , 20 Sep 2000
 
  • Download demo executable and project - 12 Kb (v1.0beta)
  • Download CLabel source - 4 Kb (v1.0beta)
  • Ever wanted a static control that behaved like a Visual Basic label control? Well this could be just the thing you might be looking for. This control was first posted as an CStatic derived class in my article Extended Use of the CStatic Class. The code in that article has been rewritten in C# using the .NET framework

    See the original article for the programming API. The main differences are that all COLORREF parameters are now Color's, CString's become String's, and BOOL's become bool's.

    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here

    About the Author

    Norm .net
    Software Developer (Senior) Software Kinetics
    United Kingdom United Kingdom
    Member



    Software Kinetics
    are experts in developing customised and bespoke applications and have expertise in the development of desktop, mobile and internet applications on Windows.

    We specialise in:

    • User Interface Design
    • Desktop Development
    • Windows Phone Development
    • Windows Presentation Framework
    • Windows Forms
    • Windows Communication Framework
    • Windows Services
    • Network Applications
    • Database Applications
    • Web Development
    • Web Services
    • Silverlight
    • ASP.net
     
    Visit Software Kinetics

    Sign Up to vote   Poor Excellent
    Add a reason or comment to your vote: x
    Votes of 3 or less require a comment

    Comments and Discussions

     
    You must Sign In to use this message board.
    Search this forum  
        Spacing  Noise  Layout  Per page   
    GeneralMy vote of 1memberSheetal_Joshi8 Dec '09 - 6:04 
    Please stop posting such articles, I am not able to see any content at all
    GeneralMy vote of 1memberRyleigh27 Mar '09 - 7:38 
    Unclear, confusing, badly written. Overall a waste of everyones time
    GeneralRe: My vote of 1groupNorm .net20 May '09 - 4:47 
    Well, you're wasting ours... Do we we complain?
     

    Generalvertically input text controlmemberjirigala29 Nov '02 - 21:53 
    I want a control ,that can vertically input text.
    how can help me,my name is jirigala, i am from China,
    my home page
     
    www.mengguren.com
    GeneralA bout Unicode in C# Controlmemberbvhhoang15 Sep '02 - 17:35 
    I'm writing a project, I want to inherit C#'s controls, but my control can display Unicode charater (C#' original control seem cannot). I just begin, can any body help me find out some usefull code.
    Thanks very much.
     
    YEAH !!!!!!!!11
    Generalbad coding stylesussMike20 Oct '00 - 3:51 
    1. Imported Win32 API calls - you must use .net api.
    2. Hungarian notation - read .net sdk documentation. You should use different style of code. Microsoft provide lot recomendations for naming.
    3. lot of Set* functions -> In C# you must use properties.
    4. .net API has systembrushes. It's bad style to create your own
    5. and _many_ other defects.
    final result: 1.0
     
    p.s. sorry for english, my C++ language is much better Smile | :)

    GeneralOk whos the jokersussSilly boy20 Oct '00 - 4:06 
    Teh hee I never laughed so much.
    Ur engleesh ist very bad considiring you r called falconrulez from u s a and u sine ur nam Mike.
     
    Mai be u write better code then, hey!

    GeneralMybe this is bettersussNorm Almond20 Oct '00 - 4:20 

    /// Author: Norm Almond
    // Class: cLabel
    // Version 1.00
    // 20 September 2000
    // Notes:
    // First attempt at Evaluating and using C# and the .NET frameworks
    //
    //------------------------------------------------------------------------------
    namespace Microsoft.Samples.WinForms.Cs.CLabel {
    using System;
    using System.ComponentModel;
    using System.ComponentModel.Design;
    using System.WinForms;
    using System.WinForms.Design;
    using System.Drawing;
    using System.Drawing.Design;
    using System.Drawing.Drawing2D;
    using System.IO;
    using System.Timers;


    //------------------------------------------------------------------------------
    /// Function: CLabel
    //
    // Description: Constructor
    //
    // Notes:
    //
    // Maintenance:
    // Name Date Version Notes
    // NT ALMOND 2009200 1.0beta Origin
    //------------------------------------------------------------------------------
    public class CLabel : RichControl {

    // Imported Win32 API calls
    [sysimport(dll="gdi32.dll")]
    public static extern int GetStockObject(int n);
     
    [sysimport(dll="shell32.dll")]
    public static extern int ShellExecuteA(int hwnd, String lpOperation, String lpFile, String lpParameters, String lpDirectory, int nShowCmd);
     
    // Constants (#defines)
    public const int WM_SYSCOLORCHANGE = 0x0015;
    public const int DEFAULT_GUI_FONT = 17;
    public const int SW_SHOWNORMAL = 1;

    // Enumerators
    public enum FlashType {None, Text, Background };
    public enum Type3D { Raised, Sunken};
    public enum BackFillMode { Normal, Gradient };
    public enum TextMode { Left, Center, Right, CenterWrap, LeftWrap};
    public enum BorderStyle { None, Frame, Raised, Sunken};
    public enum Style3D { None, Raised, Sunken};
     
    // Member Variables
    protected TextMode m_alignment = TextMode.Left;
    protected FontStyle m_fontstyle;
    protected BorderStyle m_border;
    protected Style3D m_font3dstyle;
    protected FlashType m_Type;
    protected Type3D m_3dType;
    protected BackFillMode m_fillmode;
    protected Color m_crText;
    protected Color m_cr3DHiliteColor;
    protected Color m_crHiColor;
    protected Color m_crLoColor;
    protected SolidBrush m_hwndBrush;
    protected SolidBrush m_hBackBrush;
    protected Font m_font;
    protected bool m_bState;
    protected bool m_bLink;
    protected bool m_bTransparent;
    protected bool m_bFont3d;
    protected bool m_bToolTips;
    protected bool m_bRotation;
    protected bool m_bBKColorOverride;
    protected System.Timers.Timer m_timer;

    // Static varible to give labal a new text string when its instantiated
    public static int nCount = 0;

    public CLabel()
    {

    m_bTransparent = false;

    m_crText = SystemColors.WindowText;

    m_bState = false;
    m_bTransparent = false;
    m_bLink = true;
    m_Type = FlashType.None;
    m_bFont3d = false;
    m_bToolTips = false;
    m_bRotation = false;
    m_fillmode = BackFillMode.Normal;
    m_cr3DHiliteColor = Color.White;
    m_border = BorderStyle.None;
    m_font3dstyle = Style3D.None;

    SetStyle(ControlStyles.Opaque, false);
    SetStyle(ControlStyles.ResizeRedraw, true);


    // Auto label generation
    Text = String.Format("Label{0}",++nCount);

    m_hwndBrush = new SolidBrush(SystemColors.Control);

    int nFont = GetStockObject(DEFAULT_GUI_FONT); // Default GUI Font
    m_font = Font.FromHFONT(nFont);

    }

    //------------------------------------------------------------------------------
    /// Function: ~CLabel
    //
    // Description: Destructor
    //
    // Notes:
    //
    // Maintenance:
    // Name Date Version Notes
    // NT ALMOND 2009200 1.0beta Origin
    //------------------------------------------------------------------------------
    ~CLabel()
    {
    }

    //------------------------------------------------------------------------------
    /// Function: SetHyperLink
    //
    // Description: Enables 'hyperlink' mode
    //
    // Notes:
    //
    // Maintenance:
    // Name Date Version Notes
    // NT ALMOND 2009200 1.0beta Origin
    //------------------------------------------------------------------------------
    [
    Category("Label"),
    Description("Enable Hyperlink Mode"),
    Browsable(true),
    ]
    public bool HyperLink {
    get {
    return Cursor != Cursors.Hand;
    }
    set {
    if (value == true)
    Cursor = Cursors.Hand;
    else
    Cursor = Cursors.Arrow;

    }
    }

    //------------------------------------------------------------------------------
    /// Function: SetTextColor
    //
    // Description: Sets Text Color
    //
    // Notes:
    //
    // Maintenance:
    // Name Date Version Notes
    // NT ALMOND 2009200 1.0beta Origin
    //------------------------------------------------------------------------------
    [
    Category("Label"),
    Description("Sets the text color"),
    DefaultValue(0),
    Browsable(true),
    ]
    public Color TextColor {
    get {
    return m_crText;
    }
    set {
    m_crText = value;
    Invalidate();
    Update();
    }
    }
     
    public bool ShouldPersistTextColor() {
    return !(m_crText == SystemColors.WindowText);
    }
     

     
    //------------------------------------------------------------------------------
    /// Function: SetFont3DStyle
    //
    // Description: Sets the font 3D style
    //
    // Notes:
    //
    // Maintenance:
    // Name Date Version Notes
    // NT ALMOND 2009200 1.0beta Origin
    //------------------------------------------------------------------------------
    [
    Category("Label"),
    Description("Font 3D Style"),
    DefaultValue(0),
    Browsable(true),
    ]
    public Style3D Font3DStyle
    {
    get {
    return m_font3dstyle;
    }
    set {
    m_font3dstyle = value;
    Invalidate();
    Update();
    }
    }
     
    //------------------------------------------------------------------------------
    /// Function: SetBorderStyle
    //
    // Description: Sets the border style
    //
    // Notes:
    //
    // Maintenance:
    // Name Date Version Notes
    // NT ALMOND 2009200 1.0beta Origin
    //------------------------------------------------------------------------------
    [
    Category("Label"),
    Description("Border Style"),
    DefaultValue(0),
    Browsable(true),
    ]
    public BorderStyle LabelBorderStyle
    {
    get {
    return m_border;
    }
    set {
    m_border = value;
    Invalidate();
    Update();
    }
    }
     
    //------------------------------------------------------------------------------
    /// Function: SetFontItalic
    //
    // Description: Sets the font italic style
    //
    // Notes:
    //
    // Maintenance:
    // Name Date Version Notes
    // NT ALMOND 2009200 1.0beta Origin
    //------------------------------------------------------------------------------
    [
    Category("Label"),
    Description("Italics"),
    DefaultValue(false),
    Browsable(true),
    ]
    public bool FontItalic {
    get {
    return m_fontstyle == FontStyle.Italic;
    }
    set {
    if (value == true)
    m_fontstyle |= FontStyle.Italic; else
    m_fontstyle &= ~FontStyle.Italic;

    m_font = new Font(m_font.Name,m_font.Size,m_fontstyle);
    Invalidate();
    Update();

    }
    }
     
    //------------------------------------------------------------------------------
    /// Function: SetFontBold
    //
    // Description: Sets the font bold style
    //
    //
    GeneralRe: Mybe this is bettersussmike26 Oct '00 - 21:56 
    hi. Yeah, that is much better Smile | :) ... but there some improvements.
     
    1. I think _all_ font properties and variables should be removed, because RichControl has Font property.
    (VS Designer can modify property with type Font)
    2. m_crColor & backcolor vars & properties should be removed - use ForeColor & BackColor from RichControl
    3. Remove GetStockObject - RichControl have DefaultFont property.
    4. m_hwndBrush = new SolidBrush(SystemColors.Control);
    -> must be changed to SystemBrushes.Control;
    5. BrowsableAttribute is true by default - don't need to publish that every time... Wink | ;-)
    6. in all cases "new Pen(SystemColors.xxx)" should be replaced with "SystemPens.xxx"
    7. from Paint:
    switch (m_border)
    {
    [skip]
    }
    ... look in2 msdn for ControlPaint class (it can be found by text search)
    8. I don't like winapi in net. Replace ShellExecute, please Smile | :)
     
    //mike

    GeneralRe: Mybe this is bettermemberPaulo17 Mar '02 - 12:59 
    Hey Mike, what about doing at least one useful thing in your life like posting an article yourself, heh? That way we all can see how beautiful is your coding style (sigh) so we can have a little laughs here Laugh | :laugh:

    GeneralRe: bad coding stylesussMelissa20 Oct '00 - 8:17 
    Hi Mike,
     
    I am work on a project to building a blak box trading system
    for a 400 person securities trading firm.
    Do you know of any one who would like to work on this project.
    Let me know.
    Thanks.
    GeneralRe: bad coding stylememberJon Rista20 Dec '01 - 14:57 
    I have to agree with mike, this is pretty horrid coding style. Why use C# when your going to import the Win32 API and use that? That pretty much defeats the purpose of .Net and C#.
     
    While its nice to see that its possible to easily convert an MFC program to C#, I think this could have been done far better, by actually USING .Net and all that C# has to offer. Its no wonder this has such a low rating.
     
    If you want to improove ratings, try making this a REAL .Net project and really use all that C# has to offer.
    GeneralRe: bad coding stylememberNormski6 Aug '03 - 20:55 
    Considering this was done when .net was still beta one, i'd suggest you keep those type of comments to yourself.
     
    BTW: this article are been superceded with the *built in* functionality offered by the .net framework.

    GeneralExcellent ArticlesussRanjeet Chakraborty27 Sep '00 - 23:31 
    I found the article to be really excellent. Not only provides some sample code on C# but also shows WinForms in action, and most importantly as the author pointed out, he found porting the code MFC to C#/.NET was simple, gives a relief Smile | :)
    At the point where .NET is now such code deserves a 4+ definitely
    GeneralRe: Excellent ArticlesussNorm Almond28 Sep '00 - 3:03 
    Thanks Ranjeet
    I will be posting revised sourcecode and a article to follow. I also will be posting ports from other MFC projects, so watch this section!

    GeneralRe: Excellent ArticlesussPeter28 Sep '00 - 9:19 
    I thought it was good too - but I REALLY want to see a step by step on how you did it!
    GeneralGetting started with C#sussPhil Harding22 Sep '00 - 3:31 
    Hi, can any of you good gentlemen point me in the right direction (in terms of what to get/download from where) to get started using C#/.NET
     
    Thanks
    Phil Harding
    GeneralRe: Getting started with C#sussNorm Almond22 Sep '00 - 4:00 
    Phil
    Try this link for the "NET Framework SDK Technology Preview" but beware its rather a large download 86Mb or 9x9.5mb chunks:
     
    http://msdn.microsoft.com/downloads/default.asp
    GeneralRe: Getting started with C#sussRyan Schneider26 Sep '00 - 5:56 
    Everything I've heard also warns against installing the preview on a "production" development machine. Has anyone installed it on a machine that they use for day-to-day VC development? Any problems?
     
    The things I have to have work:
     
    -VC++
    -Current Platform SDK and Win2k SP1 DDK
    -Current MFC
     
    The fact that I might easily break something has inclined me not to install the preview. Are these warnings I see more to cover MSFT's butt, or should they really be listened to?
     
    Thanks,
     
    Ryan Schneider
    NeoWorx Inc
    GeneralRe: Getting started with C#sussNorm Almond27 Sep '00 - 4:22 
    Ryan
    I first installed the .NET preview on my "test" machine, I so no problems for about a week, so I installed on my live machine, I been using it for around 2 weeks and have had no problems as of yet - fingers x'ed.
     
    BTW: I'm running VC++, Platform SDK etc.
     
    Norm
    QuestionLow rating?sussChris Maunder21 Sep '00 - 11:55 
    I'd have to say I'm a little disappointed at the low rating. Is this a reflection on the brevity of the article, the code itself, or the fact that it's C# instead of C++?
     
    I was ecstatic to get the first C++ -> C# conversion project, and I hope other authors follow suit.
     
    cheers,
    Chris Maunde
    AnswerRe: Low rating?sussAlex21 Sep '00 - 15:44 
    I guess it has to reflect C# as the original gets 4+/5 :
    AnswerRe: Low rating?sussNorm Almond22 Sep '00 - 0:15 
    Chris
     
    Maybe a if I wrote a step by step guide on how the code was ported, this would improve the ratings Smile | :)
     
    I thought by submitting the first article, would spur other programmers into doing the same.
     

    GeneralRe: Low rating?sussAlex22 Sep '00 - 13:54 
    That would be good. BTW which did you prefer as a finished product?
     
    Kind regards
     
    Al.
     
    ATL, whomever that is, all the way
    QuestionWell, what did you think?sussAlvaro Mendez21 Sep '00 - 8:21 
    So what did you think about writing this code in C# using .NET? Can you describe how it compares to writing it in C++ with MFC?
     
    Thanks,
    Alvar
    AnswerRe: Well, what did you think?sussNorm Almond21 Sep '00 - 23:52 
    I'll write some comparisions, but the basic outcome was that porting from MFC/C++ to .NET Framework/C# was a pretty simple exercise. After posting the code I am at the moment refining the code
    GeneralMissing filessussGisle Vanem20 Sep '00 - 22:15 
    Your Makefile references file
    $(QSDIR)\WinForms\samples\master.mak.
     
    This was not part of your CLabel_*.zip.
    Where is it
    GeneralRe: Missing filessussJohn M. Drescher22 Sep '00 - 14:12 
    I thought the same myself... You got to set CORESDK to the folder that you installed NGWSSDK.
     
    Here is the part of the makefile to change:
     
    !IFNDEF CORSDK
    #assumes we are in com20sdk\quickstart\winforms\samples\Cs\clabel
    CORSDK=N:\
    !ENDIF
     
    I used the subst command to assign drive N: to I:\Program Files\NGWSSDK because the makefile had problems with the space in "Program Files". Here is the subst command I used on win2k:
     
    subst n: "I:\Program Files\NGWSSDK"
     

    John

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    Permalink | Advertise | Privacy | Mobile
    Web04 | 2.6.130523.1 | Last Updated 21 Sep 2000
    Article Copyright 2000 by Norm .net
    Everything else Copyright © CodeProject, 1999-2013
    Terms of Use
    Layout: fixed | fluid