在运行时添加控件 [英] Adding controls at runtime

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

问题描述





我有一个包含多个控件的应用程序(静态文本,按钮,编辑控件),我想动态创建它们并根据它们重新定位它们添加和删​​除控件。是否有任何教程或示例让我了解如何动态添加,删除和重新定位控件。

解决方案

从这里开始:手动添加控件(MFX) [ ^ ]



教程: Visual-CMFC-Tutorial-Lesson-4-MFC-Basics [ ^ ]


创建控件动态通常不会太困难,因为解决方案1的第一个链接将向您显示。面对对话框调整大小时的重新定位更具挑战性。 CodeProject上有一些很好的文章你可以在关键词对话框调整大小下找到,例如这一篇:



简单且功能强大的可调整对话框 [ ^ ]



如果您正在开始新的开发,C#和WinForms通过使用面板和锚定使控件的动态布局变得更加容易。



如果你想要要坚持使用C ++和MFC,请注意对话框资源中的所有位置和大小都是以DLU(对话框单位)测量的,而在运行时则需要以像素为单位进行计算。因此,如果您想要将一个新控件与由对话框模板创建的另一个现有控件相关联,我建议通过GetWindowRect在运行时询问另一个控件的位置并从那里进行计算。

Hi,

I have a application that contains several controls (static text, button, edit control) and I want to create them dynamically and reposition them according to add and remove of controls. Is there any tutorial or sample that gives me an idea how to add, remove and reposition of controls dynamically.

解决方案

Start here: Adding Controls By Hand (MFX)[^]

Tutorial: Visual-CMFC-Tutorial--Lesson-4-MFC-Basics[^]


Creating controls dynamically is typically not too difficult as the first link of solution 1 will show you. The repositioning in the face of dialog resizing is a little more challenging. There are a couple of good articles here on CodeProject that you can find under the keywords "dialog" "resize", for example this one:

Simple and more powerful resizable dialog[^]

If you are starting a new development, C# and WinForms make the dynamic layout of controls so much easier by the use of panels and anchoring.

If you want to stick with C++ and MFC, note that all the positions and sizes in dialog resources are measured in DLU (dialog units), while at runtime you need to calculate in pixels. So if you want to position a new control in relation to another existing control, created by the dialog template, I''d recommend to ask the other control''s position at runtime via GetWindowRect and do my calculations from there.


这篇关于在运行时添加控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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