3

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

enter image description here

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

sia
  • 1,872
  • 1
  • 23
  • 54

3 Answers3

0

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.

Community
  • 1
  • 1
Shell
  • 6,818
  • 11
  • 39
  • 70
0

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...)
Community
  • 1
  • 1
Eugene Podskal
  • 10,270
  • 5
  • 31
  • 53
  • @sia How exactly it does not work? What is the value returned from CentimeterToPixel? Have you tried to debug CentimeterToPixel? What is the value of g.DpiY? – Eugene Podskal Aug 19 '14 at 05:43
  • @EugenePodskal how can we get the actual size from dpi? because DPI will not be changed on screen resolution change. the control will be stretched when i change the resultion to `1024x768` from `1360x768`. – Shell Aug 19 '14 at 07:03
  • @Shell DPI(PPI) is our only(at least that I know of) way to correlate pixels and physical dimensions(resolution). DPI usually doesn't change automatically with resolution change, so in any resolutions different from standard or if DPI is inconsistent(manually changed) we will have controls that actually differ from desired physical size, but their "virtual physical size" will be correct. Unfortunately, as I now understand, it won't help in OP situation. Maybe someone will get a solution that takes into account both the DPI and the resolution to provide right physical dimensions on the screen. – Eugene Podskal Aug 19 '14 at 07:16
-3

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...

user3685738
  • 35
  • 2
  • 11
  • hai this is not web application windows application – senthilkumar2185 Aug 19 '14 at 03:08
  • 1
    In that case, I'd probably read: http://social.msdn.microsoft.com/Forums/en-US/bfb15fd0-533a-47b8-af07-472310002f26/c-winforms-resizing-forms?forum=csharplanguage. Or try setting WindowState = WindowState.Maximized and anchor the buttons to ensure they keep their distance from the edges – user3685738 Aug 19 '14 at 03:19
  • 2
    you should update your answer and post some code and suggestion related to winform otherwise you may get down vote – Shell Aug 19 '14 at 03:38