Denne side indeholder artikler med forskellige perspektiver på Identity & Access Management i private og offentlige organisationer. Artiklerne behandler aktuelle IAM-emner og leveres af producenter, rådgivere og implementeringspartnere.
Du kan også få lidt inspiration i følgende eksempel som jeg lige har strikket sammen. Eksemplet anvendder renderer klasserne for at få oplysninger om de relevante CheckBox dele.
Eksemplet er ikke en komplet løsning. Der mangler at blive implementeret states (når musen bevæges over kontrollen). Og man vil næppe heller acceptere dette simple X som jeg har lagt ind i demoen. Men der er mange muligheder og jeg tror at det vil give dig et fint overblik over hvad der skal til.
namespace CheckBox { using System; using System.Drawing; using System.Windows.Forms; using System.Windows.Forms.VisualStyles;
/// <summary> /// Calculate the text bounds, exluding the check box. /// </summary> internal Rectangle TextRectangle { get { Rectangle rectangle = new Rectangle();
/// <summary> /// Override in order to draw checkmark as "X" /// </summary> /// <param name="paintEventArgs">PaintEventArgs.</param> protected override void OnPaint(PaintEventArgs paintEventArgs) { // Note! Remember to enabme VisualStyles (Application.EnableVisualStyles();) // This demo will only work then VisualStyles is enabled and Appearance is normal. if (Application.RenderWithVisualStyles && this.Appearance == Appearance.Normal) // Dont do anything in case that the CheckBox is drawn as a button. { CheckBoxRenderer.DrawParentBackground(paintEventArgs.Graphics, paintEventArgs.ClipRectangle, this);
// In case that you want to draw all using the Renderer insert these lines. //CheckBoxRenderer.DrawCheckBox(paintEventArgs.Graphics, // this.ClientRectangle.Location, // this.TextRectangle, // this.Text, // this.Font, // TextFormatFlags.HorizontalCenter, // this.Checked, // this.checkBoxState);
/// <summary> /// Draws CheckMark. /// Note that this is only a short demo! CheckBoxStates needs to be implemented. /// </summary> /// <param name="paintEventArgs"></param> private void DrawCheckMark(Graphics graphics, bool checkBoxChecked, CheckBoxState checkBoxState) { if (graphics == null) { throw new ArgumentNullException("graphics"); }
// TODO: This demo will not implement the different CheckBoxStates. This is only a demo! float penSize = 1.0f;
if (this.Focused) // Note that this is only a silly example! { penSize = 2.0F; }
using (Pen pen = new Pen(Color.Red, penSize)) { graphics.DrawRectangle(pen, this.checkMarkBounds);
if (checkBoxChecked) // Draw "X" { graphics.DrawLine(pen, this.checkMarkBounds.Location, new Point(this.checkMarkBounds.Right, this.checkMarkBounds.Bottom)); graphics.DrawLine(pen, new Point(this.checkMarkBounds.Left, this.checkMarkBounds.Bottom), new Point(this.checkMarkBounds.Right, this.checkMarkBounds.Top)); } }
}
/// <summary> /// Draw the check box in the checked or unchecked state, alternately. /// </summary> /// <param name="e">MouseEventArgs</param> protected override void OnMouseDown(MouseEventArgs mouseEventArgs) { base.OnMouseDown(mouseEventArgs);
/// <summary> /// Get the sizes and offsets for the window parts as specified by the visual style. /// </summary> private void GetPartDetails() { // Do nothing further if visual styles are not enabled. if (!Application.RenderWithVisualStyles) { return; }
using (Graphics graphics = this.CreateGraphics()) { if (this.SetRenderer(VisualStyleElement.Button.CheckBox.UncheckedNormal)) { #region CheckMark
Size size = this.visualStyleRenderer.GetPartSize(graphics, ThemeSizeType.True); Point point = this.visualStyleRenderer.GetPoint(PointProperty.Offset);
this.checkMarkBounds = new Rectangle(point, size);
/// <summary> /// Set the VisualStyleRenderer to a new element. /// </summary> /// <param name="element">Visual style element to be set for the renderer.</param> /// <returns>Returns true in case that a valid renderer has been returned.</returns> private bool SetRenderer(VisualStyleElement visualStyleElement) { bool value = false;
if (VisualStyleRenderer.IsElementDefined(visualStyleElement)) { if (visualStyleRenderer == null) { this.visualStyleRenderer = new VisualStyleRenderer(visualStyleElement); } else { this.visualStyleRenderer.SetParameters(visualStyleElement); }
Og så kan man naturligvis snyde sig færdig ved blot at tegne et "X" på baggrunden af den alm. CheckBox (Hvis man kan acceptere dette). For at gøre dette skal du fjerne Checked state, og derefter blot tegne et "X" i boxen.
/// <summary> /// Override in order to draw checkmark as "X" /// </summary> /// <param name="paintEventArgs">PaintEventArgs.</param> protected override void OnPaint(PaintEventArgs paintEventArgs) { // Note! Remember to enabme VisualStyles (Application.EnableVisualStyles();) // This demo will only work then VisualStyles is enabled and Appearance is normal. if (Application.RenderWithVisualStyles && this.Appearance == Appearance.Normal) // Dont do anything in case that the CheckBox is drawn as a button. { CheckBoxRenderer.DrawParentBackground(paintEventArgs.Graphics, paintEventArgs.ClipRectangle, this);
CheckBoxState state = RemoveCheckedState(this.checkBoxState);
// In case that you want to draw all using the Renderer insert these lines. CheckBoxRenderer.DrawCheckBox(paintEventArgs.Graphics, this.ClientRectangle.Location, this.TextRectangle, this.Text, this.Font, TextFormatFlags.Left, this.Checked, state);
using (Pen pen = new Pen(this.textColor, penSize)) { graphics.DrawLine(pen, bounds.Location, new Point(bounds.Right -1, bounds.Bottom)); graphics.DrawLine(pen, new Point(bounds.Left, bounds.Bottom), new Point(bounds.Right -1, bounds.Top)); } }
private static CheckBoxState RemoveCheckedState(CheckBoxState checkBoxState) { // Cheat! Remove the checkmark by changing the checked state! switch (checkBoxState) { case CheckBoxState.CheckedDisabled: checkBoxState = CheckBoxState.UncheckedDisabled; break;
case CheckBoxState.CheckedHot: checkBoxState = CheckBoxState.UncheckedHot; break;
case CheckBoxState.CheckedNormal: checkBoxState = CheckBoxState.UncheckedNormal; break;
case CheckBoxState.CheckedPressed: checkBoxState = CheckBoxState.UncheckedPressed; break;
case CheckBoxState.MixedDisabled: checkBoxState = CheckBoxState.UncheckedDisabled; break;
case CheckBoxState.MixedHot: checkBoxState = CheckBoxState.UncheckedHot; break;
case CheckBoxState.MixedNormal: checkBoxState = CheckBoxState.UncheckedNormal; break;
case CheckBoxState.MixedPressed: checkBoxState = CheckBoxState.UncheckedPressed; break;
default: // Do Nothing break; } return checkBoxState; }
Men alligevel... her er en rettet udgave som også retter text bounds så det svarer til en alm. CheckBox. Jeg håber ikke at jeg har overset stavefejl denne gang ;-)
namespace CheckBoxX { using System; using System.Drawing; using System.Windows.Forms; using System.Windows.Forms.VisualStyles;
/// <summary> /// Override in order to draw checkmark as "X" /// </summary> /// <param name="paintEventArgs">PaintEventArgs.</param> protected override void OnPaint(PaintEventArgs paintEventArgs) { // Note! Remember to enabme VisualStyles (Application.EnableVisualStyles();) // This demo will only work then VisualStyles is enabled and Appearance is normal. if (Application.RenderWithVisualStyles && this.Appearance == Appearance.Normal) // Dont do anything in case that the CheckBox is drawn as a button. { CheckBoxRenderer.DrawParentBackground(paintEventArgs.Graphics, paintEventArgs.ClipRectangle, this);
CheckBoxState state = RemoveCheckedState(this.checkBoxState);
// In case that you want to draw all using the Renderer insert these lines. CheckBoxRenderer.DrawCheckBox(paintEventArgs.Graphics, this.ClientRectangle.Location, this.textBounds, this.Text, this.Font, TextFormatFlags.Left, this.ShowFocusCues & this.Focused, state);
/// <summary> /// Get the sizes and offsets for the window parts as specified by the visual style. /// </summary> private void GetPartDetails() { // Do nothing further if visual styles are not enabled. if (!Application.RenderWithVisualStyles) { return; }
using (Graphics graphics = this.CreateGraphics()) { if (this.SetRenderer(VisualStyleElement.Button.CheckBox.UncheckedNormal)) { #region CheckMark
Size glyphSize = CheckBoxRenderer.GetGlyphSize(graphics, CheckBoxState.UncheckedNormal); Point point = this.visualStyleRenderer.GetPoint(PointProperty.Offset);
this.checkMarkBounds = new Rectangle(point, glyphSize); this.checkMarkBounds.Inflate(-3, -3);
Size textSize = new Size(textExtent.Width - glyphSize.Width, textMetrics.Ascent);
this.textBounds = new Rectangle(textLocation, textSize);
this.textBounds.Location = textLocation; }
#endregion // Text } } }
/// <summary> /// Set the VisualStyleRenderer to a new element. /// </summary> /// <param name="element">Visual style element to be set for the renderer.</param> /// <returns>Returns true in case that a valid renderer has been returned.</returns> private bool SetRenderer(VisualStyleElement visualStyleElement) { bool value = false;
if (VisualStyleRenderer.IsElementDefined(visualStyleElement)) { if (visualStyleRenderer == null) { this.visualStyleRenderer = new VisualStyleRenderer(visualStyleElement); } else { this.visualStyleRenderer.SetParameters(visualStyleElement); }
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.