C#通过句柄如何得到 TStringGrid 的数据

csharpcn 2011-08-16 07:56:55
通过spy++已经得到 TStringGrid 的句柄
请问

用C# 如何能得到 TStringGrid 中的数据

以前应该有人问过,没查到,请知道的告诉一下,最好有部分源代码

引用 using System.Runtime.InteropServices;
和 [DllImport("User32.dll", EntryPoint = "FindWindow")] 都是必须的,这步介绍可以省略,可以节省回答人的时间,谢谢大家帮忙
...全文
488 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
csharpcn 2011-08-18
  • 打赏
  • 举报
回复
谢谢大家的回复
csharpcn 2011-08-18
  • 打赏
  • 举报
回复
没有解决,再想其他方法吧
LMAOhuaNL 2011-08-17
  • 打赏
  • 举报
回复
StringGrid1-> Rows[index] 获得第 index 行的内容,返回值为 TStrings* 类型。
csharpcn 2011-08-17
  • 打赏
  • 举报
回复
可能比较难解决,这个问题晚点结贴,希望能有c#高手,给出代码。
谢谢大家
mjp1234airen4385 2011-08-17
  • 打赏
  • 举报
回复
是一个以前的示例代码,写的比较乱,也没有注释。
虽然写成功了,最后也没有用到。
mjp1234airen4385 2011-08-17
  • 打赏
  • 举报
回复
以前做个这个,不过是C++的,使用了hook方法。
你可以参考
下载完了,运行Exec这个程序,是TStringGrid的示例,运行EnumControls.exe,
点击【列举窗体中的控件】按钮,就获取了TStringGrid的句柄,
点击【复制SG中的数据】就可以把Exec这个程序中TStringGrid的数据复制到EnumControls.exe了。
核心代码是这个:
HWND DLLHWND;
void __stdcall (*InstallHook)(HWND, HWND);
void __stdcall (*UninstallHook)();
char * __stdcall (*GetHookedCell)();

DLLHWND = LoadLibrary("HookSG.dll");
InstallHook = (void (__stdcall *)(HWND, HWND))GetProcAddress(DLLHWND, "InstallHook");
UninstallHook = (void (__stdcall *)())GetProcAddress(DLLHWND, "UninstallHook");
GetHookedCell = (char * (__stdcall *)())GetProcAddress(DLLHWND, "GetHookedCell");

//AnsiString StrHwnd = SG->Cells[2][SelectedRow];
//if (StrHwnd == "") return;
//HWND hWnd = (void *)(StrHwnd.ToInt());
//HWND hParent = GetParent(hWnd);

//HWND hParent = FindWindow("TForm1", "列举窗体中的控件");
//HWND hParent = FindWindow("TForm1", "Form1");
//HWND hParent = FindWindow("TFrmSGHooked", "StringGrid to be hooked");
char cText[MAX_PATH] = {0};
strcpy(cText, Edit1->Text.c_str());
if (strcmp(cText, "") == 0) return;
HWND hParent = FindWindow(NULL, cText);
HWND hWnd = FindWindowEx(hParent, 0, "TStringGrid", NULL);
if (!hWnd) return;
InstallHook(Handle, hWnd);
SendMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0, 0);
PostMessage(hParent, WM_NULL, 0, 0);

for (int i = 0; i < 20; i++)
{
Sleep(100);
Application->ProcessMessages();
if (MsgWnd != 0) break;
}
if (MsgWnd == 0) goto Exit;

int iRow, iCol, iLen;
iRow = SendMessage(MsgWnd, CM_QUERYROW, 0, 0);
iCol = SendMessage(MsgWnd, CM_QUERYCOL, 0, 0);
if (iRow <= 0 || iCol <= 0) goto Exit;
SGSub->RowCount = iRow;
SGSub->ColCount = iCol;
for (int i = 0; i < iRow; i++)
{
for (int j = 0; j < iCol; j++)
{
iLen = SendMessage(MsgWnd, CM_HOOKCELL, j, i);
if (iLen < 0) goto Exit;
SGSub->Cells[j][i] = GetHookedCell();
}
}

Exit:
MsgWnd = 0;
UninstallHook();
SendMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0, 0);
FreeLibrary(DLLHWND);
mjp1234airen4385 2011-08-17
  • 打赏
  • 举报
回复
C#中,例如:Form1.Handle就是句柄的意思了。
原来不是怕 2011-08-17
  • 打赏
  • 举报
回复
请问C#有句柄这个概念嘛
stszd604 2011-08-17
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 wuzhanhui 的回复:]
引用 4 楼 lmaohuanl 的回复:
StringGrid1-> Rows[index] 获得第 index 行的内容,返回值为 TStrings* 类型。
解决了吗
[/Quote]
解决了吗
csharpcn 2011-08-16
  • 打赏
  • 举报
回复
一般情况下,可以通过句柄得到输入框和标题栏的内容,但是这个
TStringGrid 中的数据

不知道怎么能得到
ycproc 2011-08-16
  • 打赏
  • 举报
回复
你想调用它 首先得知道他的返回吧

知道了返回再去接收 处理成自己的
Jxiaoshen 2011-08-16
  • 打赏
  • 举报
回复
帮顶~~

111,112

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧