0

using .NET Framework 4.7.1

the frame or form Window size shrink after calling Keyboard.GetKeyStates Is there any reason for that ? or its a bug ? I am checking if ctrl is down or toggle on other window .

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

As can be seen in the pictures:

large image

after calling GetKeyStates

  • Is the code inside your _if_ just that? No reason then for it changing the form's size – Steve Nov 25 '21 at 10:57
  • yes its just that , the size change on calling the function only and I don't know why ! – PH Mohmmed Alaa Nov 25 '21 at 11:47
  • Is Keyboard class the on from the System.Windows.Input namespace and you have not set up the project to be DPI-aware?. If so, you are using a class designed for a WPF application in a Winform project. While that is permissible, you are triggering a side-effect of making your application being marked as DPI aware on accessing the class. Using the Microsoft.ACE.OLEDB provider is also know to cause this DPI awareness change (see: [When clicking "btnLogin", my forms decrease in size](https://stackoverflow.com/questions/48272782/when-clicking-btnlogin-my-forms-decrease-in-size).) – TnTinMn Nov 25 '21 at 19:00
  • https://stackoverflow.com/questions/48272782/when-clicking-btnlogin-my-forms-decrease-in-size#:~:text=%3C!%2D%2D%0A%3Capplication%20xmlns%3D%22urn%3Aschemas%2Dmicrosoft%2Dcom%3Aasm.v3%22%3E%0A%20%20%3CwindowsSettings%3E%0A%20%20%20%20%3CdpiAware%20xmlns%3D%22http%3A//schemas.microsoft.com/SMI/2005/WindowsSettings%22%3Etrue%3C/dpiAware%3E%0A%20%20%3C/windowsSettings%3E%0A%3C/application%3E%0A%2D%2D%3E yes the name space is System.Windows.Input I tried this solution uncommented the lines in app.manifest , not working . – PH Mohmmed Alaa Nov 27 '21 at 16:35

1 Answers1

0

as TnTinMn comment its Dpi aware problem ,I have found the solution here also tried to change the dpi option from app manifest ,did not work .

      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());
            }
    
    
    }