HeaderPanel.cs
Description du code
HeaderPanel.cs est un fichier du projet BrolDev.Ce fichier est situé dans /var/www/bin/sniplets/bibliobrol/broldev/src/.
Projet BrolDev : Librairie de composants réutilisables pour les applications BrolDev en CSharp.
Code source ou contenu du fichier
Code c# (HeaderPanel.cs) (1035 lignes)
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Text; using System.Runtime.InteropServices; using System.Windows.Forms; using be.gaudry.model.enums; using be.gaudry.view.style; using be.gaudry.view; using be.gaudry.observer; namespace KIS.Controls.Windows { [ToolboxBitmap (@"HeaderPanel.offScreenBmp")] public partial class HeaderPanel : Panel { private const int DEFAULT_BORDER_WIDTH = 1; private const int DEFAULT_SHADOW_WIDTH = 5; private const int DEFAULT_CAPTION_HEIGHT = 20;//SystemInformation.CaptionHeight; // Window message constants private const int WM_NCCALCSIZE = 0x0083; private const int WM_NCPAINT = 0x0085; #region Member Variables private int mint_CaptionHeight; private bool mbln_CaptionVisible = true; private bool mbln_Antialias = true; private string mstr_CaptionText; private Color mclr_CaptionBeginColor; private Color mclr_CaptionEndColor; private LinearGradientMode menm_CaptionGradientMode; private BORDER_POSITION menm_CaptionPosition; private EventHandler rendererChangedEvent; private Icon mico_Icon = null; private bool mbln_IconVisible = false; private BORDER_STYLE menm_BorderStyle; private Color mclr_BorderColor; private Color mclr_StartColor; private Color mclr_EndColor; private LinearGradientMode menm_GradientMode; private bool useCaptionBrolDevStyle, useContentBrolDevStyle, useContentGradient, useBorderBrolDevStyle; #endregion #region Constructor public HeaderPanel() : base() { InitializeComponent(); SystemFonts.CaptionFont.Style); this.mstr_CaptionText = this.GetType().Name; this.mint_CaptionHeight = SystemInformation.CaptionHeight; this.mclr_CaptionBeginColor = StyleFactory.CustomSystemColors.ActiveCaption; this.mclr_CaptionEndColor = StyleFactory.CustomSystemColors.GradientActiveCaption; this.menm_CaptionGradientMode = LinearGradientMode.Vertical; this.menm_CaptionPosition = BORDER_POSITION.Top; this.menm_BorderStyle = StyleFactory.BorderStyle; base.BorderStyle = 0; this.mclr_BorderColor = StyleFactory.CustomSystemColors.Menu; this.mclr_StartColor = StyleFactory.CustomSystemColors.ControlLightLight; this.mclr_EndColor = StyleFactory.CustomSystemColors.Control; this.menm_GradientMode = LinearGradientMode.Vertical; this.SetStyle(ControlStyles.ResizeRedraw, true); this.SetStyle(ControlStyles.UserPaint, true); this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); this.SetStyle(ControlStyles.DoubleBuffer, true); useCaptionBrolDevStyle = true; useContentBrolDevStyle = true; useBorderBrolDevStyle = true; //todo : find method to detach the delegate on dispose (this does not implements dispose) StyleFactory.Instance.onRendererChanged += rendererChangedEvent; setBrolDevStyle(); } #endregion #region properties #region content [ Description("Enable/Disable using gradient colors for the content. False to use only first color."), Category("Content"), DefaultValue(true), Browsable(true) ] public bool UseContentGradient { get { return useContentGradient; } set { useContentGradient = value; //todo : set values from renderer setBrolDevStyle(); } } [ Description("Enable/Disable using parent renderer colors for the content panel"), Category("Content"), DefaultValue(true), Browsable(true) ] public bool UseContentBrolDevStyle { get { return useContentBrolDevStyle; } set { useContentBrolDevStyle = value; setBrolDevStyle(); } } [ Description("Change the starting color of gradient background. Has no effect if UseCustomRenderer is true."), Category("Content"), Browsable(true) ] public Color GradientStart { get { return mclr_StartColor; } set { if (!useContentBrolDevStyle) { mclr_StartColor = value; setBrolDevStyle(); } } } [ Description("Change the end color of gradient background. Has no effect if UseCustomRenderer is true."), Category("Content"), Browsable(true) ] public Color GradientEnd { get { return mclr_EndColor; } set { if (!useContentBrolDevStyle) { mclr_EndColor = value; setBrolDevStyle(); } } } [ Description("Change the background gradient direction"), Category("Content"), DefaultValue(LinearGradientMode.Vertical), Browsable(true) ] public LinearGradientMode GradientDirection { get { return menm_GradientMode; } set { menm_GradientMode = value; setBrolDevStyle(); } } #endregion #region border [ Description("Enable/Disable using parent renderer colors for the border"), Category("Border"), DefaultValue(true), Browsable(true) ] public bool UseBorderBrolDevStyle { get { return useBorderBrolDevStyle; } set { useBorderBrolDevStyle = value; Invalidate(); } } [ Description("Change style of the border"), Category("Border"), DefaultValue(BORDER_STYLE.Groove), Browsable(true) ] public new BORDER_STYLE BorderStyle { get { return menm_BorderStyle; } set { menm_BorderStyle = value; PerformLayout(); setBrolDevStyle(); } } [ Description("Change color of the border. Has no effect if UseCustomRenderer is true."), Category("Border"), Browsable(true) ] public Color BorderColor { get { return mclr_BorderColor; } set { if (!useContentBrolDevStyle) { mclr_BorderColor = value; setBrolDevStyle(); } } } #endregion #region caption [ Description("Enable/Disable using parent renderer colors for the caption"), Category("Border"), DefaultValue(true), Browsable(true) ] public bool UseCaptionBrolDevStyle { get { return useCaptionBrolDevStyle; } set { useCaptionBrolDevStyle = value; //todo : set values from renderer setBrolDevStyle(); } } [ Description("Enable/Disable antialiasing"), Category("Caption"), DefaultValue(false), Browsable(true) ] public bool TextAntialias { get { return mbln_Antialias; } set { mbln_Antialias = value; Invalidate(); } } [ Description("Change the caption text to be displayed"), Category("Caption"), Browsable(true) ] public String CaptionText { get { return mstr_CaptionText; } set { mstr_CaptionText = value; Invalidate(); } } [ Description("Show or hide the caption"), Category("Caption"), DefaultValue(true), Browsable(true) ] public bool CaptionVisible { get { return mbln_CaptionVisible; } set { mbln_CaptionVisible = value; Invalidate(); } } [ Description("Change the caption height"), Category("Caption"), DefaultValue(DEFAULT_CAPTION_HEIGHT), Browsable(true) ] public int CaptionHeight { get { return mint_CaptionHeight; } set { mint_CaptionHeight = value; mbln_IconVisible = (mint_CaptionHeight >= 16); Invalidate(); } } [ Description("Change the caption gradient direction. Has no effect if UseCustomRenderer is true."), Category("Caption"), DefaultValue(LinearGradientMode.Vertical), Browsable(true) ] public LinearGradientMode CaptionGradientDirection { get { return menm_CaptionGradientMode; } set { menm_CaptionGradientMode = value; Invalidate(); } } [ Description("Change the caption gradient's start color. Has no effect if UseCustomRenderer is true."), Category("Caption"), Browsable(true) ] public Color CaptionBeginColor { get { return mclr_CaptionBeginColor; } set { if (!useCaptionBrolDevStyle) { mclr_CaptionBeginColor = value; setBrolDevStyle(); } } } [ Description("Change the caption gradient's end color. Has no effect if UseCustomRenderer is true."), Category("Caption"), Browsable(true) ] public Color CaptionEndColor { get { return mclr_CaptionEndColor; } set { if (!useCaptionBrolDevStyle) { mclr_CaptionEndColor = value; setBrolDevStyle(); } } } [ Description("Change the caption position"), Category("Caption"), DefaultValue(BORDER_POSITION.Top), Browsable(true) ] public BORDER_POSITION CaptionPosition { get { return this.menm_CaptionPosition; } set { this.menm_CaptionPosition = value; Invalidate(); } } [ Description("Change the caption font"), Category("Caption"), Browsable(true) ] public override Font Font { get { return base.Font; } set { base.Font = value; if (value.GetHeight() > mint_CaptionHeight) mint_CaptionHeight = Convert.ToInt32(value.GetHeight() + 8.0f); Invalidate(); } } #endregion #region icon [ Description("Change the icon to display in the title"), Category("Icon"), Browsable(true) ] public Icon PanelIcon { get { return mico_Icon; } set { mico_Icon = value; Invalidate(); } } [ Description("Enable/Disable the icon"), Category("Icon"), DefaultValue(false), Browsable(true) ] public bool PanelIconVisible { get { return mbln_IconVisible; } set { mbln_IconVisible = value && (mint_CaptionHeight >= 16); Invalidate(); } } #endregion #endregion #region Overrided Methods protected override void Dispose(bool disposing) { StyleFactory.Instance.onRendererChanged -= rendererChangedEvent; if (disposing) { if (components != null) { components.Dispose(); } } base.Dispose(disposing); } protected override void WndProc(ref Message m) { switch (m.Msg) { case WM_NCCALCSIZE: WmNCCalcSize(ref m); break; case WM_NCPAINT: IntPtr hDC = NativeMethods.GetWindowDC(m.HWnd); if (hDC != IntPtr.Zero) { using (Graphics canvas = Graphics.FromHdc(hDC)) { PaintNonClientArea(canvas); } NativeMethods.ReleaseDC(m.HWnd, hDC); } m.Result = IntPtr.Zero; break; } base.WndProc(ref m); } protected override void OnResize(EventArgs e) { Invalidate(); base.OnResize(e); } protected override void OnPaint(PaintEventArgs e) { if (Width == 0 || Height == 0) return; base.OnPaint(e); if (BackgroundImage == null && useContentGradient) { DrawBackground(e, menm_BorderStyle); } } #endregion #region Event Handlers private void HeaderPanel_Scroll(object sender, ScrollEventArgs e) { Invalidate(); } private void onRendererChanged(object sender, EventArgs e) { setBrolDevStyle(); } #endregion #region private methods private void setBrolDevStyle() { /*if (this.Visible) { this.SuspendLayout(); }*/ if (useCaptionBrolDevStyle) { mclr_CaptionBeginColor = StyleFactory.CustomSystemColors.ActiveCaption;//StyleFactory.ProfessionalColorTable.ToolStripContentPanelGradientBegin; mclr_CaptionEndColor = StyleFactory.CustomSystemColors.GradientActiveCaption;//StyleFactory.ProfessionalColorTable.ToolStripContentPanelGradientEnd; } if (useContentBrolDevStyle) { mclr_StartColor = StyleFactory.CustomSystemColors.ControlLightLight; mclr_EndColor = StyleFactory.CustomSystemColors.Control; mclr_BorderColor = StyleFactory.CustomSystemColors.ActiveBorder; } if (useBorderBrolDevStyle) { menm_BorderStyle = StyleFactory.BorderStyle; } //does not work as well as we want //using invalidate seems not the solution!!! if (this.Visible) { Invalidate(); //this.OnPaint(new PaintEventArgs(this.grap); //this.Refresh(); //this.Show(); //this.PerformLayout(); //this.ResumeLayout(true); //OnResize(null); be.gaudry.observer.StaticObservable.notify( be.gaudry.observer.Notification.VERBOSE.basicOperation, "H mod", "\n" + this.CaptionText + " candidate to invalidate\nStyle : " + EnumHelper.GetDescription(StyleFactory.getStyle()) + "\n BE CAREFULL :\n\nProblems seems occurs on HeaderPanel Invalidate. It doesn't apply the new style.", this)); } } #endregion #region Helper Routines private void WmNCCalcSize(ref Message m) { if (m.WParam == NativeMethods.FALSE) { Rectangle proposed = ncRect.Rect; RecalcNonClientArea(ref proposed); ncRect = NativeMethods.RECT.FromRectangle(proposed); Marshal.StructureToPtr(ncRect, m.LParam, false); } else if (m.WParam == NativeMethods.TRUE) { NativeMethods.NCCALCSIZE_PARAMS ncParams = Rectangle proposed = ncParams.rectProposed.Rect; RecalcNonClientArea(ref proposed); ncParams.rectProposed = NativeMethods.RECT.FromRectangle(proposed); Marshal.StructureToPtr(ncParams, m.LParam, false); } m.Result = IntPtr.Zero; } private void RecalcNonClientArea(ref Rectangle proposed) { int brdWidth = 0; int shadowWidth = 0; int captionHight = 0; brdWidth = ComputeBorderWidth(); if (menm_BorderStyle == BORDER_STYLE.Shadow) shadowWidth = DEFAULT_SHADOW_WIDTH; if (mbln_CaptionVisible) captionHight = mint_CaptionHeight; switch (menm_CaptionPosition) { case BORDER_POSITION.Top: proposed.Top + (brdWidth + captionHight), proposed.Width - (brdWidth + brdWidth + shadowWidth), proposed.Height - (brdWidth + brdWidth + shadowWidth + captionHight)); break; case BORDER_POSITION.Bottom: proposed.Top + brdWidth, proposed.Width - (brdWidth + brdWidth + shadowWidth), proposed.Height - (brdWidth + brdWidth + shadowWidth + captionHight)); break; case BORDER_POSITION.Left: proposed.Top + brdWidth, proposed.Width - (brdWidth + brdWidth + shadowWidth), proposed.Height - (brdWidth + brdWidth + shadowWidth)); break; case BORDER_POSITION.Right: proposed.Top + brdWidth, proposed.Width - (brdWidth + brdWidth + shadowWidth + captionHight), proposed.Height - (brdWidth + brdWidth + shadowWidth)); break; } } private void PaintNonClientArea(Graphics canvas) { if (menm_BorderStyle == BORDER_STYLE.Shadow) DrawShadow(canvas); if (menm_BorderStyle != BORDER_STYLE.None) DrawBorder(canvas); if (mbln_CaptionVisible) DrawCaption(canvas); } private void DrawShadow(Graphics canvas) { Pen pen = null; Color[] clrArr = null; try { Color.FromArgb(171, 171, 171), Color.FromArgb(212, 212, 212), Color.FromArgb(220, 220, 220)}; for (int cntr = 0; cntr < 4; cntr++) { canvas.DrawLine(pen, Width - (5 - cntr), ((cntr + 1) * 2), Width - (5 - cntr), Height - (5 - cntr)); canvas.DrawLine(pen, ((cntr + 1) * 2), Height - (5 - cntr), Width - (5 - cntr), Height - (5 - cntr)); pen.Dispose(); pen = null; } } finally { if (pen != null) { pen.Dispose(); pen = null; } clrArr = null; } } private void DrawCaption(Graphics canvas) { int brdWidth = 0; int shadowWidth = 0; Bitmap offScreenBmp = null; Graphics offScreenDC = null; Rectangle drawRect; StringFormat format = null; LinearGradientBrush brsh = null; try { if (menm_BorderStyle == BORDER_STYLE.None) brdWidth = 0; else if (menm_BorderStyle == BORDER_STYLE.Groove || menm_BorderStyle == BORDER_STYLE.Ridge) brdWidth = 2; else brdWidth = DEFAULT_BORDER_WIDTH; if (menm_BorderStyle == BORDER_STYLE.Shadow) shadowWidth = DEFAULT_SHADOW_WIDTH; format.FormatFlags = StringFormatFlags.NoWrap; format.LineAlignment = StringAlignment.Center; format.Alignment = StringAlignment.Near; format.Trimming = StringTrimming.EllipsisCharacter; if (menm_CaptionPosition == BORDER_POSITION.Bottom || menm_CaptionPosition == BORDER_POSITION.Top) mint_CaptionHeight); else mint_CaptionHeight); offScreenDC = Graphics.FromImage(offScreenBmp); if (mbln_Antialias) offScreenDC.TextRenderingHint = TextRenderingHint.AntiAlias; offScreenBmp.Width, offScreenBmp.Height), mclr_CaptionBeginColor, mclr_CaptionEndColor, menm_CaptionGradientMode); offScreenDC.FillRectangle(brsh, 0.0f, 0.0f, offScreenBmp.Width, offScreenBmp.Height); if (mico_Icon != null && mbln_IconVisible) { offScreenDC.DrawIcon(mico_Icon, (mint_CaptionHeight - 16) / 2, 16, 16)); offScreenDC.DrawString(mstr_CaptionText, this.Font, offScreenBmp.Width - 20, mint_CaptionHeight), format); } else { offScreenDC.DrawString(mstr_CaptionText, this.Font, offScreenBmp.Width, mint_CaptionHeight), format); } switch (menm_CaptionPosition) { case BORDER_POSITION.Top: { drawRect.X = brdWidth; drawRect.Width = DisplayRectangle.Width; drawRect.Y = brdWidth; drawRect.Height = mint_CaptionHeight; break; } case BORDER_POSITION.Bottom: { drawRect.X = brdWidth; drawRect.Width = DisplayRectangle.Width; drawRect.Y = Height - (shadowWidth + brdWidth + mint_CaptionHeight); drawRect.Height = mint_CaptionHeight; break; } case BORDER_POSITION.Left: { drawRect.X = brdWidth; drawRect.Width = mint_CaptionHeight; drawRect.Y = brdWidth; drawRect.Height = Height - (shadowWidth + (2 * brdWidth)); offScreenBmp.RotateFlip(RotateFlipType.Rotate270FlipNone); break; } case BORDER_POSITION.Right: { drawRect.X = Width - (shadowWidth + brdWidth + mint_CaptionHeight); drawRect.Width = mint_CaptionHeight; drawRect.Y = brdWidth; drawRect.Height = Height - (shadowWidth + (2 * brdWidth)); offScreenBmp.RotateFlip(RotateFlipType.Rotate90FlipNone); break; } } canvas.DrawImage(offScreenBmp, drawRect); } catch (Exception ex) { } finally { if (brsh != null) { brsh.Dispose(); brsh = null; } if (offScreenDC != null) { offScreenDC.Dispose(); offScreenDC = null; } if (offScreenBmp != null) { offScreenBmp.Dispose(); offScreenBmp = null; } if (format != null) { format.Dispose(); format = null; } } } private void DrawBackground(PaintEventArgs peArgs, BORDER_STYLE brdStyle) { Brush brsh = null; try { if (menm_BorderStyle == BORDER_STYLE.Shadow) { //if (useContentGradient) //{ mclr_EndColor, menm_GradientMode ); //} peArgs.Graphics.FillRectangle(brsh, 0, 0, Width - DEFAULT_SHADOW_WIDTH, Height - DEFAULT_SHADOW_WIDTH); } else { if (useContentGradient) { mclr_StartColor, mclr_EndColor, menm_GradientMode ); } else { } peArgs.Graphics.FillRectangle(brsh, 0, 0, Width, Height); } } finally { if (brsh != null) { brsh.Dispose(); brsh = null; } } } private void DrawBorder(Graphics canvas) { Pen penDark = null; Pen penLight = null; try { switch (menm_BorderStyle) { case BORDER_STYLE.Shadow: canvas.DrawRectangle(penDark, 0, 0, Width - 6, Height - 6); break; case BORDER_STYLE.Single: canvas.DrawRectangle(penDark, 0, 0, Width - DEFAULT_BORDER_WIDTH, Height - DEFAULT_BORDER_WIDTH); break; case BORDER_STYLE.Inset: canvas.DrawLine(penLight, 0, 0, Width - DEFAULT_BORDER_WIDTH, 0); canvas.DrawLine(penLight, 0, 0, 0, Height - DEFAULT_BORDER_WIDTH); canvas.DrawLine(penDark, Width - DEFAULT_BORDER_WIDTH, 0, Width - DEFAULT_BORDER_WIDTH, Height - DEFAULT_BORDER_WIDTH); canvas.DrawLine(penDark, 0, Height - DEFAULT_BORDER_WIDTH, Width - DEFAULT_BORDER_WIDTH, Height - DEFAULT_BORDER_WIDTH); break; case BORDER_STYLE.Outset: canvas.DrawLine(penDark, 0, 0, Width - DEFAULT_BORDER_WIDTH, 0); canvas.DrawLine(penDark, 0, 0, 0, Height - DEFAULT_BORDER_WIDTH); canvas.DrawLine(penLight, Width - DEFAULT_BORDER_WIDTH, 0, Width - DEFAULT_BORDER_WIDTH, Height - DEFAULT_BORDER_WIDTH); canvas.DrawLine(penLight, 0, Height - DEFAULT_BORDER_WIDTH, Width - DEFAULT_BORDER_WIDTH, Height - DEFAULT_BORDER_WIDTH); break; case BORDER_STYLE.Groove: canvas.DrawRectangle(penLight, Height - (2 * DEFAULT_BORDER_WIDTH))); canvas.DrawRectangle(penDark, Height - (2 * DEFAULT_BORDER_WIDTH))); break; case BORDER_STYLE.Ridge: canvas.DrawRectangle(penLight, Height - (2 * DEFAULT_BORDER_WIDTH))); canvas.DrawRectangle(penDark, Height - (2 * DEFAULT_BORDER_WIDTH))); break; default: break; } } finally { if (penDark != null) { penDark.Dispose(); penDark = null; } if (penLight != null) { penLight.Dispose(); penLight = null; } } } /// <summary> /// Helper function to compute the border width /// </summary> /// <returns>The border width</returns> private int ComputeBorderWidth() { int brdWidth = 0; switch (menm_BorderStyle) { case BORDER_STYLE.Single: case BORDER_STYLE.Shadow: case BORDER_STYLE.Inset: case BORDER_STYLE.Outset: brdWidth = DEFAULT_BORDER_WIDTH; break; case BORDER_STYLE.Groove: case BORDER_STYLE.Ridge: brdWidth = (DEFAULT_BORDER_WIDTH + DEFAULT_BORDER_WIDTH); break; default: break; } return brdWidth; } #endregion } }
Structure et Fichiers du projet
Afficher/masquer...Icône | Nom | Taille | Modification |
Pas de sous-répertoires. | |||
Icône | Nom | Taille | Modification |
| _ | Répertoire parent | 0 octets | 1732283640 22/11/2024 14:54:00 |
Utilisation de l'explorateur de code
- Navigation :
- Un clic sur une icône de répertoire ouvre ce répertoire pour en afficher les fichiers.
- Lorsque le répertoire en cours ne contient pas de sous-répertoires il est possible de remonter vers le répertoire parent.
- La structure de répertoires en treetable (tableau en forme d'arborescence) n'est plus possibledans cette version.
- Un clic sur une icône de fichier ouvre ce fichier pour en afficher le code avec la coloration syntaxique adaptée en fonction du langage principal utilisé dans le fichier.
- Affichage :
- Il est possible de trier les répertoires ou les fichiers selon certains critères (nom, taille, date).
- Actions :
- Les actions possible sur les fichiers dépendent de vos droits d'utilisateur sur le site. Veuillez activer le mode utilisateur pour activer les actions.
English translation
You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.
Thank you in advance.
Document created the 16/10/2009, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/cs-broldev-source-rf-view/controls//HeaderPanel.cs.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.