80 lines
2.8 KiB
C#
80 lines
2.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.Drawing;
|
|
using System.Windows.Forms;
|
|
using System.Runtime.InteropServices;
|
|
|
|
namespace DevComponents.DotNetBar.Layout
|
|
{
|
|
internal static class Helpers
|
|
{
|
|
public static Size Max(Size s1, Size s2)
|
|
{
|
|
return new Size(Math.Max(s1.Width, s2.Width), Math.Max(s1.Height, s2.Height));
|
|
}
|
|
public static Rectangle Deflate(Rectangle r, System.Windows.Forms.Padding padding)
|
|
{
|
|
r.X += padding.Left;
|
|
r.Width -= padding.Horizontal;
|
|
r.Y += padding.Top;
|
|
r.Height -= padding.Vertical;
|
|
|
|
return r;
|
|
}
|
|
public static Rectangle Deflate(Rectangle r, DevComponents.DotNetBar.Padding padding)
|
|
{
|
|
r.X += padding.Left;
|
|
r.Width -= padding.Horizontal;
|
|
r.Y += padding.Top;
|
|
r.Height -= padding.Vertical;
|
|
|
|
return r;
|
|
}
|
|
|
|
public static int GetTextBaseline(Control ctrl, ContentAlignment alignment)
|
|
{
|
|
return GetTextBaseline(ctrl, ctrl.Font, alignment);
|
|
}
|
|
|
|
public static int GetTextBaseline(Control ctrl, Font font, ContentAlignment alignment)
|
|
{
|
|
Rectangle clientRect = ctrl.ClientRectangle;
|
|
int ascent = 0;
|
|
int height = 0;
|
|
using (Graphics g = ctrl.CreateGraphics())
|
|
{
|
|
IntPtr hdc = g.GetHdc();
|
|
IntPtr hFont = font.ToHfont();
|
|
try
|
|
{
|
|
IntPtr oldObject = WinApi.SelectObject(hdc, hFont);
|
|
WinApi.TEXTMETRIC tm = new WinApi.TEXTMETRIC();
|
|
WinApi.GetTextMetrics(new HandleRef(ctrl, hdc), tm);
|
|
ascent = tm.tmAscent + 1;
|
|
height = tm.tmHeight;
|
|
WinApi.SelectObject(hdc, oldObject);
|
|
}
|
|
finally
|
|
{
|
|
WinApi.DeleteObject(hFont);
|
|
g.ReleaseHdc(hdc);
|
|
}
|
|
}
|
|
if ((alignment & (ContentAlignment.TopRight | ContentAlignment.TopCenter | ContentAlignment.TopLeft)) != (ContentAlignment)0)
|
|
{
|
|
return (clientRect.Top + ascent);
|
|
}
|
|
if ((alignment & (ContentAlignment.MiddleRight | ContentAlignment.MiddleCenter | ContentAlignment.MiddleLeft)) != (ContentAlignment)0)
|
|
{
|
|
return (((clientRect.Top + (clientRect.Height / 2)) - (height / 2)) + ascent);
|
|
}
|
|
return ((clientRect.Bottom - height) + ascent);
|
|
}
|
|
public static bool IsEmpty(System.Windows.Forms.Padding p)
|
|
{
|
|
return p.Left == 0 && p.Right == 0 && p.Top == 0 && p.Bottom == 0;
|
|
}
|
|
}
|
|
}
|