Click here to Skip to main content
12,078,370 members (47,692 online)
Click here to Skip to main content
Add your own
alternative version

Stats

208.8K views
196 downloads
23 bookmarked
Posted

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

, 20 Sep 2000
Rate this:
Please Sign up or sign in to vote.
A fully stocked owner drawn RichControl class
<!-- Article Starts -->
  • Download demo executable and project - 12 Kb (v1.0beta)
  • Download CLabel source - 4 Kb (v1.0beta)
  • <!-- STEP 3. Add the article text.-->

    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

    Share

    About the Author

    NormDroid
    Software Developer (Senior) Software Kinetics
    United Kingdom United Kingdom



    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

    You may also be interested in...

    Comments and Discussions

     
    GeneralMy vote of 1 Pin
    Sheetal_Joshi8-Dec-09 7:04
    memberSheetal_Joshi8-Dec-09 7:04 
    GeneralMy vote of 1 Pin
    Ryleigh27-Mar-09 8:38
    memberRyleigh27-Mar-09 8:38 
    GeneralRe: My vote of 1 Pin
    Norm .net20-May-09 5:47
    groupNorm .net20-May-09 5:47 
    Generalvertically input text control Pin
    jirigala29-Nov-02 22:53
    memberjirigala29-Nov-02 22:53 
    GeneralA bout Unicode in C# Control Pin
    bvhhoang15-Sep-02 18:35
    memberbvhhoang15-Sep-02 18:35 
    Generalbad coding style Pin
    Mike20-Oct-00 4:51
    sussMike20-Oct-00 4:51 
    GeneralOk whos the joker Pin
    Silly boy20-Oct-00 5:06
    sussSilly boy20-Oct-00 5:06 
    GeneralMybe this is better Pin
    Norm Almond20-Oct-00 5:20
    sussNorm Almond20-Oct-00 5: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 better Pin
    mike26-Oct-00 22:56
    sussmike26-Oct-00 22:56 
    GeneralRe: Mybe this is better Pin
    Paulo17-Mar-02 13:59
    memberPaulo17-Mar-02 13:59 
    GeneralRe: bad coding style Pin
    Melissa20-Oct-00 9:17
    sussMelissa20-Oct-00 9:17 
    GeneralRe: bad coding style Pin
    Jon Rista20-Dec-01 15:57
    memberJon Rista20-Dec-01 15:57 
    GeneralRe: bad coding style Pin
    Normski6-Aug-03 21:55
    memberNormski6-Aug-03 21:55 
    GeneralExcellent Article Pin
    Ranjeet Chakraborty28-Sep-00 0:31
    sussRanjeet Chakraborty28-Sep-00 0:31 
    GeneralRe: Excellent Article Pin
    Norm Almond28-Sep-00 4:03
    sussNorm Almond28-Sep-00 4:03 
    GeneralRe: Excellent Article Pin
    Peter28-Sep-00 10:19
    sussPeter28-Sep-00 10:19 
    GeneralGetting started with C# Pin
    Phil Harding22-Sep-00 4:31
    sussPhil Harding22-Sep-00 4:31 
    GeneralRe: Getting started with C# Pin
    Norm Almond22-Sep-00 5:00
    sussNorm Almond22-Sep-00 5:00 
    GeneralRe: Getting started with C# Pin
    Ryan Schneider26-Sep-00 6:56
    sussRyan Schneider26-Sep-00 6:56 
    GeneralRe: Getting started with C# Pin
    Norm Almond27-Sep-00 5:22
    sussNorm Almond27-Sep-00 5:22 
    QuestionLow rating? Pin
    Chris Maunder21-Sep-00 12:55
    sussChris Maunder21-Sep-00 12:55 
    AnswerRe: Low rating? Pin
    Alex21-Sep-00 16:44
    sussAlex21-Sep-00 16:44 
    AnswerRe: Low rating? Pin
    Norm Almond22-Sep-00 1:15
    sussNorm Almond22-Sep-00 1:15 
    GeneralRe: Low rating? Pin
    Alex22-Sep-00 14:54
    sussAlex22-Sep-00 14:54 
    QuestionWell, what did you think? Pin
    Alvaro Mendez21-Sep-00 9:21
    sussAlvaro Mendez21-Sep-00 9:21 

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

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

    | Advertise | Privacy | Terms of Use | Mobile
    Web03 | 2.8.160212.1 | Last Updated 21 Sep 2000
    Article Copyright 2000 by NormDroid
    Everything else Copyright © CodeProject, 1999-2016
    Layout: fixed | fluid