/* Fills the specified buffer with the metrics for the currently selected font. */
        internal static int GetTextMetrics(HandleRef hDC, ref NativeMethods.TEXTMETRIC lptm)
        {
            if (Marshal.SystemDefaultCharSize != 1)
            {
                // Handle Unicode
                return(SafeNativeMethods.GetTextMetricsW(hDC, out lptm));
            }

            // Handle ANSI; call GetTextMetricsA and translate to Unicode struct
            NativeMethods.TEXTMETRICA tEXTMETRICA = new NativeMethods.TEXTMETRICA();
            int result = SafeNativeMethods.GetTextMetricsA(hDC, out tEXTMETRICA);

            lptm.tmHeight           = tEXTMETRICA.tmHeight;
            lptm.tmAscent           = tEXTMETRICA.tmAscent;
            lptm.tmDescent          = tEXTMETRICA.tmDescent;
            lptm.tmInternalLeading  = tEXTMETRICA.tmInternalLeading;
            lptm.tmExternalLeading  = tEXTMETRICA.tmExternalLeading;
            lptm.tmAveCharWidth     = tEXTMETRICA.tmAveCharWidth;
            lptm.tmMaxCharWidth     = tEXTMETRICA.tmMaxCharWidth;
            lptm.tmWeight           = tEXTMETRICA.tmWeight;
            lptm.tmOverhang         = tEXTMETRICA.tmOverhang;
            lptm.tmDigitizedAspectX = tEXTMETRICA.tmDigitizedAspectX;
            lptm.tmDigitizedAspectY = tEXTMETRICA.tmDigitizedAspectY;
            lptm.tmFirstChar        = Convert.ToChar(tEXTMETRICA.tmFirstChar);
            lptm.tmLastChar         = Convert.ToChar(tEXTMETRICA.tmLastChar);
            lptm.tmDefaultChar      = Convert.ToChar(tEXTMETRICA.tmDefaultChar);
            lptm.tmBreakChar        = Convert.ToChar(tEXTMETRICA.tmBreakChar);
            lptm.tmItalic           = tEXTMETRICA.tmItalic;
            lptm.tmUnderlined       = tEXTMETRICA.tmUnderlined;
            lptm.tmStruckOut        = tEXTMETRICA.tmStruckOut;
            lptm.tmPitchAndFamily   = tEXTMETRICA.tmPitchAndFamily;
            lptm.tmCharSet          = tEXTMETRICA.tmCharSet;

            return(result);
        }
        private static int GetBaseline(Control ctrl, ContentAlignment alignment)
        {
            int textAscent = 0;
            int textHeight = 0;

            // Retrieve the ClientRect of the Control
            Rectangle clientRect = ctrl.ClientRectangle;

            // Create a Graphics object for the Control
            using (Graphics graphics = ctrl.CreateGraphics())
            {
                // Retrieve the device context Handle
                IntPtr hDC = graphics.GetHdc();

                // Create a wrapper for the Font of the Control
                Font      controlFont    = ctrl.Font;
                HandleRef tempFontHandle = new HandleRef(controlFont, controlFont.ToHfont());

                try
                {
                    // Create a wrapper for the device context
                    HandleRef deviceContextHandle = new HandleRef(ctrl, hDC);

                    // Select the Font into the device context
                    IntPtr originalFont = SafeNativeMethods.SelectObject(deviceContextHandle, tempFontHandle);

                    // Create a TEXTMETRIC and calculate metrics for the selected Font
                    NativeMethods.TEXTMETRIC tEXTMETRIC = new NativeMethods.TEXTMETRIC();
                    if (SafeNativeMethods.GetTextMetrics(deviceContextHandle, ref tEXTMETRIC) != 0)
                    {
                        textAscent = (tEXTMETRIC.tmAscent + 1);
                        textHeight = tEXTMETRIC.tmHeight;
                    }

                    // Restore original Font
                    HandleRef originalFontHandle = new HandleRef(ctrl, originalFont);
                    SafeNativeMethods.SelectObject(deviceContextHandle, originalFontHandle);
                }
                finally
                {
                    // Cleanup tempFont
                    SafeNativeMethods.DeleteObject(tempFontHandle);

                    // Release device context
                    graphics.ReleaseHdc(hDC);
                }
            }

            // Calculate return value based on the specified alignment; first check top alignment
            if ((alignment & (ContentAlignment.TopLeft | ContentAlignment.TopCenter | ContentAlignment.TopRight)) != 0)
            {
                return(clientRect.Top + textAscent);
            }

            // Check middle alignment
            if ((alignment & (ContentAlignment.MiddleLeft | ContentAlignment.MiddleCenter | ContentAlignment.MiddleRight)) == 0)
            {
                return((clientRect.Bottom - textHeight) + textAscent);
            }

            // Assume bottom alignment
            return((int)Math.Round((double)clientRect.Top + (double)clientRect.Height / 2 - (double)textHeight / 2 + (double)textAscent));
        }
 private static extern int GetTextMetricsW(HandleRef hDC, out NativeMethods.TEXTMETRIC lptm);