Windows Forms window changes its size when I create a WPF window

后端 未结 3 1338
别跟我提以往
别跟我提以往 2020-12-02 01:46

I have a System.Window.Forms.Form where I handle every button click. When I receive the first event I create a new WPF System.Windows.Window object

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-02 02:28

    These is some method you can use. Set the width and height of the window:

    public static Graphics graphics = Graphics.FromHwnd(IntPtr.Zero);
    public static double GetDIPIndependent(double value, double DPI)
    {
        value = value * (graphics.DpiX / DPI);
        return value;
    }
    public static double GetDIPDependent(double value, double DPI)
    {
        value = value * (DPI / graphics.DpiX);
        return value;
    }
    public static double GetDIPIndependent(double value)
    {
        value = value * (graphics.DpiX / 96.0);
        return value;
    }
    
    public static double GetDIPDependent(double value)
    {
        value = value * (96.0 / graphics.DpiX);
        return value;
    }
    

    Example:

    If DPI independent

    this.Width = GetDIPIndependent(this.Width)
    this.Height = GetDIPIndependent(this.Height)
    

    If DPI dependent

    this.Width = GetDIPDependent(this.Width)
    this.Height = GetDIPDependent(this.Height)
    

    It works for me...

提交回复
热议问题