怎么用pinvoke获得唯一的控制名称? [英] How do get unique name of control with pinvoke ?

查看:74
本文介绍了怎么用pinvoke获得唯一的控制名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个从不同的.net应用程序生成报表的应用程序,问题是表单上有多个文本框,所有文本框的类名都是一样的,有没有办法得到唯一的名称每个控件?





I'm creating a application that generates reports form different .net application froms, the problem is there is multiple text boxes on the form and all the text boxes class name is the same, is there a way get the unique name for each control ?


private List<Data.Objects.ClassObject> GetChildClassesWithName(IntPtr hParent, int pI)
       {
           int ii = 1;
           int i = 0;
           int iMax = 100;

           IntPtr prevChild = IntPtr.Zero;
           IntPtr currChild = IntPtr.Zero;

           try
           {
               while (true & i < iMax)
               {
                   currChild = FindWindowEx(hParent, prevChild, null, null);
                   if (currChild == IntPtr.Zero)
                   {
                       return null;
                   }
                   else
                   {
                       ii++;

                       // Get current ClassName
                       int nRet;
                       StringBuilder ClassName = new StringBuilder(256);
                       nRet = GetClassName(currChild, ClassName, ClassName.Capacity);

                       Data.Objects.ClassObject item = new Data.Objects.ClassObject();
                       item._Cap = GetTextBoxText(currChild);
                       item.Name = ClassName.ToString();
                       item._Curr = currChild;
                       item.ParentID = pI;
                       item.CurrID = ii;

                       if (nRet != 0)
                       {
                           string[] ignore = { "" };
                           if (!ignore.Contains(ClassName.ToString()))
                           {
                               if (item._Cap != null)
                               {
                                   classlist.Add(item);
                               }
                               else if (item.Name == Data.DataManager.GetSettingValue("DesktopClassName"))
                               {
                                   item.CurrID = 1;
                                   item.ParentID = 12;
                                   item._doc = currChild;

                                   classlist.Add(item);
                               }
                           }
                           GetChildClassesWithName(currChild, ii);
                           prevChild = currChild;
                       }
                       else
                       {
                           return null;
                       }
                   }
               }

               return null;
           }
           catch (Exception ex)
           {
               string error = ex.Message;
               return null;
           }
       }

推荐答案

会员11117223写道:
Member 11117223 wrote:



谢谢,但是整数窗口每次都处理相同或不同的流程,威胁,系统等不同?


Thanks, but is the integer window handles the same each time or is it different for different process, threat, system, etc?

窗口把手是系统范围的。与在不同进程的地址空间中不同的指针不同,窗口句柄(以及其他句柄,原子等)被专门设计为在所有进程之间共享。它们是句柄,而不是通常的指针。



并且没有等等。隔离单位是应用程序域 AppDomain ;并且所有进程都在不同的应用程序域中。应用程序域有各自的地址空间;指针/句柄(即使是由不同进程共享的同一对象)也可以在不同的地址空间中具有不同的值;如果某些地址(通过任何更改)具有相同的数值,则它们指向不同应用程序域或进程中的不同对象。您可以在每个进程的应用程序域上创建更多,但这是一个不同的故事: http://msdn.microsoft.com/en-us/library/system.appdomain%28v=vs.110%29.aspx [ ^ ]。



现在,正如Dave Kreskowiak和我试图解释的那样,这个问题毫无意义,但并非全部。如果您认为可以依赖属性 Name 控制,请再想一想。这些名称仅对设计者很重要,用于生成代码,以确定生成成员的名称。在运行期间,这是微不足道的。如果在自己的代码中创建控件,则不需要使用这些名称。此外,您可以在运行时修改(例如,为所有这些操作分配null),只是为了看到它们不起任何作用。 因此,您永远不应该依赖这些名称。当然,它们甚至不存在于您通过P / Invoke使用的原始Windows API中。



-SA

Window handles are system-wide. Unlike pointers which are different in the address spaces of different process, window handle (as well as other handles, atoms, etc.) are designed specifically to be shared between all processes. They are handles, not usual pointers.

And there is no "etc.". The unit of isolation is application domain AppDomain; and all processes are in separate application domains. The application domains have their separate address spaces; pointers/handles (even to the same object shared by different processes) can have different values in different address spaces; and if some addresses, by any change, have the same numeric values, they point to different objects in different application domains or processes. You can create more then on application domain per process, but this is a different story: http://msdn.microsoft.com/en-us/library/system.appdomain%28v=vs.110%29.aspx[^].

Now, as Dave Kreskowiak at and I tried to explain, the question makes no sense, but that's not all. If you think that you can rely on the property Name of control, think again. Those names are only important for the designer, are used to generate code, to figure out the names of the generated members. During runtime, there are insignificant. If you create controls in your own code, you don't need to use these names. Moreover, you can modify the during runtime (say, assign null to all of them), just to see that they don't play any role. Therefore, you should never rely on those names. And of course, they don't even exist in raw Windows API you use through P/Invoke.

—SA


这篇关于怎么用pinvoke获得唯一的控制名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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