Archive Browser
Download DesignerSnapLinesDemo.zip, last updated 19/04/2019 (8.39 KB)
Download- md5: 85fd7e0029eb045c195ad6eafab1a2f1
- sha1: 4deddf404012c1456079d45d3072d41eb7c84495
- sha256: d613c6fb34461e57632fceb6bdbd48ea1b4cb050f151d16d4a94c5c6a5446022
using System;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.Windows.Forms.Design.Behavior;
namespace DesignerSnapLinesDemo.Design
{
internal class BetterTextBoxDesigner : ControlDesigner
{
#region Properties
public override SelectionRules SelectionRules
{
get { return SelectionRules.Visible | SelectionRules.Moveable | SelectionRules.LeftSizeable | SelectionRules.RightSizeable; }
}
public override IList SnapLines
{
get
{
IList snapLines;
int textBaseline;
SnapLine snapLine;
BetterTextBox control;
control = (BetterTextBox)this.Control;
snapLines = base.SnapLines;
textBaseline = this.GetFontAscent(control) + 1;
textBaseline += 2; // offset for the borders of the control
textBaseline += 2; // inner margin
snapLine = new SnapLine(SnapLineType.Baseline, textBaseline, SnapLinePriority.Medium);
// Although Resharper thinks this can be a null value, the default implemention
// *always* adds snaplines detailing the margins of the control. Therefore I can
// happily disable this warning - it's not likely the behaviour will change
// ReSharper disable once PossibleNullReferenceException
snapLines.Add(snapLine);
return snapLines;
}
}
#endregion
#region Methods
private int GetFontAscent(Control control)
{
using (Graphics graphics = control.CreateGraphics())
{
return this.GetFontAscent(graphics, control.Font);
}
}
private int GetFontAscent(IDeviceContext dc, Font font)
{
int result;
IntPtr hDC;
IntPtr hFont;
IntPtr hFontDefault;
hDC = IntPtr.Zero;
hFont = IntPtr.Zero;
hFontDefault = IntPtr.Zero;
try
{
NativeMethods.TEXTMETRICW textMetric;
hDC = dc.GetHdc();
hFont = font.ToHfont();
hFontDefault = NativeMethods.SelectObject(hDC, hFont);
NativeMethods.GetTextMetrics(hDC, out textMetric);
result = textMetric.tmAscent;
}
finally
{
if (hFontDefault != IntPtr.Zero)
{
NativeMethods.SelectObject(hDC, hFontDefault);
}
if (hFont != IntPtr.Zero)
{
NativeMethods.DeleteObject(hFont);
}
dc.ReleaseHdc();
}
return result;
}
#endregion
}
}
Donate
This software may be used free of charge, but as with all free software there are costs involved to develop and maintain.
If this site or its services have saved you time, please consider a donation to help with running costs and timely updates.
Donate