开发者

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


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜