开发者

Programmatically set the DPI from a .net 2.0 WinForms application

开发者 https://www.devze.com 2023-01-01 16:05 出处:网络
I want to run my application on 96dpi, no matter what the dpi size from Windows is set to. It is possible ?

I want to run my application on 96dpi, no matter what the dpi size from Windows is set to. It is possible ?

' Edit ' I found that using the Scale() method and resizing the font will almost do the trick.

public class MyForm : Form
{
    private static bool ScaleDetected = false;
    const float DPI = 80F;

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        if (!ScaleDetected)
        {
            Graphics g = e.Graphics;
            float factorX = DPI / g.DpiX;
            float factorY = DPI / g.DpiY;

            SizeF newSize = new SizeF(factorX, factorY);

            AutoScaleDimensions = newSize;
            AutoScaleMode = AutoScaleMode.Dpi;

            Scale(newSize);
            Font = new Font(Font.FontFamily, Font.Size * factorX);

            ScaleDetected = true;
        }
    }
}

Programmatically set the DPI from a .net 2.0 WinForms application

However when using this 'trick' in a MDI application using Janus Con开发者_JAVA技巧trols, the main form is resized, but for some other forms, the scaling + changed font are not applied.


If I understand correctly you want to disable the automatic DPI scaling. If so, I think that you just need to call SetProcessDPIAware to tell Windows that you'll handle it yourself.

See this link for how to call it from C#/VB.Net:
http://www.pinvoke.net/default.aspx/user32/setprocessdpiaware.html


You should set the AutoScaleMode to AutoScaleMode.None to prevent automatic scaling

0

精彩评论

暂无评论...
验证码 换一张
取 消