Programmatically set the DPI from a .net 2.0 WinForms application
Posted
by Stef
on Stack Overflow
See other posts from Stack Overflow
or by Stef
Published on 2010-05-26T12:32:54Z
Indexed on
2010/05/27
9:51 UTC
Read the original article
Hit count: 930
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;
}
}
}
However when using this 'trick' in a MDI application using Janus Controls, the main form is resized, but for some other forms, the scaling + changed font are not applied.
© Stack Overflow or respective owner