2

I got "LogInform" form. which has some labels, two text boxes (username and password). and a button "log in". size of form is (1149, 847).

When a user clicks on the button I have a SQL function that checks if the user exists in the database.

//log in to user
private void bunifuThinButton21_Click(object sender, EventArgs e)
{
    if(ConsoleApp2.UsersDB.userExists(this.userName.Text,this.passWord.Text))
    { 
        this.Hide();          
    }
    else
    {
        bunifuThinButton22.Visible = false;
        wrongUser.Visible = true;
        timer2.Enabled = true; 
    }
}

and the SQL function (userExists) :

public static bool userExists(string userName,string passWord)
{
    DataTable d1;
    string com = "SELECT * FROM users where user_name='" + userName + "'AND user_password='"+passWord+"'";
    d1 = oledbhelper.GetTable(com);

    if (d1.Rows.Count == 0)
    {
        return false;
    }

    return true;
}

After the click everything goes like it should except for one thing - The window gets smaller by itself, there is no code that makes the window smaller after the click. I don't understand why it does it. For those who don't understand what I mean, I made 20 sec video(where I start the form and put in some incorrect names and passwords and then press the button): youtube vid

I tried to set the "MinumumSize" with the regular size numbers, I tried to set "AutoSize" to false, I tried to set the Size of the window after it gets smaller(logInForm.size = new size(x,y)), I tried every possible way! I cant understand why it does it ); thanks for the help, hope I explained well enough..

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Daniel
  • 21
  • 2
  • 3
    Odd - this exact issue came up yesterday. Same answers apply - its changing size because of something being done – BugFinder Oct 20 '17 at 10:52
  • I had [a similar issue some month ago](https://github.com/oozcitak/imagelistview/issues/188) which was resolved by _not_ using [WIC](https://en.wikipedia.org/wiki/Windows_Imaging_Component) anymore. Also calling the [`SetProcessDPIAware`](https://msdn.microsoft.com/en-us/library/windows/desktop/ms633543(v=vs.85).aspx) function [might help](https://github.com/oozcitak/imagelistview/issues/188#issuecomment-287047672). – Uwe Keim Oct 20 '17 at 11:15
  • 1
    Just ensure you declared your app to be dpiAware up front so you can't get surprised later. https://stackoverflow.com/a/13228495/17034 – Hans Passant Oct 20 '17 at 11:21

1 Answers1

3

Form size changes when you have high-DPI monitor with layout scaling enabled in Windows display settings. Winforms DPI autoscaling gets reset when your application loads any WPF-targeted DLL into memory (check Visual Studio output console at the time when it happens). For example, when PresentationCore.dll or PresentationFramework.dll are loaded, the scaling gets reset.

You can create app.manifest file overriding dpiAwareness setting:

<application xmlns="urn:schemas-microsoft-com:asm.v3">
  <windowsSettings>
    <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">false</dpiAware>
  </windowsSettings>
</application>

...but this won't play together with ClickOnce manifest:

Installing ClickOnce published applications is not possible if there is a tag in the manifest file about the dpi-awareness. This is not something that ClickOnce explicitly supports. So you may need to exclude the DPI-aware part for ClickOnce deployment.

Solution is to add DisableDpiAwareness attribute to your Properties/AssemblyInfo.cs file:

// Disable Dpi awareness in the application assembly.
// Add reference to WindowsBase.dll
[assembly: System.Windows.Media.DisableDpiAwareness]
jtmnt
  • 746
  • 7
  • 12