public override void OnDraw(Graphics graphics, Rectangle valueRect, Color textColor, PropertyEnumerator propEnum, string drawAnotherString) { if ((drawAnotherString != null) && (drawAnotherString.Length == 0)) { return; } string[] displayedStrings = Value.GetDisplayedValues(); if (displayedStrings.Length == 0) { return; } int margin = Value.Grid.GlobalTextMargin; valueRect.Height++; valueRect.X += margin - 1; valueRect.Width -= margin - 1; Rectangle oneLineRect = valueRect; bool hasMultipleValues = Value.HasMultipleValues; if (Value.UnderlyingType == typeof(Boolean)) { bool value = (bool)Value.GetValue(); Rectangle checkboxRect = oneLineRect; checkboxRect.Height -= 4; checkboxRect.Y += 2; checkboxRect.Width = checkboxRect.Height; // Draw checkbox if (hasMultipleValues) { propEnum.Property.ParentGrid.DrawManager.DrawCheckBoxIndeterminate(graphics, propEnum.Property.ParentGrid.Grid, checkboxRect, propEnum.Property.Enabled); } else { propEnum.Property.ParentGrid.DrawManager.DrawCheckBox(graphics, propEnum.Property.ParentGrid.Grid, checkboxRect, value, propEnum.Property.Enabled); Rectangle textRect = oneLineRect; textRect.X = checkboxRect.Right + margin; textRect.Width = oneLineRect.Right - textRect.Left; // Draw corresponding text Win32Calls.DrawText(graphics, (value ? displayedStrings[0] : displayedStrings[1]), ref textRect, Value.Font, textColor, Win32Calls.DT_VCENTER | Win32Calls.DT_SINGLELINE | Win32Calls.DT_NOPREFIX | ((Value.Grid.EllipsisMode & PropertyGrid.EllipsisModes.EllipsisOnValues) != 0 ? Win32Calls.DT_END_ELLIPSIS : 0)); } } else if (propEnum.Property.Value.UnderlyingType.IsEnum) { oneLineRect.Height = valueRect.Height / displayedStrings.Length; int currentValue = 0; if (hasMultipleValues == false) { currentValue = propEnum.Property.Value.TypeConverter.ConvertFromString( propEnum.Property.Value.GetTypeDescriptorContext(Value.OwnerEnumerator), propEnum.Property.Value.CultureInfo, (drawAnotherString != null ? drawAnotherString : propEnum.Property.Value.GetStringValue())).GetHashCode(); } FieldInfo[] fis = Value.UnderlyingType.GetFields(BindingFlags.Public | BindingFlags.Static); #if _DOTNET2 // There is a bug in .Net 2.0 that prevents to use ArrayList.Sort (performance reasons) Array.Sort <object>(fis, 0, fis.Length, new FieldInfoEnumComparer(propEnum.Property.Value.UnderlyingType)); #else Array.Sort(fis, 0, fis.Length, new FieldInfoEnumComparer(propEnum.Property.Value.UnderlyingType)); #endif int i = 0; int displayedStringsIndex = 0; foreach (FieldInfo fi in fis) { // If the field is not browsable, we continue to the next one object[] attrs = fi.GetCustomAttributes(typeof(BrowsableAttribute), false); if ((attrs.Length > 0) && (((BrowsableAttribute)attrs[0]).Browsable == false)) { i++; continue; } Rectangle checkboxRect = oneLineRect; checkboxRect.Height -= 4; checkboxRect.Y += 2; checkboxRect.Width = checkboxRect.Height; // Draw checkbox if (hasMultipleValues) { propEnum.Property.ParentGrid.DrawManager.DrawCheckBoxIndeterminate(graphics, propEnum.Property.ParentGrid.Grid, checkboxRect, propEnum.Property.Enabled); } else { int flagValue = fi.GetValue(propEnum.Property.Value.UnderlyingValue).GetHashCode(); bool check = (((currentValue & flagValue) == flagValue) && (flagValue != 0)) || ((currentValue == 0) && (flagValue == 0)); propEnum.Property.ParentGrid.DrawManager.DrawCheckBox(graphics, propEnum.Property.ParentGrid.Grid, checkboxRect, check, propEnum.Property.Enabled); } Rectangle textRect = oneLineRect; textRect.X = checkboxRect.Right + margin; textRect.Width = oneLineRect.Right - textRect.Left; // Draw corresponding text Win32Calls.DrawText(graphics, displayedStrings[displayedStringsIndex], ref textRect, Value.Font, textColor, Win32Calls.DT_VCENTER | Win32Calls.DT_SINGLELINE | Win32Calls.DT_NOPREFIX | ((Value.Grid.EllipsisMode & PropertyGrid.EllipsisModes.EllipsisOnValues) != 0 ? Win32Calls.DT_END_ELLIPSIS : 0)); oneLineRect.Offset(0, oneLineRect.Height); i++; displayedStringsIndex++; } } else if (propEnum.Property.Value.UnderlyingValue is ICollection) { oneLineRect.Height = valueRect.Height / displayedStrings.Length; for (int i = 0; i < displayedStrings.Length; i++) { Rectangle checkboxRect = oneLineRect; checkboxRect.Height -= 4; checkboxRect.Y += 2; checkboxRect.Width = checkboxRect.Height; // Draw checkbox if (hasMultipleValues) { propEnum.Property.ParentGrid.DrawManager.DrawCheckBoxIndeterminate(graphics, propEnum.Property.ParentGrid.Grid, checkboxRect, propEnum.Property.Enabled); } else { bool strFound = false; ICollection collection = propEnum.Property.Value.UnderlyingValue as ICollection; foreach (object o in collection) { if (o.ToString() == displayedStrings[i]) { strFound = true; } } propEnum.Property.ParentGrid.DrawManager.DrawCheckBox(graphics, propEnum.Property.ParentGrid.Grid, checkboxRect, strFound, propEnum.Property.Enabled); } Rectangle textRect = oneLineRect; textRect.X = checkboxRect.Right + margin; textRect.Width = oneLineRect.Right - textRect.Left; // Draw corresponding text Win32Calls.DrawText(graphics, displayedStrings[i], ref textRect, Value.Font, textColor, Win32Calls.DT_VCENTER | Win32Calls.DT_SINGLELINE | Win32Calls.DT_NOPREFIX | ((Value.Grid.EllipsisMode & PropertyGrid.EllipsisModes.EllipsisOnValues) != 0 ? Win32Calls.DT_END_ELLIPSIS : 0)); oneLineRect.Offset(0, oneLineRect.Height); } } }
// DrawText wrapper public static int DrawText(Graphics graphics, string text, ref Rectangle rect, Font font, Color color, int format) { if (_instance == null) _instance = new Win32Calls(); IntPtr hFont; object value = _instance._fonts[font]; if (value == null) { value = font.ToHfont(); _instance._fonts.Add(font, value); } hFont = (IntPtr)value; RECT rc = new RECT(); rc.Left = rect.Left; rc.Right = rect.Right; rc.Top = rect.Top; rc.Bottom = rect.Bottom; IntPtr hDC = graphics.GetHdc(); IntPtr oldHFont = SelectObject(hDC, hFont); ColorRef oldColor = SetTextColor(hDC, new ColorRef(color)); SetBkMode(hDC, TRANSPARENT); int result = DrawTextA(hDC, text, text.Length, ref rc, format); if ((format & DT_CALCRECT) != 0) { rect.X = rc.Left; rect.Y = rc.Top; rect.Width = rc.Right - rc.Left; rect.Height = rc.Bottom - rc.Top; } SetTextColor(hDC, oldColor); SelectObject(hDC, oldHFont); graphics.ReleaseHdc(hDC); return result; }
public override Rectangle GetDisplayStringRect(Graphics graphics, Rectangle valueRect, Point point) { int margin = Value.Grid.GlobalTextMargin; valueRect.X += margin - 1; valueRect.Width -= margin - 1; Rectangle calcRect = Rectangle.Empty; string[] displayedStrings = Value.GetDisplayedValues(); if (displayedStrings.Length == 0) { return(calcRect); } if (Value.UnderlyingType == typeof(Boolean)) { if (Value.HasMultipleValues) { return(Rectangle.Empty); } bool value = (bool)Value.GetValue(); string str = (value ? displayedStrings[0] : displayedStrings[1]); // Radio button rectangle Rectangle checkboxRect = valueRect; checkboxRect.Height -= 4; checkboxRect.Y += 2; checkboxRect.Width = checkboxRect.Height; // Text rectangle Rectangle textRect = valueRect; textRect.X = checkboxRect.Right + margin; textRect.Width = valueRect.Right - textRect.Left; // Draw corresponding text calcRect = textRect; Win32Calls.DrawText(graphics, str, ref calcRect, Value.Font, Color.Black, Win32Calls.DT_VCENTER | Win32Calls.DT_SINGLELINE | Win32Calls.DT_CALCRECT | Win32Calls.DT_NOPREFIX); calcRect.Y = valueRect.Top; calcRect.Height = valueRect.Bottom - calcRect.Y; return(calcRect); } else { Rectangle oneLineRect = valueRect; oneLineRect.Height = valueRect.Height / displayedStrings.Length; for (int i = 0; i < displayedStrings.Length; i++) { string str = displayedStrings[i]; // Radio button rectangle Rectangle checkboxRect = oneLineRect; checkboxRect.Height -= 4; checkboxRect.Y += 2; checkboxRect.Width = checkboxRect.Height; // Text rectangle Rectangle textRect = oneLineRect; textRect.X = checkboxRect.Right + margin; textRect.Width = oneLineRect.Right - textRect.Left; // Draw corresponding text calcRect = textRect; Win32Calls.DrawText(graphics, str, ref calcRect, Value.Font, Color.Black, Win32Calls.DT_VCENTER | Win32Calls.DT_SINGLELINE | Win32Calls.DT_CALCRECT | Win32Calls.DT_NOPREFIX); if (calcRect.Contains(point)) { break; } oneLineRect.Offset(0, oneLineRect.Height); } calcRect.Y = oneLineRect.Top; calcRect.Height = oneLineRect.Bottom - calcRect.Y; return(calcRect); } }
public override Size GetTooltipStringSize(Graphics graphics, Rectangle valueRect, Point point, int line) { return(Win32Calls.GetTextExtent(graphics, GetTooltipString(line), Value.Font)); }
private void StartHookMessages() { GC.KeepAlive(this); lock (this) { if (_messageHookHandle != IntPtr.Zero) { return; } if (thisProcessID == 0) { Win32Calls.GetWindowThreadProcessId(new HandleRef(_control, _control.Handle), out thisProcessID); } MessageHookObject obj = new MessageHookObject(this); Win32Calls.HookProc msgHookProc = new Win32Calls.HookProc(obj.Callback); _messageHookRoot = GCHandle.Alloc(msgHookProc); _messageHookHandle = Win32Calls.SetWindowsHookEx(Win32Calls.WH_CALLWNDPROC, msgHookProc, (IntPtr)0, Win32Calls.GetCurrentThreadId()); } }
protected override void OnPaint(PaintEventArgs e) { Rectangle clientRect = ClientRectangle; clientRect.Height++; // Fill the background _ownerPropertyEnum.Property.ParentGrid.DrawManager.DrawPropertyValueBackground(e.Graphics, clientRect, _ownerPropertyEnum); clientRect.X += _ownerPropertyEnum.Property.ParentGrid.GlobalTextMargin - 1; clientRect.Width -= _ownerPropertyEnum.Property.ParentGrid.GlobalTextMargin - 1; string[] displayStrings = _ownerPropertyEnum.Property.Value.GetDisplayedValues(); if (displayStrings.Length > 0) { Rectangle oneLineRect = clientRect; oneLineRect.Height = clientRect.Height / displayStrings.Length; for (int i = 0; i < displayStrings.Length; i++) { Rectangle radioRect = oneLineRect; radioRect.Height -= 4; radioRect.Y += 2; radioRect.Width = radioRect.Height; // Draw radio button _ownerPropertyEnum.Property.ParentGrid.DrawManager.DrawRadioButton(e.Graphics, _ownerPropertyEnum.Property.ParentGrid.Grid, radioRect, (Text == displayStrings[i]), _ownerPropertyEnum.Property.Enabled); Rectangle textRect = oneLineRect; textRect.X = radioRect.Right + _ownerPropertyEnum.Property.ParentGrid.GlobalTextMargin; textRect.Width = oneLineRect.Right - textRect.Left; // Draw corresponding text Color valueColor; if (_ownerPropertyEnum.Property.Enabled == false) { valueColor = SystemColors.GrayText; } else { valueColor = _ownerPropertyEnum.Property.Value.ForeColor; } Win32Calls.DrawText(e.Graphics, displayStrings[i], ref textRect, Font, valueColor, Win32Calls.DT_VCENTER | Win32Calls.DT_SINGLELINE | Win32Calls.DT_END_ELLIPSIS | Win32Calls.DT_NOPREFIX); if (Focused && (_focusIndex == i)) { Rectangle focusRect = textRect; focusRect.X -= 2; focusRect.Width += 2; focusRect.Y += 1; focusRect.Height -= 2; ControlPaint.DrawFocusRectangle(e.Graphics, focusRect); } oneLineRect.Offset(0, oneLineRect.Height); } } }
protected override void OnPaint(PaintEventArgs e) { // Draw the background mOwnerPropertyEnum.Property.ParentGrid.DrawManager.DrawPropertyValueBackground(e.Graphics, ClientRectangle, mOwnerPropertyEnum); Rectangle buttonRect = GetButtonRect(e.Graphics); if (!ReadOnly) { if (ThemeRenderer.Enabled) { int state = (_pushed ? (_mouseOver ? ThemeButton.PushButtonPressed : ThemeButton.PushButtonHot) : (_mouseOver ? ThemeButton.PushButtonHot : ThemeButton.PushButtonNormal)); ThemeButton.PushButton.Draw(e.Graphics, state, buttonRect); Win32Calls.DrawText(e.Graphics, _buttonText, ref buttonRect, Font, OwnerPropertyEnumerator.Property.Value.ForeColor, Win32Calls.DT_SINGLELINE | Win32Calls.DT_VCENTER | Win32Calls.DT_CENTER); } else { ControlPaint.DrawButton(e.Graphics, buttonRect, (_pushed && _mouseOver) ? ButtonState.Pushed : ButtonState.Normal); StringFormat stringFormat = (StringFormat)StringFormat.GenericDefault.Clone(); stringFormat.Alignment = StringAlignment.Center; stringFormat.LineAlignment = StringAlignment.Center; Brush brush = new SolidBrush(Color.Black); e.Graphics.DrawString(_buttonText, Font, brush, buttonRect, stringFormat); brush.Dispose(); } if (Focused) { Rectangle focusRect = buttonRect; focusRect.Inflate(-3, -3); ControlPaint.DrawFocusRectangle(e.Graphics, focusRect); } } // Draw the value if (mEdit == null) { Rectangle valueRect = ClientRectangle; valueRect.Width -= buttonRect.Width + 1; Color valueColor; if (mOwnerPropertyEnum.Property.Enabled == false) { valueColor = SystemColors.GrayText; } else { valueColor = mOwnerPropertyEnum.Property.Value.ForeColor; } mOwnerPropertyEnum.Property.Value.DrawValue(e.Graphics, valueRect, valueColor, mOwnerPropertyEnum, null); } base.OnPaint(e); }
public override void Draw(Graphics graphics, Rectangle itemRect, PropertyVisibleDeepEnumerator enumSelf) { // Fill the label background Rectangle labelColumnRect = GetLabelColumnRect(itemRect, enumSelf); ParentGrid.DrawManager.DrawPropertyLabelBackground(graphics, labelColumnRect, enumSelf); ParentGrid.DrawManager.DrawSeparationLines(graphics, itemRect, labelColumnRect, enumSelf); // Draw label text //---------------- // Set the font to underline Font underlinedFont = new Font(Font, FontStyle.Underline); Color textColor; // Choose the colors of the background and text regarding the item selection if (Selected) { if (ParentGrid.GridContainsFocus) { textColor = SystemColors.HighlightText; } else { textColor = SystemColors.WindowText; } } else // if not selected { if (!ParentGrid.Enabled && (ParentGrid.DisableMode != PropertyGrid.DisableModes.None) && ParentGrid.DisableModeGrayedOut) { textColor = ParentGrid.DisabledForeColor; } else { if (Enabled == false) { textColor = DisabledForeColor; } else { textColor = ForeColor; } } } labelColumnRect.X += ParentGrid.GlobalTextMargin; labelColumnRect.Width -= 2 * ParentGrid.GlobalTextMargin; labelColumnRect.Height--; // Draw the text, clipped by the given area Size textSize = Win32Calls.GetTextExtent(graphics, DisplayName, underlinedFont); if (textSize.Width <= labelColumnRect.Width) { Win32Calls.DrawText(graphics, DisplayName, ref labelColumnRect, underlinedFont, textColor, Win32Calls.DT_RIGHT | Win32Calls.DT_SINGLELINE | Win32Calls.DT_VCENTER | Win32Calls.DT_NOPREFIX); } else { Win32Calls.DrawText(graphics, DisplayName, ref labelColumnRect, underlinedFont, textColor, Win32Calls.DT_LEFT | Win32Calls.DT_SINGLELINE | Win32Calls.DT_VCENTER | Win32Calls.DT_END_ELLIPSIS | Win32Calls.DT_NOPREFIX); } underlinedFont.Dispose(); }
public override void Draw(Graphics graphics, Rectangle itemRect, PropertyVisibleDeepEnumerator enumSelf) { // Choose the colors of the text regarding the item selection Color textColor; if (Selected) { if (ParentGrid.GridContainsFocus) { textColor = ParentGrid.HighlightedTextColor; } else { textColor = ForeColor; } } else // if not selected { textColor = ForeColor; } // Draw the +/- sign if needed //---------------------------- if (ParentGrid.HasOneVisibleChild(enumSelf)) { ParentGrid.DrawManager.DrawPlusMinusSign(graphics, ParentGrid.Grid, itemRect, enumSelf); } // Draw a background for the label //-------------------------------- Rectangle rect = itemRect; rect.X += ParentGrid.LeftColumnWidth + 1; rect.Width -= ParentGrid.LeftColumnWidth + 1; ParentGrid.DrawManager.DrawCategoryLabelBackground(graphics, rect, enumSelf); // Draw the vertical separation between the columns and the bottom horizontal line //-------------------------------------------------------------------------------- Rectangle labelColumnRect = GetLabelColumnRect(itemRect, enumSelf); ParentGrid.DrawManager.DrawSeparationLines(graphics, itemRect, labelColumnRect, enumSelf); // Draw checkbox used to be able to manually disable/enable this category //----------------------------------------------------------------------- Win32Calls.SetClippingRect(graphics, labelColumnRect); Rectangle frameRect = GetManuallyDisableRect(itemRect, enumSelf); if (frameRect != Rectangle.Empty) { ParentGrid.DrawManager.DrawCheckBox(graphics, ParentGrid.Grid, frameRect, GetManuallyDisabledVariable(), true /* checkbox is enabled */); } // Draw the image if any //---------------------- Rectangle imgRect = GetImageRect(itemRect, enumSelf); if (imgRect != Rectangle.Empty) { ParentGrid.DrawImage(ImageIndex, graphics, imgRect.Left, imgRect.Top); } // Draw the category label //------------------------ Rectangle labelRect = GetLabelTextRect(itemRect, enumSelf); ParentGrid.DrawManager.DrawCategoryLabelText(graphics, labelRect, textColor, enumSelf); Win32Calls.ResetClippingRect(graphics); // Draw value text if any //----------------------- if (_valueText.Length > 0) { Rectangle valueRect = itemRect; int delta = ParentGrid.LeftColumnWidth + 1 + ParentGrid.LabelColumnWidth; valueRect.X += delta; valueRect.Width -= delta; valueRect.Inflate(-ParentGrid.GlobalTextMargin, 0); ParentGrid.DrawManager.DrawCategoryValue(graphics, valueRect, textColor, enumSelf); } }
private void StartHookMessages() { GC.KeepAlive(this); lock (this) { if (_keyboardHookHandle != IntPtr.Zero) { return; } // if (_thisProcessID == 0) // Win32Calls.GetWindowThreadProcessId(new HandleRef(_control, _control.Handle), out _thisProcessID); KeyboardHookObject obj = new KeyboardHookObject(this); Win32Calls.HookProc keyboardHookProc = new Win32Calls.HookProc(obj.Callback); _keyboardHookRoot = GCHandle.Alloc(keyboardHookProc); _keyboardHookHandle = Win32Calls.SetWindowsHookEx(Win32Calls.WH_KEYBOARD, keyboardHookProc, (IntPtr)0, Win32Calls.GetCurrentThreadId()); } }
protected override void OnPaint(PaintEventArgs e) { // Draw the background _ownerPropertyEnum.Property.ParentGrid.DrawManager.DrawPropertyValueBackground(e.Graphics, ClientRectangle, _ownerPropertyEnum); Rectangle buttonRect = GetButtonRect(e.Graphics); int listboxWidth = GetListboxWidth(); Color valueColor; if (_ownerPropertyEnum.Property.Enabled == false) { valueColor = SystemColors.GrayText; } else { if (Focused) { valueColor = SystemColors.HighlightText; } else { valueColor = _ownerPropertyEnum.Property.Value.ForeColor; } } // Draw the combobox arrow if (!ReadOnly) { if (ThemeRenderer.Enabled) { int state = (_pushed ? (_mouseOver ? ThemeComboBox.DropDownButtonPressed : ThemeComboBox.DropDownButtonHot) : (_mouseOver ? ThemeComboBox.DropDownButtonHot : ThemeComboBox.DropDownButtonNormal)); ThemeComboBox.DropDownButton.Draw(e.Graphics, state, buttonRect); } else { ControlPaint.DrawComboButton(e.Graphics, buttonRect, (_pushed && _mouseOver) ? ButtonState.Pushed : ButtonState.Normal); } if (Focused) { Rectangle focusRect = buttonRect; focusRect.Inflate(-3, -3); ControlPaint.DrawFocusRectangle(e.Graphics, focusRect); } } // Draw the listbox content Rectangle listRect = ClientRectangle; listRect.X = listRect.Right - listboxWidth; listRect.Width = listboxWidth - GetButtonWidth(null); Rectangle listRect2 = listRect; listRect2.Inflate(0, -1); listRect2.Width--; if (Focused) { e.Graphics.FillRectangle(SystemBrushes.FromSystemColor(SystemColors.Highlight), listRect2); ControlPaint.DrawFocusRectangle(e.Graphics, listRect2); } else { Brush listBrush = new SolidBrush(_ownerPropertyEnum.Property.ParentGrid.GridColor); e.Graphics.FillRectangle(listBrush, listRect2); listBrush.Dispose(); } Rectangle strRect = listRect; int margin = _ownerPropertyEnum.Property.ParentGrid.GlobalTextMargin; strRect.X += margin; strRect.Width = strRect.Right - strRect.Left - margin; strRect.Y++; strRect.Height--; Win32Calls.DrawText(e.Graphics, ListText, ref strRect, Font, valueColor, Win32Calls.DT_VCENTER | Win32Calls.DT_SINGLELINE | Win32Calls.DT_NOPREFIX); // Draw the value if (_edit == null) { Rectangle valueRect = ClientRectangle; valueRect.Width -= listboxWidth; _ownerPropertyEnum.Property.Value.DrawValue(e.Graphics, valueRect, valueColor, _ownerPropertyEnum, Text); } base.OnPaint(e); }