001
19.12.2006, 19:26 Uhr
~Jack
Gast
|
Hallo erstma,
also du erbst erstmal von der Klasse (z.B. TextBox) und änderst die Methoden, die für das Darstellen verantwortlich sind.
ein großes Beispiel findest du unter: www.codeproject.com/csharp/dzcollectioneditor.asp (der Kerl hat z.B. seine eigenen Controls erstellt)
oder ein Beispiel von mir:
Code: |
using System; using System.ComponentModel; using System.ComponentModel.Design; using System.Windows.Forms; using ControlsPlus.Enumeration; using System.Drawing;
namespace ControlsPlus { public class TextBoxPlus : TextBox, Observer { #region variables
private ControlsPlus.Enumeration.State state = ControlsPlus.Enumeration.State.Normal; private System.Windows.Forms.BorderStyle cbs_style_old = System.Windows.Forms.BorderStyle.FixedSingle; // Wird für den Start des Programmes gebraucht; ab 2005 private ControlsPlus.Enumeration.CustomBorderStyle cbs_style = ControlsPlus.Enumeration.CustomBorderStyle.Flat; private ControlDesigner controlDesigner = null;
#endregion
#region Constructor
public TextBoxPlus() { base.BorderStyle = cbs_style_old; Designer = ControlColors.getControlDesigner(); }
#endregion
#region Proberties
[Browsable(false)] protected Enumeration.State State { get { return state; } set { if(value!=state) { state = value; Invalidate(); } } }
[Browsable(true)] [Category("Appearance")] [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] public new Enumeration.CustomBorderStyle BorderStyle { get { return cbs_style; } set { cbs_style = value; switch(value) { case Enumeration.CustomBorderStyle.Fixed3D: cbs_style_old = System.Windows.Forms.BorderStyle.Fixed3D; base.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; Console.WriteLine("Fixed3D"); break; case Enumeration.CustomBorderStyle.Flat: cbs_style_old = System.Windows.Forms.BorderStyle.Fixed3D; base.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; Console.WriteLine("Flat"); break; } } }
#endregion
#region Overrides
protected override void OnMouseEnter(EventArgs e) { base.OnMouseEnter(e); State = Enumeration.State.Selected; }
protected override void OnMouseLeave(EventArgs e) { base.OnMouseLeave(e); if ( !ContainsFocus ) { State = Enumeration.State.Normal; } }
protected override void OnGotFocus(EventArgs e) { base.OnGotFocus(e); State = Enumeration.State.Selected; }
protected override void OnLostFocus(EventArgs e) { base.OnLostFocus(e); State = Enumeration.State.Normal; }
protected override void WndProc(ref Message m) { base.WndProc(ref m); if(m.Msg==(int)Msgs.WM_PAINT) { //Console.WriteLine("WM_PAINT"); border(); } }
#endregion
#region Private-Methods
/// <summary> /// Paint the border of the TextBox /// </summary> private void border() { if(this.BorderStyle == Enumeration.CustomBorderStyle.Flat) { using (Graphics g=this.CreateGraphics()) { Color borderColor;
if(Enabled) { // is Enabled if(state == Enumeration.State.Normal) { // is Normal if(this.ReadOnly) { // is ReadOnly borderColor = ControlColors.readOnlyBorder; } else { // is not ReadOnly borderColor = ControlColors.normalBorder; } } else { // is Selected or Pressed borderColor = ControlColors.selectedBorder; } } else { // is Disabled borderColor = ControlColors.disabledBorder; }
using (Pen pen= new Pen(borderColor)) { g.DrawRectangle(pen,new Rectangle(0,0,this.Width-1, this.Height-1)); } } } }
#endregion
} }
namespace ControlsPlus.Enumeration { // Art des Styles public enum CustomBorderStyle { Flat = 0, Fixed3D = 1 }
// Stufe des Styles, wenn Flat public enum Style { Normal = 0, Pressed = 1, Selected = 2 }
public enum State { Normal = 0, Pressed = 1, Selected = 2 }
public enum Msgs { WM_PAINT = 0x000F } }
using System; using System.Windows.Forms; using System.Drawing;
namespace ControlsPlus { public class ControlColors { private static Color defaultHighlightColor = SystemColors.Highlight; private static Color defaultControlColor = SystemColors.Control;
public static Color normalBorder { get { return defaultControlColor; } }
public static Color highlightBorder { get { return defaultHighlightColor; } }
public static void setNormalBorder(Color color) { defaultControlColor = color; }
public static void setHighlightBorder(Color color) { defaultHighlightColor = color; }
public static Color selectedBorder { get { return Color.FromArgb(130, highlightBorder); } }
public static Color readOnlyBorder { get { return ControlPaint.LightLight(normalBorder); } }
public static Color disabledBorder { get { return ControlPaint.Light(normalBorder); } }
} }
|
(PS sollten im Code fehler vorkommen, kann es daher sein, da ich einiges für diesen Post rausgelöscht habe was einfach nur unnötig war)
Sollten noch fragen bestehen: MELDEN! |