如何在Visual C ++中创建动态资源符号 [英] How can I create dynamically resource symbols in Visual C++

查看:76
本文介绍了如何在Visual C ++中创建动态资源符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我还在移动旧的应用程序,我可以看到应该改进的东西。



作为一个例子,我使用特定的硬编码资源符号对DoDataExchange函数进行硬编码以进行DDX分配。



我可以创建一个数组使用动态内存分配很容易,但我不知道如何在我刚刚创建的内容和资源符号名称之间进行分配。



ie:

Hello all,

I'm still porting hat old application and I can see things that should be improved.

As an example, I'm hardcoding the DoDataExchange function with specific hardcoded resource symbols to make the DDX assignments.

I can create an array using dynamic memory allocation easily, but what I do not know is how to make the assignment between what I've just created and the Resource symbol name.

i.e.:

DDX_Control(pDX, IDC_STATIC_R5, m_cstatR5);
DDX_Control(pDX, IDC_STATIC_R4, m_cstatR4);
DDX_Control(pDX, IDC_STATIC_R3, m_cstatR3);
DDX_Control(pDX, IDC_STATIC_R2, m_cstatR2);
DDX_Control(pDX, IDC_STATIC_R1, m_cstatR1);



在这里你可以看到我是如何建立联系的。



我想使用类似的东西:


Here you can see how I'm making some associations.

I would like to use something like:

CString csName = "";
for (i=0;i<100;i++)
{
  csName.Format("m_cstatR%i",i);
  DDX_Control(pDX, IDC_STATIC_R1+i, csName);
}





现在......我怎样才能动态创建IDC_STATIC_Rx而不用担心现有的IDC_STATIC_Rx?



我担心如果两个资源标识符相同会发生什么。



我试过谷歌对于它,但是我的google fu不如它应该那么好......显然我的VC ++技能有点生锈...



谢谢你提前!



Now... how can I create the IDC_STATIC_Rx dynamically without worrying for the existing ones?

I'm worried about what could happen if two resource identifiers would become equal.

I've tried to google for it, but my google fu is not as good as it should... clearly as my VC++ skills which are a little bit rusty...

Thank you in advance!

推荐答案

IDC_STATIC_R1 + i部分看起来没问题。只需确保为这些符号分配一个连续的数字范围,可以使用资源编辑器,也可以只编辑resource.h文件。



第三个参数DDX_Control应该是对CWnd的引用。你不能用字符串替换它。在我看来,遵守API的最简单方法是创建一个包含这些控件的控件成员变量的数组,并将数组元素提供给循环中的DDX_Control调用。
The IDC_STATIC_R1+i part looks okay. Just make sure that those symbols are assigned a consecutive range of numbers, either by using the resource editor or by simply editing the resource.h file.

The third argument of DDX_Control is supposed to be a reference to CWnd. You cannot replace that by a string. The easiest way to comply to the API is in my opinion to create an array with your control member variables for those controls and feed the array elements to the DDX_Control call in your loop.


A静态解决方案:):

A "static" solution :) :
{
  enum { maxCount = ... };
  
  static CWnd& arCtls[maxCount] = {
    m_ctlStatic1, m_ctlStatic2, ...
  };
  static UINT  arCtlIds[maxCount] = {
    IDC_STATIC1, IDC_STATIC2, ...
  };

  for (int i(0); i < maxCount; i++) {
    DDX_Control(pDX, arCtlIds[i], arCtls[i]);
  }
}



当然,数组和枚举可以声明为对话框的成员......


Of course, the arrays and the enum could be declared as members of your dialog...


这篇关于如何在Visual C ++中创建动态资源符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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