矩形与控件的对齐 [英] Alignment of Rectangles to Controls

查看:99
本文介绍了矩形与控件的对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新年快乐。希望每个人都享受假期!



我安装了一个我在Visual Studio C#中开发的应用程序,在一些具有更高分辨率屏幕的新笔记本电脑上并将它们发送到现场。一切正常,但有以下异常。此应用程序的关键组件是在客户站点上花费的时间。它以15分钟为增量输入。为了帮助员工输入时间,我使用无线电控件并在控件下方的图形栏中显示隐含时间。这是一个糟糕的屏幕表示(因为我不能粘贴图像)。图像'X'是图形彩色矩形。以下是3:00到4:15的正确表示。



 1 2 3 4 5 6 7 
ooooooo
XXXXXX





当在其中一台新笔记本电脑上输入相同的数据时,矩形不再与正确的无线电控制。



 1 2 3 4 5 6 7 
ooooooo
XXXXXX







时间分为24小时,增量为15分钟。矩形的颜色由代码中较早初始化的数组中的条目控制。根据无线电控制位置属性中的x,y坐标,无线电控制器相隔20个单位。所以我使矩形的宽度= 5(20/4十五分钟段)并计算左侧位置。



所以问题是:为什么这对旧笔记本电脑起作用,无论我在屏幕上设置什么分辨率,都可以在我的开发PC上运行,但是没有正确对齐新的笔记本电脑?



矩形r = 矩形( 2  2  20  10 );  //   left,top,width,heigth  
int width = 5 ;
int top = 2 ;
int height = 10 ;
位图bmp = 位图(hourlyPictureBox.Width,hourlyPictureBox.Height);
Graphics g = Graphics.FromImage(bmp);
g.PageUnit = GraphicsUnit.Pixel;
for (i = 0 ; i < ; = 100 ; i ++)
{
r = new 矩形( 4 +(i * width),top,width,height); // left,top,width,heigth
if (slot [i] == 0
{
g.FillRectangle(Brushes.White,r);
}
如果(slot [i] == 1
{
g.FillRectangle(Brushes.red,r);
}
........等等 所有颜色$​​ b $ b}



hourlyPictureBox.Image = bmp;

希望对于知道自己在做什么的人来说这是一个简单的问题。 :=)

提前感谢您对此事的帮助。

Rob



添加了代码块 - OriginalGriff [/ edit]

解决方案

原则上你做错了:使用手动定位。您应该使用一种非常不同的方法:使用 System.Windows.Forms.Control.Dock 属性,使用不同的值,但 Dock.None

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.dock%28v=vs.110%29.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.windows.forms.dockstyle%28v=vs.110%29.aspx [ ^ ]。



此外,它附带属性 System.Windows.Forms.Control.Padding

http://msdn.microsoft.com/en-us/library/system。 windows.forms.control.padding%28v = vs.110%29.aspx [ ^ ]。



在此方法,所有控件的所有位置属性变得无关紧要,一切都自动对齐。您将需要更多 Panel 的实例,包括那些仅用于间距的实例。在您的情况下,您需要将所有单选按钮放在几个垂直面板中。您将有两种矩形区域:单选按钮下方和它们之间。对于这两种情况,您将需要一个单独的面板,一个带有单选按钮,一个用于中间的空间。在这两种面板中,您将有子面板作为矩形颜色。方法如下:

┌─────────────────
...│2 ││3││4│... $ b $b│o││o│││$ b $b├──────────────────$ b $b└──────────────

矩形形状表示 Panel 实例,例如,使用 DockStyle.Left 进行排列。底部面板可以使用 BackColor 属性进行着色。 (请注意,面板上没有可见的边框,上图仅供说明。)它们是放置在较大面板上的子面板。所以在图片上显示10个 Panel 对象,还有一个可以是他们的父面板。



And等......



-SA


很可能是Lappie有不同的显示,字体尺寸,缩放系数等 - 这些都是可调节的,以应对较小的显示尺寸。



如何做到这一点:

取第一个按钮的Left属性,然后从最后一个按钮的Left plus Width属性中减去它。除以2。这使您可以从第一个按钮的左边缘到中心的距离。

 1 2 3 4 5 6 7 
ooooooo
^



减少想要绘制的块数的一半*(块宽+间隙宽度))

将其添加到第一个按钮的Left属性,第一个块有Left属性。

 1 2 3 4 5 6 7 
ooooooo
XXXXXXX
^

因为这些都在同一个坐标系中,所以它应该与设备无关。


嗯......经过几个小时的挫折后,我发现使用浮动#并没有帮助。一切都必须达到一个整数,所以事情会在每15分钟的增量上排成一行。



我也从未想过如何循环播放并引用单选按钮,所以一堆硬编码的垃圾修复了我的问题。代码很难看,但很有效。



感谢您指引我正确的方向,知道要看什么。魔鬼在细节中。











  if ((radioButton54.Left) - (radioButton30.Left)> ;   600 
{
width = 7 ;
offset = 12 ;
radioButton2.Left = radioButton1.Left +(width * 4 );
radioButton3.Left = radioButton2.Left +(width * 4 );
radioButton4.Left = radioButton3.Left +(width * 4 );
radioButton5.Left = radioButton4.Left +(width * 4 );
radioButton6.Left = radioButton5.Left +(width * 4 );
radioButton7.Left = radioButton6.Left +(width * 4 );
radioButton8.Left = radioButton7.Left +(width * 4 );
radioButton9.Left = radioButton8.Left +(width * 4 );
radioButton10.Left = radioButton9.Left +(width * 4 );
radioButton11.Left = radioButton10.Left +(width * 4 );
radioButton12.Left = radioButton11.Left +(width * 4 );
radioButton13.Left = radioButton12.Left +(width * 4 );
radioButton14.Left = radioButton13.Left +(width * 4 );
radioButton15.Left = radioButton14.Left +(width * 4 );
radioButton16.Left = radioButton15.Left +(width * 4 );
radioButton17.Left = radioButton16.Left +(width * 4 );
radioButton18.Left = radioButton17.Left +(width * 4 );
radioButton19.Left = radioButton18.Left +(width * 4 );
radioButton20.Left = radioButton19.Left +(width * 4 );
radioButton21.Left = radioButton20.Left +(width * 4 );
radioButton22.Left = radioButton21.Left +(width * 4 );
radioButton23.Left = radioButton22.Left +(width * 4 );
radioButton24.Left = radioButton23.Left +(width * 4 );
radioButton25.Left = radioButton24.Left +(width * 4 );
st1.Left = radioButton1.Left - 4 ;
st2.Left = radioButton2.Left - 4 ;
st3.Left = radioButton3.Left - 4 ;
st4.Left = radioButton4.Left - 4 ;
st5.Left = radioButton5.Left - 4 ;
st6.Left = radioButton6.Left - 4 ;
st7.Left = radioButton7.Left - 4 ;
st8.Left = radioButton8.Left - 4 ;
st9.Left = radioButton9.Left - 4 ;
st10.Left = radioButton10.Left - 4 ;
st11.Left = radioButton11.Left - 4 ;
st12.Left = radioButton12.Left - 4 ;
st13.Left = radioButton13.Left - 4 ;
st14.Left = radioButton14.Left - 4 ;
st15.Left = radioButton15.Left - 4 ;
st16.Left = radioButton16.Left - 4 ;
st17.Left = radioButton17.Left - 4 ;
st18.Left = radioButton18.Left - 4 ;
st19.Left = radioButton19.Left - 4 ;
st20.Left = radioButton20.Left - 4 ;
st21.Left = radioButton21.Left - 4 ;
st22.Left = radioButton22.Left - 4 ;
st23.Left = radioButton23.Left - 4 ;
st24.Left = radioButton24.Left - 4 ;
pm13.Left = radioButton13.Left - 4 ;
pm14.Left = radioButton14.Left - 4 ;
pm15.Left = radioButton15.Left - 4 ;
pm16.Left = radioButton16.Left - 4 ;
pm17.Left = radioButton17.Left - 4 ;
pm18.Left = radioButton18.Left - 4 ;
pm19.Left = radioButton19.Left - 4 ;
pm20.Left = radioButton20.Left - 4 ;
pm21.Left = radioButton21.Left - 4 ;
pm22.Left = radioButton22.Left - 4 ;
pm23.Left = radioButton23.Left - 4 ;
pm24.Left = radioButton24.Left - 4 ;

radioButton31.Left = radioButton30.Left +(width * 4 );
radioButton32.Left = radioButton31.Left +(width * 4 );
radioButton33.Left = radioButton32.Left +(width * 4 );
radioButton34.Left = radioButton33.Left +(width * 4 );
radioButton35.Left = radioButton34.Left +(width * 4 );
radioButton36.Left = radioButton35.Left +(width * 4 );
radioButton37.Left = radioButton36.Left +(width * 4 );
radioButton38.Left = radioButton37.Left +(width * 4 );
radioButton39.Left = radioButton38.Left +(width * 4 );
radioButton40.Left = radioButton39.Left +(width * 4 );
radioButton41.Left = radioButton40.Left +(width * 4 );
radioButton42.Left = radioButton41.Left +(width * 4 );
radioButton43.Left = radioButton42.Left +(width * 4 );
radioButton44.Left = radioButton43.Left +(width * 4 );
radioButton45.Left = radioButton44.Left +(width * 4 );
radioButton46.Left = radioButton45.Left +(width * 4 );
radioButton47.Left = radioButton46.Left +(width * 4 );
radioButton48.Left = radioButton47.Left +(width * 4 );
radioButton49.Left = radioButton48.Left +(width * 4 );
radioButton50.Left = radioButton49.Left +(width * 4 );
radioButton51.Left = radioButton50.Left +(width * 4 );
radioButton52.Left = radioButton51.Left +(width * 4 );
radioButton53.Left = radioButton52.Left +(width * 4 );
radioButton54.Left = radioButton53.Left +(width * 4 );
et30.Left = radioButton30.Left-4;
et31.Left = radioButton31.Left - 4 ;
et32.Left = radioButton32.Left - 4 ;
et33.Left = radioButton33.Left - 4 ;
et34.Left = radioButton34.Left - 4 ;
et35.Left = radioButton35.Left - 4 ;
et36.Left = radioButton36.Left - 4 ;
et37.Left = radioButton37.Left - 4 ;
et38.Left = radioButton38.Left - 4 ;
et39.Left = radioButton39.Left - 4 ;
et40.Left = radioButton40.Left - 4 ;
et41.Left = radioButton41.Left - 4 ;
et42.Left = radioButton42.Left - 4 ;
et43.Left = radioButton43.Left - 4 ;
et44.Left = radioButton44.Left - 4 ;
et45.Left = radioButton45.Left - 4 ;
et46.Left = radioButton46.Left - 4 ;
et47.Left = radioButton47.Left - 4 ;
et48.Left = radioButton48.Left - 4 ;
et49.Left = radioButton49.Left - 4 ;
et50.Left = radioButton50.Left - 4 ;
et51.Left = radioButton51.Left - 4 ;
et52.Left = radioButton52.Left - 4 ;
et53.Left = radioButton53.Left - 4 ;
et54.Left = radioButton54.Left - 4 ;
}


int top = 2 ;
int height = 10 ;
位图bmp = 位图(hourlyPictureBox.Width,hourlyPictureBox.Height);
Graphics g = Graphics.FromImage(bmp);
g.PageUnit = GraphicsUnit.Pixel; // ??

for (i = 0 ; i < = 100 ; i ++)
{

int w =宽度;
int z = offset +(i * width);
// r = new矩形(4 +(i *宽度)),顶部,宽度,高度) ; // left,top,width,heigth
r = new 矩形(z,top,w,height); // left,top,width,heigth
if (slot [i] == 0
{
g.FillRectangle(Brushes.White,r);
}
......依旧等等
}
hourlyPictureBox.Image = bmp;
g.Dispose();


Happy New Year. Hope everyone enjoyed the holidays !

I installed an application I developed in Visual Studio C# on some new laptops with wider higher resolution screens and sent them into the field. Everything works fine with the following exception. A key component to this application is the entry of time spent on customer sites. It is entered in 15 minute increments. To aid the employee in entering the time I use radio controls and display the implied time in a graphic bar below the controls. Here is a poor representation to the screen (since I cant paste an image here). Image the 'X' are a graphic colored rectangle. The following is a correct representation of 3:00 to 4:15.

1   2   3   4   5   6   7
o   o   o   o   o   o   o
        XXXXXX



When the same data is entered on one of the new laptops the rectangles no longer align with the correct radio controls.

1   2   3   4   5   6   7
o   o   o   o   o   o   o
   XXXXXX




The time is split into 24 hours in 15 minute increments. The color of the rectangle is controlled by an entry in an array initialized earlier in the code. The Radio controls are located 20 units apart according the x,y coordinates in the radio control location property. So I make the width of the rectangle = 5 (20/4 fifteen minute segments) and calculate the left position.

So the question is: Why does this work on the old laptops, works on my development PC regardless of what resolution I set the screen, but does NOT align properly on the new laptops?

Rectangle r = new Rectangle(2, 2, 20, 10); //left,top,width,heigth
int width = 5;
int top = 2;
int height = 10;
Bitmap bmp = new Bitmap(hourlyPictureBox.Width, hourlyPictureBox.Height);
Graphics g = Graphics.FromImage(bmp);
g.PageUnit = GraphicsUnit.Pixel;
for (i = 0; i <= 100; i++)
   {
    r = new Rectangle(4 + (i * width), top, width, height); //left,top,width,heigth
   if (slot[i] == 0)
    {
          g.FillRectangle(Brushes.White, r);
     }
   if (slot[i] == 1)
    {
          g.FillRectangle(Brushes.red, r);
     }
      ........so on and so forth for all the colors
   }


hourlyPictureBox.Image = bmp;
Hopefully a simple question for someone who knows what they are doing. :=)
Thank you in advance for your help in this matter.
Rob

[edit]Code blocks added - OriginalGriff[/edit]

解决方案

You are doing wrong thing in principle: using manual positioning. You should use a very different approach: using System.Windows.Forms.Control.Dock property with different values except Dock.None:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.dock%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.dockstyle%28v=vs.110%29.aspx[^].

Also, it comes with the property System.Windows.Forms.Control.Padding:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.padding%28v=vs.110%29.aspx[^].

In this approach, all the Location property of all controls become irrelevant, everything is aligned automatically. You will need a lot more instances of Panel, including those use just for spacing. In your case, you will need to put all radio buttons in several vertical panels. You will have two kinds of rectangular areas: under the radio buttons and between them. For both cases, you will need a separate panel, one with the radio button, one for the space in between. In both kinds of panel, you will have sub-panels colored as your rectangles. Here is how:

   ┌───┬───┬───┬───┬───┐
...│ 2 │   │ 3 │   │ 4 │...
   │ o │   │ o │   │ o │
   ├───┼───┼───┼───┼───┤
   └───┴───┴───┴───┴───┘

Rectangular shapes denotes Panel instances, arranged, for example, using DockStyle.Left. Bottom panels can be colored by using BackColor property. (Note that there are not visible borders on the panels, the picture above is just for explanation.) They are child panels placed on the bigger panels. So the on the picture, 10 Panel objects are shown, one more could be their parent panel.

And so on…

—SA


Could well be that the Lappie has different display, font size, zoom factor, etc. - these are all adjustable to cope with the smaller display dimensions.

How I would do it:
Take the Left property of the first button, and subtract it from the Left plus Width properties of the last. Divide by two. This gives you distance to the centre from the left edge of the first button.

1   2   3   4   5   6   7
o   o   o   o   o   o   o
            ^


Reduce that by half of the number of blocks you want to paint * (block width + gap width))
Add that to the Left property of the first button, and you have the Left property for the first block.

1   2   3   4   5   6   7
o   o   o   o   o   o   o
      X X X X X X X
      ^

Because this is all in the same coordinate system, it should be device independent.


Well.....after a couple of hours of frustration I found out that using floating # does not help. Everything had to work out to an integer so things would line up on each 15 minute increment.

I also never figured out how to loop thru and reference the radio buttons, so a bunch of hard coded crap fixed my problem. Code is ugly, but works.

Thank you for heading me in the right direction to know what to look at. Devil is in the details.





if ((radioButton54.Left) - (radioButton30.Left) > 600)
            {
                width = 7;
                offset = 12;
                radioButton2.Left = radioButton1.Left + (width * 4);
                radioButton3.Left = radioButton2.Left + (width * 4);
                radioButton4.Left = radioButton3.Left + (width * 4);
                radioButton5.Left = radioButton4.Left + (width * 4);
                radioButton6.Left = radioButton5.Left + (width * 4);
                radioButton7.Left = radioButton6.Left + (width * 4);
                radioButton8.Left = radioButton7.Left + (width * 4);
                radioButton9.Left = radioButton8.Left + (width * 4);
                radioButton10.Left = radioButton9.Left + (width * 4);
                radioButton11.Left = radioButton10.Left + (width * 4);
                radioButton12.Left = radioButton11.Left + (width * 4);
                radioButton13.Left = radioButton12.Left + (width * 4);
                radioButton14.Left = radioButton13.Left + (width * 4);
                radioButton15.Left = radioButton14.Left + (width * 4);
                radioButton16.Left = radioButton15.Left + (width * 4);
                radioButton17.Left = radioButton16.Left + (width * 4);
                radioButton18.Left = radioButton17.Left + (width * 4);
                radioButton19.Left = radioButton18.Left + (width * 4);
                radioButton20.Left = radioButton19.Left + (width * 4);
                radioButton21.Left = radioButton20.Left + (width * 4);
                radioButton22.Left = radioButton21.Left + (width * 4);
                radioButton23.Left = radioButton22.Left + (width * 4);
                radioButton24.Left = radioButton23.Left + (width * 4);
                radioButton25.Left = radioButton24.Left + (width * 4);
                st1.Left = radioButton1.Left - 4;
                st2.Left = radioButton2.Left - 4;
                st3.Left = radioButton3.Left - 4;
                st4.Left = radioButton4.Left - 4;
                st5.Left = radioButton5.Left - 4;
                st6.Left = radioButton6.Left - 4;
                st7.Left = radioButton7.Left - 4;
                st8.Left = radioButton8.Left - 4;
                st9.Left = radioButton9.Left - 4;
                st10.Left = radioButton10.Left - 4;
                st11.Left = radioButton11.Left - 4;
                st12.Left = radioButton12.Left - 4;
                st13.Left = radioButton13.Left - 4;
                st14.Left = radioButton14.Left - 4;
                st15.Left = radioButton15.Left - 4;
                st16.Left = radioButton16.Left - 4;
                st17.Left = radioButton17.Left - 4;
                st18.Left = radioButton18.Left - 4;
                st19.Left = radioButton19.Left - 4;
                st20.Left = radioButton20.Left - 4;
                st21.Left = radioButton21.Left - 4;
                st22.Left = radioButton22.Left - 4;
                st23.Left = radioButton23.Left - 4;
                st24.Left = radioButton24.Left - 4;
                pm13.Left = radioButton13.Left - 4;
                pm14.Left = radioButton14.Left - 4;
                pm15.Left = radioButton15.Left - 4;
                pm16.Left = radioButton16.Left - 4;
                pm17.Left = radioButton17.Left - 4;
                pm18.Left = radioButton18.Left - 4;
                pm19.Left = radioButton19.Left - 4;
                pm20.Left = radioButton20.Left - 4;
                pm21.Left = radioButton21.Left - 4;
                pm22.Left = radioButton22.Left - 4;
                pm23.Left = radioButton23.Left - 4;
                pm24.Left = radioButton24.Left - 4;
                
                radioButton31.Left = radioButton30.Left + (width * 4);
                radioButton32.Left = radioButton31.Left + (width * 4);
                radioButton33.Left = radioButton32.Left + (width * 4);
                radioButton34.Left = radioButton33.Left + (width * 4);
                radioButton35.Left = radioButton34.Left + (width * 4);
                radioButton36.Left = radioButton35.Left + (width * 4);
                radioButton37.Left = radioButton36.Left + (width * 4);
                radioButton38.Left = radioButton37.Left + (width * 4);
                radioButton39.Left = radioButton38.Left + (width * 4);
                radioButton40.Left = radioButton39.Left + (width * 4);
                radioButton41.Left = radioButton40.Left + (width * 4);
                radioButton42.Left = radioButton41.Left + (width * 4);
                radioButton43.Left = radioButton42.Left + (width * 4);
                radioButton44.Left = radioButton43.Left + (width * 4);
                radioButton45.Left = radioButton44.Left + (width * 4);
                radioButton46.Left = radioButton45.Left + (width * 4);
                radioButton47.Left = radioButton46.Left + (width * 4);
                radioButton48.Left = radioButton47.Left + (width * 4);
                radioButton49.Left = radioButton48.Left + (width * 4);
                radioButton50.Left = radioButton49.Left + (width * 4);
                radioButton51.Left = radioButton50.Left + (width * 4);
                radioButton52.Left = radioButton51.Left + (width * 4);
                radioButton53.Left = radioButton52.Left + (width * 4);
                radioButton54.Left = radioButton53.Left + (width * 4);
                et30.Left = radioButton30.Left-4;
                et31.Left = radioButton31.Left - 4;
                et32.Left = radioButton32.Left - 4;
                et33.Left = radioButton33.Left - 4;
                et34.Left = radioButton34.Left - 4;
                et35.Left = radioButton35.Left - 4;
                et36.Left = radioButton36.Left - 4;
                et37.Left = radioButton37.Left - 4;
                et38.Left = radioButton38.Left - 4;
                et39.Left = radioButton39.Left - 4;
                et40.Left = radioButton40.Left - 4;
                et41.Left = radioButton41.Left - 4;
                et42.Left = radioButton42.Left - 4;
                et43.Left = radioButton43.Left - 4;
                et44.Left = radioButton44.Left - 4;
                et45.Left = radioButton45.Left - 4;
                et46.Left = radioButton46.Left - 4;
                et47.Left = radioButton47.Left - 4;
                et48.Left = radioButton48.Left - 4;
                et49.Left = radioButton49.Left - 4;
                et50.Left = radioButton50.Left - 4;
                et51.Left = radioButton51.Left - 4;
                et52.Left = radioButton52.Left - 4;
                et53.Left = radioButton53.Left - 4;
                et54.Left = radioButton54.Left - 4;
            }


            int top = 2;
            int height = 10;
            Bitmap bmp = new Bitmap(hourlyPictureBox.Width, hourlyPictureBox.Height);
            Graphics g = Graphics.FromImage(bmp);
            g.PageUnit = GraphicsUnit.Pixel;//??

            for (i = 0; i <= 100; i++)
            {

                int w = width;
                int z = offset  + (i * width);
                //r = new Rectangle(4 + (i * width)), top, width, height); //left,top,width,heigth
                r = new Rectangle(z, top, w, height); //left,top,width,heigth
                if (slot[i] == 0)
                {
                    g.FillRectangle(Brushes.White, r);
                }
  ......so on and so forth
            }
            hourlyPictureBox.Image = bmp;
            g.Dispose();


这篇关于矩形与控件的对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆