Solution for WPF to block the impact of DPI changes on programs

Source: Internet
Author: User
Converter:
[Valueconversion (typeof (object), typeof (object)]
Public class dpiconverter: ivalueconverter
{
Public object convert (object value, type targettype, object parameter, cultureinfo Culture)
{
Export winterophelper winhelper = new export winterophelper (mainwindow) app. Current. mainwindow );
Intptr main1_whandle = winhelper. Handle;
Graphics currentgraphics = graphics. fromhwnd (main1_whandle );
Double currentdpix = currentgraphics. dpix;
Double dpixratio = currentdpix/96;

If (targettype = typeof (gridlength ))
{
Double Data = double. parse (parameter. tostring ());

Gridlength = new gridlength (data/dpixratio, gridunittype. pixel );
Return gridlength;
}
If (targettype = typeof (thickness ))
{
Thickness margin = (thickness) parameter;
If (margin! = NULL)
{
Margin. Top = margin. Top/dpixratio;
Margin. Left = margin. Left/dpixratio;
Margin. Right = margin. Right/dpixratio;
Margin. Bottom = margin. Bottom/dpixratio;
Return margin;
}
}
If (targettype = typeof (double ))
{
Double fontsize = double. parse (parameter. tostring ());
Return fontsize/dpixratio;
}
If (targettype = typeof (system. Windows. Point ))
{
System. Windows. Point point = (system. Windows. Point) parameter;
Point. x = point. X/dpixratio;
Point. Y = point. Y/dpixratio;
Return Point;
}
Return NULL;
}

Public object convertback (object value, type targettype, object parameter, cultureinfo Culture)
{
Return NULL;
}
}

Xmal code:
<Image name = "imageicon" stretch = "fill" horizontalalignment = "Left" verticalignment = "TOP"
Width = "{binding converter = {staticresource dpiconverter}, converterparameter = 32 }"
Height = "{binding converter = {staticresource dpiconverter}, converterparameter = 32}">
<Image. margin>
<Binding converter = "{staticresource dpiconverter}">
<Binding. converterparameter>
<Thickness left = "18" Top = "24" Right = "0" Bottom = "48"/>
</Binding. converterparameter>
</Binding>
</Image. margin>
</Image>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.