想获取另外一个程序中的TDrawGrid表格中的数据,应该怎么实现?

解决方案 »

  1.   

    另外一个程序中的数据?用通讯的形式吧,比如SOCKET,REMOTING,WCF等等;
      

  2.   


    首先你应该知道你需要获取的数据,来自于另外的一个程序,或者叫另外一个进程,
    这就涉及到一个进程间通讯的问题,Remoting可以实现跨进程或计算机边界进行通讯。或者用简单的SOCKET也能够实现,关于这方面的资料网上有很多参考的。
      

  3.   

    导出数据到XML实现数据传递
      

  4.   

    LoadTabl.html
    <html>
    <head><title>LoadTable</title></head>
    <script type="text/javascript" src="jquery-1.3.2.min.js"></script><script type="text/javascript">
    $(function()
    {
    $("#test").click(function(){$("#show").load("table.html table[id*=TDrawGrid]");}); });
    </script>
    <body>
    <input type="button" id="test" value="LoadTable"/>
    <div id="show"></div></body>
    </html>table.html
    <table id="TDrawGrid" border="1">
    <thead style="background-Color:#DFD"><th>Titel1</th><th>Titel2</th><th>Titel3</th><th>Titel4</th>
    </thead>
    <tbody>
    <tr><td>test1</td><td>test2</td><td>test3</td><td>test4</td></tr>
    <tr><td>test1</td><td>test2</td><td>test3</td><td>test4</td></tr>
    <tr><td>test1</td><td>test2</td><td>test3</td><td>test4</td></tr>
    <tr><td>test1</td><td>test2</td><td>test3</td><td>test4</td></tr>
    <tr><td>test1</td><td>test2</td><td>test3</td><td>test4</td></tr>
    </tbody>
    </table>
      

  5.   

    $("#test").click(function(){$("#show").loadtable.html table[id*=TDrawGrid]");});
      

  6.   

    如果两个程序的源代码都由你控制的话,可以采用共享内存,socket通信,共享文件等方式交换数据。如果包含TDrawGrid表格的应用程序无法修改,可以考虑下使用反射机制。没用过TDrawGrid控件,这是不是一个delphi控件? 你参考下面的代码,看有没有帮助。下面代码里获取TDrawGrid返回值的那句要根据实际数据存储的结构修改下。
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using System.Reflection;
    using System.Threading;namespace GetTDrawGrid
    {
        static class Program
        {
            static Form m_TargetForm = null;
            static BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance;
            static int delay = 1000;
            [STAThread]
            static void Main()
            {
                try
                {
                    Console.WriteLine("\nLaunching target app");
                    string exePath = @"..\\..\\..\WinApp\bin\Debug\TDrawGrid.exe"; //the file contains TDrawGrid                m_TargetForm = LaunchApp(exePath, "TDrawGrid.Form1"); //Form1 is the container of TDrawGrid control               
                    //根据TDrawGrid的数据结构修改此句的数据返回类型和属性名(这里是Rows)控件名是tDrawGrid1
                    DataGridViewRowCollection c1 = (DataGridViewRowCollection)GetControlPropertyValue("tDrawGrid1", "Rows");            
                    // Process the return value    
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error: " + ex.Message);
                    Console.ReadLine();
                }
            }
            static object GetControlPropertyValue( string controlName, string propertyName)
            {
                if (m_TargetForm.InvokeRequired)
                {
                    Thread.Sleep(delay);
                    return m_TargetForm.Invoke(new GetControlPropertyValueHandler(GetControlPropertyValue), new object[] { controlName, propertyName });
                }
                Type t1 = m_TargetForm.GetType();
                FieldInfo fi = t1.GetField(controlName, flags);
                object ctrl = fi.GetValue(m_TargetForm);
                Type t2 = ctrl.GetType();
                PropertyInfo pi = t2.GetProperty(propertyName, flags);
                return pi.GetValue(ctrl, new object[0]);
            }
            delegate object GetControlPropertyValueHandler(string controlName, string propertyName);        static Form LaunchApp(string exePath, string formName)
            {
                Thread.Sleep(delay);
                Assembly a = Assembly.LoadFrom(exePath);
                Type formType = a.GetType(formName);
                Form resultForm = (Form)a.CreateInstance(formType.FullName);
                Thread t = new Thread(new ThreadStart(new AppState(resultForm).RunApp));
                t.ApartmentState = ApartmentState.STA;
                t.IsBackground = true;
                t.Start();
                return resultForm;
            }        private class AppState
            {
                public AppState(Form f) { FormToRun = f; }
                public readonly Form FormToRun;
                public void RunApp()
                {
                    Application.Run(FormToRun);
                }
            }
        }
    }
      

  7.   


    因为程序我没有源代码,程序应该是用delphi写的,TDrawGrid控件名称也无法获取到,但是可以得到句柄,是否可以对这个控件的内容进行操作呢?我试过如果是TextBox的话,可以通过SendMessage或PostMessage方法修改控件内容或是获取控件内容信息,但是TDrawGrid不知道怎么下手.
      

  8.   

    这个我以前做过,不过是C++的。
    如果是用C#的话,就要引入许多的WindowsAPI。
    基本思路好像是:
    找到别人的应用程序,列举当前窗口所有的控件,
    直到找到你要的GridView。
    列举GridView里列和行,发送消息过去,获得数据。具体的我记不清了。以前的一个Demo,仅供参考下载完的压缩包里,先运行Exec.exe,再运行EnumControls.exe,
    单击【列举窗体中的控件】,在点击复制SG中的数据,
    就可以把Exec.exe里的数据拷贝到EnumControls.exe中了。
      

  9.   

    我刚看了下,我的那个文件夹下没有提供
    HookSG.dll 这个文件的代码。
    我回头找找,给发上来。
    不过调用这个动态库,应该可以读取到你要的数据。
      

  10.   


    我可以通过外部工具得到Grid控件的句柄,应该可以直接获取到控件的,现在主要是有三个问题:
    1、用: DataGridViewRowCollection 是否可以把TDrawGrid转过来?
    2、如果可以,怎么可获取到他的行和列呢?
    3、怎么查读到值?
      

  11.   

    纯属个人观点:不可以转换过来的。
    你用我的那个测试Demo可以读取到数据的。
    我就找找动态库的资料发给你,或者你自己也可以找找看。
    从StringGrid里读取数据的这个例子,我是从网上查资料的来的。
      

  12.   


    StringGrid和TDrawGrid是同一个控件吗?
      

  13.   

    我之前也在网上下载了两个demo,但是运行的时候,都处于假死状态,不知道是什么原因。
      

  14.   

    StringGrid是从DrawGrid继承下来的,使用的时候应该不会有什么问题的。
    当然,我没有亲自试过。
      

  15.   


    你那个demo是delphi的吗?有没有c#的啊?
      

  16.   

    这个贴结了,另开了两个贴 RMB求购C#读取TDrawGrid表格数据源代码
    http://topic.csdn.net/u/20100104/09/d0f7a2a1-8dcb-4bef-9e0f-aff6732847d5.html 1000分求购C#读取TDrawGrid表格数据源代码
    http://topic.csdn.net/u/20100106/09/235527a4-1ea0-4b43-8637-ca9dd528865c.html
    有办法解决的哥们,请看上面的两个链接,谢谢