我有一个特定的需求,即在不同的屏幕分辨率下,我的表单中的按钮大小应保持一定大小。如上图所示,我有“ON”和“OFF”两个按钮,它们的大小在特定分辨率下都是2厘米。现在我需要代码来重新调整按钮大小以适应不同的分辨率,或者使按钮的大小即使在不同分辨率下也保持不变。谢谢。
根据https://dev59.com/rW445IYBdhLWcg3wnrvM#4767664的适应,我们可以得到:
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);
几点需要注意:
1024x768
而不是1360x768
时,控件将被拉伸。 - Shell不了解Winforms,但这可能可以通过CSS处理:
@media (min-width: 221px) {
#button1 {
width: 200px;
height: 200px;
}
}
@media (min-width: 280px) {
#button1 {
width: 240px;
height: 240px;
}
}
针对不同的屏幕尺寸和按钮2进行重复操作。可以使用比我定义的更小或更大的屏幕尺寸。
或者可以使用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));
}
});
无论如何都是这样。包括电话方面的内容,以防您正在寻找这方面的信息...
ON
按钮将覆盖在OFF
按钮上。因为OFF
按钮的左侧位置不会根据ON
按钮的宽度进行设置。 - Shell