在显示我的 C# windows 窗体时关闭“拖动时显示窗口内容"设置 [英] Turn off 'Show window contents while dragging' setting while displaying my C# windows forms

查看:100
本文介绍了在显示我的 C# windows 窗体时关闭“拖动时显示窗口内容"设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个窗口显示设置,称为拖动时显示窗口内容".

There is a window display setting called 'Show window contents while dragging'.

http://www.thewindowsclub.com/disable-show-windows-contents-while-dragging

启用此设置后,如果您拖动一个窗口,该窗口将立即在新位置重新绘制.如果您调整窗口大小,它会立即为每个新的窗口大小重新绘制窗口,即使您仍然按住鼠标按钮也是如此.

When this setting is turned on, if you drag a window the window will immediately repaint at the new position. If you resize a window, it will repaint the window for each new window size immediately, even if you are still holding down the mouse button.

当设置关闭时,拖动窗口或调整窗口大小只会显示新窗口位置或大小的轮廓,直到您松开鼠标按钮,然后它将在新位置或大小上绘制窗口.

When the setting is turned off, dragging or resizing the window simply shows an outline of the new window position or size until you release the mouse button and then it will paint the window at the new position or size.

我想在我的 C# WinForms 应用程序中显示我的表单,并关闭拖动时显示窗口内容"设置.然而,由于这是一个操作设置,我只希望它在我的表单显示时有效,而不管操作系统中的设置是什么.

I would like to display my forms in my C# WinForms application with 'Show window contents while dragging' setting turned off. However as this is an operating setting, I would only like this to be effective for when my forms are displayed regardless of what the setting is set to in the OS.

有没有办法使用一些 WinAPI 调用来改变我的 winforms 的行为?

Is there a way to achieve this using some WinAPI calls to change the behavior specifically for my winforms?

如果没有,有没有办法在我的表单显示之前以编程方式更改设置并在我的表单关闭后重置它?执行此更改是否需要管理员权限(因为我不想要)?

If not, is there a way that I can change the setting programatically before my form is displayed and reset it after my form has closed? Would performing this change require admin rights (because I don't want that)?

推荐答案

下面的代码将使用系统设置的'Show window content while dragging'进行窗口重新定位,同时会暂时设置为关闭,而调整窗口大小,然后将其设置回系统设置.

The following code below will use the system setting of 'Show window content while dragging' for window re-positioning, while it will temporarily set it to off, while resizing the window and then set it back to the system setting.

这使您可以在 Windows 窗体中自由地调整窗体大小.

This gives you flicker free form resizing in windows forms.

由于此属性是修改 HKEY_CURRENT_USER\Control Panel\Desktop\DragFullWindows 注册表项的用户设置,因此它不需要管理员权限.

Since this property is a user setting that modifies the HKEY_CURRENT_USER\Control Panel\Desktop\DragFullWindows registry key, it should not require admin rights.

    [DllImport("user32.dll", EntryPoint = "SystemParametersInfo", CharSet = CharSet.Auto)]
    public static extern int GetSystemParametersInfo(int uAction, int uParam, out int lpvParam, int fuWinIni);

    [DllImport("user32.dll", EntryPoint = "SystemParametersInfo", CharSet = CharSet.Auto)]
    public static extern int SetSystemParametersInfo(int uAction, int uParam, int lpvParam, int fuWinIni);

    private const int SPI_GETDRAGFULLWINDOWS = 38;
    private const int SPI_SETDRAGFULLWINDOWS = 37;

    private const int WM_SYSCOMMAND = 0x0112;
    private const int SC_SIZE = 0xF000; 

    //change 'Show window content while dragging' to false while resizing
    protected override void WndProc(ref Message m)
    {
        if (m.Msg == WM_SYSCOMMAND && (m.WParam.ToInt32() & 0xfff0) == SC_SIZE)
        {
            int isDragFullWindow;
            GetSystemParametersInfo(SPI_GETDRAGFULLWINDOWS, 0, out isDragFullWindow, 0);

            if (isDragFullWindow != 0)
                SetSystemParametersInfo(SPI_SETDRAGFULLWINDOWS, 0, 0, 0);

            base.WndProc(ref m);

            if (isDragFullWindow != 0)
                SetSystemParametersInfo(SPI_SETDRAGFULLWINDOWS, 1, 0, 0);
        }
        else
        {
            base.WndProc(ref m);
        }
    } 

    //reduce control flickering and black stripes when window is resized
    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams cp = base.CreateParams;
            cp.ExStyle |= 0x02000000;  // Turn on WS_EX_COMPOSITED
            return cp;
        }
    }

这篇关于在显示我的 C# windows 窗体时关闭“拖动时显示窗口内容"设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆