简体   繁体   中英

resize button controls on windows form to fixed size as per screen resolution

i have a specific requirement where -in size of buttons on my form should remain of particular size at different screen resolution.

在此处输入图片说明

Example as seen in above form, i have buttons "ON" and "OFF" & size of both buttons is 2 cms for particular resolution.

Now i need code which re-sizes buttons to 2 cms for different resolution or size of button should remain same even at different resolution.

thanks

The question seems like a "How to make windows form resolution independent?". BTW, there are lot of examples are given in internet to make your form resolution independent. Unfortunately .Net doesn't provide any kind of facility to make your application common for all resolutions. But, there is no one correct way.

I have already posted a logic for this. You can check this out.

Scale windows forms window

You just need to add that code to your windows form.

Adapting https://stackoverflow.com/a/4767664/3745022 we can get:

private int CentimeterToPixel(double Centimeter)
{
    double pixel = -1;
    using (Graphics g = this.CreateGraphics())
    {
        pixel = Centimeter * g.DpiY / 2.54d;
    }
    return (int)pixel;
}

// ...

button.Width = CentimeterToPixel(2);

Few points:

  1. It uses DpiY for conversion, that may(hardly possible, but nonetheless) differ from DpiX.
  2. You may want to read about DPI aware applications , DPI awareness is a better choice than manual scaling, because in manual scaling you are responsible for everything that should be scaled(fonts, margins, size...)

Dont know Winforms but this could potentially be handled with CSS:

@media (min-width: 221px) {
    #button1 {
        width: 200px;
        height: 200px;
    }
}
@media (min-width: 280px) {
    #button1 {
        width: 240px;
        height: 240px;
    }
}

repeat for different screen sizes, and for button2. Can smaller and larger screen sizes than what I have defined.

Or could be handled using jQuery:

jQuery(document).ready(function ($) {

window.onresize = function () {
    if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
        $("#button1").width(($(window).width()) / (3));
        $("#button1").height(($(window).height()) / (3));
        $("#button2").width(($(window).width()) / (3));
        $("#button2").height(($(window).height()) / (3));
    }
    else {
        $("#button1").width(($(window).width()) / (1.5));
        $("#button1").height(($(window).height()) / (1.5));
        $("#button2").width(($(window).width()) / (1.5));
        $("#button2").height(($(window).height()) / (1.5));
    }
});

something like that anyway. Included the phone stuff in case that's what your looking for...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM