Exemplo n.º 1
0
        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;
            Ole32.IFont nativeFont = (Ole32.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);
        }
Exemplo n.º 2
0
            public IntPtr MarshalManagedToNative(object obj)
            {
                Font font = (Font)obj;

                User32.LOGFONTW logFont  = User32.LOGFONTW.FromFont(font);
                var             fontDesc = new Oleaut32.FONTDESC
                {
                    cbSizeOfStruct = (uint)Marshal.SizeOf <Oleaut32.FONTDESC>(),
                    lpstrName      = font.Name,
                    cySize         = (long)(font.SizeInPoints * 10000),
                    sWeight        = (short)logFont.lfWeight,
                    sCharset       = logFont.lfCharSet,
                    fItalic        = font.Italic.ToBOOL(),
                    fUnderline     = font.Underline.ToBOOL(),
                    fStrikethrough = font.Strikeout.ToBOOL(),
                };
                Guid iid = typeof(Ole32.IFont).GUID;

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

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

                Marshal.Release(pFont);

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

                return(pIFont);
            }
Exemplo n.º 3
0
 public object MarshalNativeToManaged(IntPtr pObj)
 {
     Ole32.IFont nativeFont = (Ole32.IFont)Marshal.GetObjectForIUnknown(pObj);
     try
     {
         return(Font.FromHfont(nativeFont.hFont));
     }
     catch (Exception e) when(!ClientUtils.IsCriticalException(e))
     {
         return(DefaultFont);
     }
 }