【问题标题】:WinForms Window size change (shrink) after calling Keyboard.GetKeyStates调用 Keyboard.GetKeyStates 后 WinForms 窗口大小更改(缩小)
【发布时间】:2021-11-25 10:54:00
【问题描述】:

使用 .NET Framework 4.7.1

调用 Keyboard.GetKeyStates 后框架或窗体窗口大小缩小 有什么理由吗?还是它的错误? 我正在检查 ctrl 是否关闭或在其他窗口上切换。

 if (((int)Keyboard.GetKeyStates(Key.LeftCtrl) == 1 || (int)Keyboard.GetKeyStates(Key.LeftCtrl) == 3))
    {
        Console.WriteLine("pressed");
    }

如图所示:

large image

after calling GetKeyStates

【问题讨论】:

  • 你的if里面的代码就是这样吗?那么没有理由改变表单的大小
  • 是的,只是调用函数时大小会发生变化,我不知道为什么!
  • System.Windows.Input 命名空间中的键盘类是否已打开,并且您尚未将项目设置为可识别 DPI?。如果是这样,您正在使用为 Winform 项目中的 WPF 应用程序设计的类。虽然这是允许的,但您正在触发一个副作用,即在访问该类时使您的应用程序被标记为 DPI 感知。使用 Microsoft.ACE.OLEDB 提供程序也会导致这种 DPI 感知变化(请参阅:When clicking "btnLogin", my forms decrease in size。)
  • stackoverflow.com/questions/48272782/… 是的,命名空间是 System.Windows.Input 我试过这个解决方案,取消注释 app.manifest 中的行,但不起作用。

标签: c# winforms .net-4.0


【解决方案1】:

作为 TnTinMn 评论它的 Dpi 感知问题,我找到了解决方案 here 还尝试从应用程序清单中更改 dpi 选项,但不起作用。

      static class Program
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
        //add this line 
[System.Runtime.InteropServices.DllImport("user32.dll")]
        private static extern bool SetProcessDPIAware();
        static void Main()
            {
        //add this line  
            if (Environment.OSVersion.Version.Major >= 6)
                SetProcessDPIAware();
    
            Application.EnableVisualStyles();
          Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
    
    
    }

【讨论】:

    猜你喜欢
    • 2012-12-02
    • 2013-12-22
    • 1970-01-01
    • 2013-09-17
    • 2020-09-09
    • 2014-09-01
    • 2016-07-07
    • 2021-08-29
    • 1970-01-01
    相关资源
    最近更新 更多