[英]resize button controls on windows form to fixed size as per screen resolution
我有一个特殊的要求,即在不同屏幕分辨率下,表单上的-in按钮大小应保持特定大小。
如上图所示,我有一个按钮“ ON”和“ OFF”,对于特定分辨率,两个按钮的大小均为2 cms。
现在,我需要将不同尺寸的按钮调整为2厘米的代码,或者即使在不同分辨率下,按钮的尺寸也应保持不变。
谢谢
这个问题似乎是“如何使Windows窗体分辨率独立?”。 顺便说一句,互联网上有很多示例可以使您的表单解析独立。 不幸的是,.Net没有提供任何类型的功能来使您的应用程序对所有分辨率通用。 但是,没有一种正确的方法。
我已经为此发布了逻辑。 您可以检查一下。
您只需要将该代码添加到Windows窗体中即可。
修改https://stackoverflow.com/a/4767664/3745022,我们可以获得:
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);
几点:
不知道Winforms,但是可以使用CSS处理:
@media (min-width: 221px) {
#button1 {
width: 200px;
height: 200px;
}
}
@media (min-width: 280px) {
#button1 {
width: 240px;
height: 240px;
}
}
对不同的屏幕尺寸和button2重复此操作。 可以比我定义的屏幕更大或更小。
或者可以使用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));
}
});
反正就是这样 包括手机内容,以防您寻找...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.