Example #1
0
        /// <include file='doc\COM2FontConverter.uex' path='docs/doc[@for="Com2FontConverter.ConvertManagedToNative"]/*' />
        /// <devdoc>
        ///     Converts the managed value into a native value
        /// </devdoc>
        public override object ConvertManagedToNative(object managedValue, Com2PropertyDescriptor pd, ref bool cancelSet)
        {
            // we default to black.
            //
            if (managedValue == null)
            {
                managedValue = Control.DefaultFont;
            }

            cancelSet = true;

            if (lastFont != null && lastFont.Equals(managedValue))
            {
                // don't do anything here.
                return(null);
            }

            lastFont = (Font)managedValue;
            UnsafeNativeMethods.IFont nativeFont = (UnsafeNativeMethods.IFont)pd.GetNativeValue(pd.TargetObject);

            // now, push all the values into the native side
            if (nativeFont != null)
            {
                bool changed = ControlPaint.FontToIFont(lastFont, nativeFont);

                if (changed)
                {
                    // here, we want to pick up a new font from the handle
                    lastFont = null;
                    ConvertNativeToManaged(nativeFont, pd);
                }
            }
            return(null);
        }
            => - 1;    // not a value type, so use -1

            public IntPtr MarshalManagedToNative(object obj)
            {
                Font font = (Font)obj;

                NativeMethods.tagFONTDESC fontDesc = new NativeMethods.tagFONTDESC();
                NativeMethods.LOGFONTW    logFont  = NativeMethods.LOGFONTW.FromFont(font);

                fontDesc.lpstrName      = font.Name;
                fontDesc.cySize         = (long)(font.SizeInPoints * 10000);
                fontDesc.sWeight        = (short)logFont.lfWeight;
                fontDesc.sCharset       = logFont.lfCharSet;
                fontDesc.fItalic        = font.Italic;
                fontDesc.fUnderline     = font.Underline;
                fontDesc.fStrikethrough = font.Strikeout;

                Guid iid = typeof(UnsafeNativeMethods.IFont).GUID;

                UnsafeNativeMethods.IFont oleFont = UnsafeNativeMethods.OleCreateFontIndirect(fontDesc, ref iid);
                IntPtr pFont = Marshal.GetIUnknownForObject(oleFont);

                int hr = Marshal.QueryInterface(pFont, ref iid, out IntPtr pIFont);

                Marshal.Release(pFont);

                if (NativeMethods.Failed(hr))
                {
                    Marshal.ThrowExceptionForHR(hr);
                }

                return(pIFont);
            }
Example #3
0
        /// <include file='doc\COM2FontConverter.uex' path='docs/doc[@for="Com2FontConverter.ConvertNativeToManaged"]/*' />
        /// <devdoc>
        ///     Converts the native value into a managed value
        /// </devdoc>
        public override object ConvertNativeToManaged(object nativeValue, Com2PropertyDescriptor pd)
        {
            // we're getting an IFont thing here
            UnsafeNativeMethods.IFont nativeFont = nativeValue as UnsafeNativeMethods.IFont;

            if (nativeFont == null)
            {
                lastHandle = IntPtr.Zero;
                lastFont   = Control.DefaultFont;
                return(lastFont);
            }

            IntPtr fontHandle = nativeFont.GetHFont();

            // see if we have this guy cached
            if (fontHandle == lastHandle && lastFont != null)
            {
                return(lastFont);
            }

            lastHandle = fontHandle;

            try {
                // this wasn't working because it was converting everything to
                // world units.
                //
                Font font = Font.FromHfont(lastHandle);
                try {
                    lastFont = ControlPaint.FontInPoints(font);
                }
                finally {
                    font.Dispose();
                }
            }
            catch (ArgumentException) {
                // we will fail on non-truetype fonts, so
                // just use the default font.
                lastFont = Control.DefaultFont;
            }

            return(lastFont);
        }
            public object MarshalNativeToManaged(IntPtr pObj)
            {
                UnsafeNativeMethods.IFont nativeFont = (UnsafeNativeMethods.IFont)Marshal.GetObjectForIUnknown(pObj);
                IntPtr hfont = nativeFont.GetHFont();

                Font font;

                try
                {
                    font = Font.FromHfont(hfont);
                }
                catch (Exception e)
                {
                    if (ClientUtils.IsSecurityOrCriticalException(e))
                    {
                        throw;
                    }

                    font = DefaultFont;
                }

                return(font);
            }