简体   繁体   English

在VB6中调整窗口大小时的居中元素

[英]Centering elements when resizing windows in VB6

I'm building an application in VB6 where the standard form size is 1024x768, for the older monitors that will see it. 我正在标准格式为1024x768的VB6中构建一个应用程序,供较早的显示器使用。 Some of the users will, however, have higher resolutions available, and I'd like the program to still look nice when they maximize the screen. 但是,有些用户将具有更高的分辨率,我希望该程序在最大化屏幕时仍然看起来不错。 I don't want to resize the elements on the form at all, but I am looking to recenter everything. 我根本不想调整表单上元素的大小,但我希望对所有内容进行更新。 There are two options as I see it, and I don't really like either one: 我看到有两种选择,但我并不喜欢其中一种:

  • The _real_ way, writing dynamic code that'll place each element on the form in relation to other elements, so that no matter what size it is, they'll always recenter properly. _real_方式,编写动态代码,该代码将每个元素与其他元素相关联地放置在表单上,​​因此,无论其大小如何,它们始终会正确更新。
  • The easier but cheesier way, to put all the elements in a 1024x768 PictureBox and just center that on the screen whenever the form is resized. 将所有元素放置在1024x768 PictureBox中并在调整表单大小时将其居中放置在屏幕上的方法,是一种更简单但更实用的方法。

I don't love the first one because ugh, and I don't love the second one because it's such an ad hoc solution. 我不喜欢第一个,因为呃,我不喜欢第二个,因为这是一个特别的解决方案。 Also, while I like the idea behind the first one more, it has the problem of permanence: if I need to go back and change some elements, I'm then stuck rewriting lots of repositioning code. 同样,虽然我更喜欢第一个背后的想法,但是它存在永久性问题:如果我需要返回并更改某些元素,那么我就不得不重写许多重新定位的代码。 And the second one has a sorta similar issue, that I'd lose one of the nice things that VB6 does provide: being able to see the grid structure of the form. 第二个也有类似的问题,我将失去VB6确实提供的一件好事:能够看到表单的网格结构。

Anyone know of any magic solutions? 任何人都知道魔术解决方案吗?

I'm currently leaning towards the PictureBox, just because the prospect of writing and undoubtedly rewriting all that positioning code is depressing. 我目前倾向于使用PictureBox,因为编写和无疑重写所有定位代码的前景令人沮丧。 The issue here then is that I already have these 50+ elements on the form, but I'd need them to be in the PictureBox. 那么这里的问题是我已经在窗体上拥有了这50多个元素,但是我需要将它们放在PictureBox中。 I can copy and paste without creating the array it always seems to want to make, but then I'd have to go through an rename them all... so my second question, is there any simple way to transfer a preexisting element on a form into a PictureBox? 我可以复制和粘贴而不创建似乎总是想创建的数组,但是然后我必须对它们全部进行重命名...所以我的第二个问题是,有什么简单的方法可以将预先存在的元素转移到形式成一个PictureBox?

Thanks everyone! 感谢大家!

There is no free lunch. 天下没有免费的午餐。 If you want your VB6 application to "look nice" when resized, you have to write the dynamic sizing logic in the Form_Resize event. 如果希望您的VB6应用程序在调整大小时“看起来不错”,则必须Form_Resize事件中编写动态大小调整逻辑。 Otherwise, as others have stated, you'll have large gray areas with everything crammed in the upper left corner. 否则,正如其他人所述,您将有很大的灰色区域,所有内容都塞在左上角。 Centering in a PictureBox isn't much better. PictureBox居中并没有更好。 Either way, it will look non-standard and amateurish. 无论哪种方式,它看起来都是非标准的和业余的。

I've written this type of code for several VB6 applications. 我已经为多个VB6应用程序编写了此类代码。 I'll agree that it's a little tiresome to write, but it's not difficult. 我同意写起来有点累,但这并不困难。 You just have to think about: 您只需要考虑:

  • Button positioning - The lower right coordinates of your form will change, and buttons are usually placed along the right side, or relative to the lower-right corner. 按钮位置-表单的右下角坐标将改变,并且按钮通常沿右侧或相对于右下角放置。
  • Display control resizing - You can widen listboxes, multi-line textboxes, dropdown lists, etc. Your code can decide if these should be a percentage of the form's new width/height, or should expand to fill what's left after you've positioned everything else. 调整显示控件的大小-您可以加宽列表框,多行文本框,下拉列表等。您的代码可以决定这些值应为表单新宽度/高度的百分比,还是应该扩展为填充定位所有内容后剩下的内容其他。 I think the latter approach works better, but it depends on the application. 我认为后一种方法效果更好,但这取决于应用程序。
  • The margin between the controls and the edges of the form, as well as the margin between the controls themselves. 控件和窗体边缘之间的边距,以及控件本身之间的边距。 I define a value named "Gutter" to hold this value, then apply it as necessary when positioning, say, a series of buttons horizontally relative to the lower right corner. 我定义了一个名为“装订线”的值来保存该值,然后在相对于右下角水平放置一系列按钮时,根据需要应用它。
  • Don't resize buttons or labels, just reposition them. 不要调整按钮或标签的大小 ,只需调整它们的位置即可。
  • Don't resize fonts. 不要调整字体大小。

"Anchor" type properties make this type of code unnecessary in VB.NET. “锚点”类型属性使VB.NET中不需要这种类型的代码。 If you think about that for a minute, you'll see that if a few properties can handle this logic, the lines of code needed to do the same can't be all that complex. 如果您仔细考虑一下,您会发现,如果有一些属性可以处理此逻辑,那么执行相同操作所需的代码行就不会那么复杂。 Once you have the first control positioned and sized, you can base other controls positions off that control's top, left, width, and height, and just walk your way across the form. 一旦第一个控件的位置和大小确定后,就可以将其他控件的位置作为该控件的顶部,左侧,宽度和高度的基础,然后在表单中移动。

When you get it right, I think you will find that it actually is worth the effort. 如果您做对了,我认为您会发现实际上值得付出努力。

If the elements are nog going to change size or position in relation to each other I would probably go for the PictureBox approach. 如果元素要改变相对于彼此的大小或位置,我可能会选择PictureBox方法。 I don't see how that would be bad really. 我不知道那会是多么糟糕。

If the controls need to be resized or relocated, I would (and God knows I used to) write loads of resizing code... 如果需要调整控件的大小或位置,我(上帝知道我曾经)写过很多调整代码的代码...

http://www.functionx.com/vbnet/Lesson08.htm http://www.functionx.com/vbnet/Lesson08.htm

Look at Control Containers. 查看控制容器。 As I noted in comment the second solution you described isn't ad hoc - it is organized. 正如我在评论中指出的那样,您描述的第二个解决方案不是临时性的-它是有组织的。 UI is all about containers. UI都是关于容器的。

You're right in feeling icky about PictureBox, though. 不过,您对PictureBox感到讨厌。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 调试vb6程序时出现错误错误的调用约定 - Error Bad Calling Convention when debugging vb6 program VB6中的Dtpicker - Dtpicker in VB6 我想在 Windows 10 操作系统上安装 VB6。 我需要处理 VBP 项目,所以我必须无缝安装 VB6 - I want to install VB6 on windows 10 OS. I need to work on VBP project so I have to install VB6 seamlessly 虽然VB6 IDE无法通过Windows 10上的GetObject函数获取对象 - Though VB6 IDE unable to get object through GetObject function on windows 10 Windows 8 RTM是否支持VB6(SP6)运行时文件?如果是这样,哪些? - Does Windows 8 RTM Support VB6 (SP6) Runtime files? If so, which ones? Visual Studio 是否有任何显示 Windows API 代码片段的内容(就像我认为 VB6 可能有的那样)? - does visual studio have anything that shows snippets of code for the Windows API(like I think VB6 might have)? 在VB.net中将元素添加到另一页上的列表框(Windows Phone 7) - Add elements to listbox on another page (Windows Phone 7) in VB.net 在VB中上传之前调整图像大小 - Resizing image before upload in vb 如何调用按钮单击VB6中的事件 - How To Call Button Click Event In VB6 在 VB6 中创建一个 dll 以在 .Net 应用程序中使用 - Creating a dll in VB6 for use in a .Net application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM