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

发布于 2024-10-20 11:34:19 字数 676 浏览 5 评论 0原文

我正在 VB6 中构建一个应用程序,其中标准表单大小为 1024x768,以便较旧的显示器可以看到它。然而,一些用户可以使用更高的分辨率,并且我希望程序在最大化屏幕时仍然看起来不错。我根本不想调整表单上元素的大小,但我希望将所有内容重新居中。我认为有两种选择,但我不太喜欢其中一种:

  • _real_ 方式,编写动态代码,将每个元素相对于其他元素放置在表单上,​​这样无论它的大小是多少,他们总是会正确地重新定位。
  • 更简单但更俗气的方法是将所有元素放入 1024x768 PictureBox 中,并在调整表单大小时将其置于屏幕中央。

我不喜欢第一个,因为呃,我也不喜欢第二个,因为它是一个临时解决方案。另外,虽然我更喜欢第一个背后的想法,但它存在永久性问题:如果我需要返回并更改某些元素,那么我就不得不重写大量重新定位代码。第二个有一个类似的问题,我会失去 VB6 提供的一项好东西:能够看到表单的网格结构。

有人知道有什么神奇的解决方案吗?

我目前倾向于 PictureBox,只是因为编写和毫无疑问地重写所有定位代码的前景令人沮丧。这里的问题是我已经在表单上有了这 50 多个元素,但我需要它们位于 PictureBox 中。我可以复制和粘贴,而无需创建它似乎总是想要创建的数组,但随后我必须将它们全部重命名......所以我的第二个问题是,是否有任何简单的方法可以在形成一个PictureBox?

谢谢大家!

I'm building an application in VB6 where the standard form size is 1024x768, for the older monitors that will see it. 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.
  • 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.

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.

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

Thanks everyone!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南风几经秋 2024-10-27 11:34:19

天下没有免费的午餐。如果您希望 VB6 应用程序在调整大小时“看起来不错”,则必须Form_Resize 事件中编写动态调整大小逻辑。否则,正如其他人所说,您将拥有大片灰色区域,所有内容都挤在左上角。在 PictureBox 中居中也好不了多少。无论哪种方式,它都会显得不标准和业余。

我已经为多个 VB6 应用程序编写了此类代码。我承认写起来有点累,但这并不困难。您只需要考虑:

  • 按钮定位 - 表单的右下角坐标将会改变,按钮通常沿着右侧放置,或者相对于右下角放置。
  • 显示控件调整大小 - 您可以加宽列表框、多行文本框、下拉列表等。您的代码可以决定这些是否应该是表单新宽度/高度的百分比,或者应该扩展以填充定位所有内容后剩下的内容别的。我认为后一种方法效果更好,但这取决于应用程序。
  • 控件和窗体边缘之间的边距,以及控件本身之间的边距。我定义了一个名为“Gutter”的值来保存该值,然后在相对于右下角水平定位一系列按钮时根据需要应用它。
  • 不要调整按钮或标签的大小,只需重新定位它们。
  • 不要调整字体大小。

“锚”类型属性使得这种类型的代码在 VB.NET 中变得不必要。如果您想一想,您就会发现,如果一些属性可以处理此逻辑,那么执行相同操作所需的代码行就不会那么复杂。一旦您确定了第一个控件的位置和大小,您就可以将其他控件的位置置于该控件的顶部、左侧、宽度和高度的基础上,然后直接在表单中移动。

当你做对了,我想你会发现它实际上是值得的。

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. 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. Either way, it will look non-standard and amateurish.

I've written this type of code for several VB6 applications. 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. 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.

美煞众生 2024-10-27 11:34:19

如果元素不会改变彼此之间的大小或位置,我可能会采用 PictureBox 方法。我不明白那真的会很糟糕。

如果控件需要调整大小或重新定位,我会(上帝知道我曾经)编写大量调整大小的代码......

If the elements are nog going to change size or position in relation to each other I would probably go for the PictureBox approach. 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...

挖鼻大婶 2024-10-27 11:34:19

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

查看控件容器。正如我在评论中指出的,您描述的第二个解决方案不是临时的 - 它是有组织的。 UI 都是关于容器的。

不过,您对 PictureBox 感到厌恶是对的。

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.

You're right in feeling icky about PictureBox, though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文