/// <summary><seealso cref="OptimalWidth"/> /// Calculates the preferred width on a per-tab basis. /// </summary> private void CalcPreferredWidth(TabBarTab tab) { IntPtr hdc = (IntPtr)0; try { if (IsHandleCreated) { hdc = Win32Declarations.GetDC(Handle); } else { hdc = Win32Declarations.GetDC((IntPtr)0); } IntPtr oldFont = Win32Declarations.SelectObject(hdc, _fontHandle); Win32Declarations.SelectObject(hdc, oldFont); SIZE sz = new SIZE(); Win32Declarations.GetTextExtentPoint32(hdc, tab.Text, tab.Text.Length, ref sz); int prefWidth = sz.cx + 2 * 8; tab.PreferredWidth = Math.Max(50, prefWidth); tab.Width = tab.PreferredWidth; } finally { if (hdc != (IntPtr)0) { Win32Declarations.ReleaseDC(Handle, hdc); hdc = (IntPtr)0; } } }
private void ProcessWmNcPaint(ref Message message) { // GetDCEx usually returns 0, so just get the windows DC var hdc = GetDCEx(message.HWnd, message.WParam, DCX_WINDOW | DCX_INTERSECTRGN); if (hdc == IntPtr.Zero) { hdc = GetWindowDC(message.HWnd); } try { using (var graphics = Graphics.FromHdc(hdc)) { var windowRectangle = new Rectangle(0, 0, Width, Height); var clientRectangle = OnCalculateNonClientSize(windowRectangle); graphics.ExcludeClip(clientRectangle); OnPaintNonClient(graphics, windowRectangle); } } finally { Win32Declarations.ReleaseDC(message.HWnd, hdc); } }
public Size MeasureText(string text, Font font) { IntPtr hdc = Win32Declarations.GetDC(IntPtr.Zero); try { return(MeasureText(hdc, font, text)); } finally { Win32Declarations.ReleaseDC(IntPtr.Zero, hdc); } }
/// <summary> /// Get the size on the screen HDC /// </summary> /// <returns></returns> public Size GetSize() { IntPtr hDC = Win32Declarations.GetDC(IntPtr.Zero); try { return(GetSize(hDC).ToSize()); } finally { Win32Declarations.ReleaseDC(IntPtr.Zero, hDC); } }
protected override void WndProc(ref Message m) { if (m.Msg == Win32Declarations.WM_NCPAINT && BorderStyle == BorderStyle.FixedSingle) { IntPtr hdc = Win32Declarations.GetWindowDC(Handle); IntPtr brush = Win32Declarations.CreateSolidBrush(Win32Declarations.ColorToRGB(_borderColor)); IntPtr oldBrush = Win32Declarations.SelectObject(hdc, brush); RECT rect = new RECT(0, 0, Width, Height); Win32Declarations.FrameRect(hdc, ref rect, brush); Win32Declarations.SelectObject(hdc, oldBrush); Win32Declarations.DeleteObject(brush); Win32Declarations.ReleaseDC(Handle, hdc); m.Result = IntPtr.Zero; } else { base.WndProc(ref m); } }