예제 #1
0
        public void ParkingWindow_SystemAware()
        {
            // run tests only on Windows 10 versions that support thread dpi awareness.
            if (!PlatformDetection.IsWindows10Version1803OrGreater)
            {
                return;
            }

            // set thread awareness context to PermonitorV2(PMv2).
            // if process/thread is not in PMv2, calling 'EnterDpiAwarenessScope' is a no-op and that is by design.
            // In this case, we will be setting thread to PMv2 mode and then scope to UNAWARE
            IntPtr originalAwarenessContext = User32.SetThreadDpiAwarenessContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2);

            try
            {
                using (DpiHelper.EnterDpiAwarenessScope(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE))
                {
                    using var control = new Control();
                    ThreadContext ctx = GetContextForHandle(new HandleRef(this, control.Handle));
                    Assert.NotNull(ctx);
                    ParkingWindow parkingWindow = ctx.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE);
                    Assert.NotNull(parkingWindow);

                    IntPtr dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle);
                    Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE, dpiContext));
                }
            }
            finally
            {
                // reset back to original awareness context.
                User32.SetThreadDpiAwarenessContext(originalAwarenessContext);
            }
        }
예제 #2
0
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            ArgumentNullException.ThrowIfNull(provider);

            if (!provider.TryGetService(out IWindowsFormsEditorService editorService))
            {
                throw new InvalidOperationException($"Service provider couldn't fetch {nameof(IWindowsFormsEditorService)}.");
            }

            IUIService uiService = provider.GetService <IUIService>();
            IComponent component = context.Instance as IComponent;

            using (DpiHelper.EnterDpiAwarenessScope(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE))
            {
                _builderDialog ??= new DataGridViewCellStyleBuilder(provider, component);
                if (uiService is not null)
                {
                    _builderDialog.Font = (Font)uiService.Styles["DialogFont"];
                }

                if (value is DataGridViewCellStyle style)
                {
                    _builderDialog.CellStyle = style;
                }

                _builderDialog.Context = context;
                if (_builderDialog.ShowDialog() == DialogResult.OK)
                {
                    value = _builderDialog.CellStyle;
                }
            }

            return(value);
        }
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            if (provider is null)
            {
                throw new ArgumentNullException(nameof(provider));
            }

            IWindowsFormsEditorService edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));

            if (edSvc is null)
            {
                throw new InvalidOperationException("Service provider couldn't fetch " + nameof(edSvc));
            }

            IUIService uiService = (IUIService)provider.GetService(typeof(IUIService));
            IComponent comp      = context.Instance as IComponent;

            using (DpiHelper.EnterDpiAwarenessScope(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE))
            {
                _builderDialog ??= new DataGridViewCellStyleBuilder(provider, comp);
                if (uiService != null)
                {
                    _builderDialog.Font = (Font)uiService.Styles["DialogFont"];
                }

                if (value is DataGridViewCellStyle dgvcs)
                {
                    _builderDialog.CellStyle = dgvcs;
                }

                _builderDialog.Context = context;
                if (_builderDialog.ShowDialog() == DialogResult.OK)
                {
                    value = _builderDialog.CellStyle;
                }
            }

            return(value);
        }