导航
  • 主页
  • VC综合技术
  • 互联网技术
  • MFC AppLauncher
  • .NET 技术
  • 界面
  • 进程
  • 算法
  • 硬件/系统
  • 数据库
  • VC++技术资源

高分求教一关于SendMessage的简单问题

guxing 2004-04-08 08:26:08
我想用SendMessage
得到鼠标当前指向的SysTreeView32 的Item的值

TVHITTESTINFO tf

lpMouseHookStruct = (LPMOUSEHOOKSTRUCT)lParam;
pt.x = lpMouseHookStruct->pt.x;//(LONG) pts.x;
pt.y = lpMouseHookStruct->pt.y;//(LONG) pts.y;
::ClientToScreen(OrgWnd, &pt );
tf.pt = pt;
tf.flags = TVHT_ONITEMLABEL;

SendMessage(OrgWnd,TVM_HITTEST,0, LPARAM);//
//上面的LPARAM 不知怎么用tf写,我是菜鸟,请指教
请给出完整代码,马上结贴
...全文
79 点赞 收藏 24
写回复
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
guxing 2004-04-10
AdamBear(毛毛熊) (
多谢你,
我按你的改了,但还是乱码?
麻烦各位再帮我看看
LRESULT __declspec(dllexport)__stdcall CALLBACK MouseProc(int nCode,WPARAM wParam,LPARAM lParam)
{
if(nCode>=0)
{
LPMOUSEHOOKSTRUCT pMouseHook=(MOUSEHOOKSTRUCT FAR *) lParam;
HWND glhTargetWnd=pMouseHook->hwnd;//取目标窗口句柄
HWND ParentWnd=glhTargetWnd;
HWND OrgWnd =glhTargetWnd;
char szClassName[100],szPClassName[100];
//取目标窗口Classname
::GetClassName(ParentWnd,szClassName,100);
if((wParam==WM_LBUTTONDOWN || wParam==WM_LBUTTONDBLCLK) && (!lstrcmp(szClassName,"SysTreeView32") || !lstrcmp(szClassName,"SysListView32")))
{
while (ParentWnd !=NULL)
{
glhTargetWnd=ParentWnd;
ParentWnd= ::GetParent(glhTargetWnd);
//取应用程序主窗口句柄
}
::GetClassName(glhTargetWnd,szPClassName,100);
if(!lstrcmp(szPClassName,"ExploreWClass") || (!lstrcmp(szPClassName,"CabinetWClass") && !lstrcmp(szClassName,"SysListView32")))
{
HWND ltWnd =NULL;//树\列表的Item句柄
POINTS pts;
POINT pt;
TVHITTESTINFO tf ;
TV_ITEM tv ;
//LPMOUSEHOOKSTRUCT lpMouseHookStruct = NULL;
//LPTVITEM pitem=NULL;//Tree List Item //DWORD hitem=NULL;//Tree List Item Handle
HGLOBAL hglbCopy;
LPTSTR lptstrCopy;
TCHAR szDiskName[1024];
//得到当前盘符
if(!lstrcmp(szClassName,"SysTreeView32"))
{
lptstrCopy = NULL;
//Get the currently selected item SendMessage
pts = MAKEPOINTS( lParam );
pt.x = (LONG) pts.x;
pt.y = (LONG) pts.y;
::ClientToScreen(OrgWnd, &pt );
tf.pt = pt;
tf.flags = TVHT_ONITEMLABEL;
tf.hItem = NULL;
//获得光标所在的Item的句柄
::SendMessage(OrgWnd,TVM_HITTEST,0, (LPARAM) (TVHITTESTINFO *) &tf);//TVHITTESTINFO
ltWnd = (HWND)tf.hItem; //((PCWPSTRUCT)hitem)->hwnd;
//分配一定的内存空间用以存储Item的标题
hglbCopy = GlobalAlloc( GMEM_FIXED, 1024 );
lptstrCopy = (LPTSTR)GlobalLock(hglbCopy);
tv.mask = TVIF_TEXT;//获取标题文本
tv.hItem = (HTREEITEM)ltWnd;//Item句柄
tv.pszText = lptstrCopy;
tv.cchTextMax = 1023;

//发送TVM_GETITEM获得标题文本
::SendMessage(OrgWnd,TVM_GETITEM,0,(LPARAM)(TV_ITEM *)&tv);//(LPTVITEM)
_tcsncpy(szDiskName,lptstrCopy,1024);
GlobalFree(hglbCopy);
::SendMessage(glhTargetWnd,WM_SETTEXT,0,(LPARAM)(LPTSTR)szDiskName);//(LPARAM)(LPCTSTR)szDiskName

//::SendMessage(g_hWnd,WM_USER,0,0);//(LPARAM)(LPCTSTR)szDiskName
return 1;
if(szDiskName[2] =='E')//判断是E盘则控制,否则继续
{
return 1;
}
}
else
{
}
}
}
}
return CallNextHookEx(glhHook,nCode,wParam,lParam);
}
回复
guxing 2004-04-09
这是VB的呀
改为
lptstrCopy = NULL;
//Get the currently selected item SendMessage
//SendMessage(OrgWnd,TVM_GETNEXTITEM,TVGN_CARET,hitem);
//Get Current Curpos
pts = MAKEPOINTS( lParam );
pt.x = (LONG) pts.x;
pt.y = (LONG) pts.y;

//lpMouseHookStruct = (LPMOUSEHOOKSTRUCT)lParam;
//pt.x = lpMouseHookStruct->pt.x;//(LONG) pts.x;
//pt.y = lpMouseHookStruct->pt.y;//(LONG) pts.y;
::ClientToScreen(OrgWnd, &pt );

tf.pt = pt;
tf.flags = TVHT_ONITEMLABEL;
tf.hItem = NULL;
//获得光标所在的Item的句柄
::SendMessage(OrgWnd,TVM_HITTEST,0, (LPARAM) &tf);//TVHITTESTINFO
ltWnd = (HWND)tf.hItem; //((PCWPSTRUCT)hitem)->hwnd;
//分配一定的内存空间用以存储Item的标题
hglbCopy = GlobalAlloc( GMEM_DDESHARE|GMEM_MOVEABLE, 1024 );
lptstrCopy = (LPTSTR)GlobalLock(hglbCopy);

tv.mask = TVIF_TEXT;//获取标题文本
tv.hItem = (HTREEITEM)ltWnd;//Item句柄
tv.pszText = lptstrCopy;
tv.cchTextMax = 1023;
//发送TVM_GETITEM获得标题文本
::SendMessage(OrgWnd,TVM_GETITEM,0,(LPARAM) &tv);//(LPTVITEM)
//strcpy(szDiskName,((TVITEM *)pitem)->pszText);
::SendMessage(OrgWnd,TVM_SELECTITEM,0,(LPARAM) &tv);//(LPTVITEM)
strcpy(szDiskName, lptstrCopy );
GlobalUnlock(hglbCopy);
::SendMessage(glhTargetWnd,WM_SETTEXT,0,(LPARAM)(LPCTSTR)szDiskName);//(LPARAM)(LPCTSTR)szDiskName
if((fp =fopen("c:\\share1.txt", "r")) !=NULL)
{
fscanf(fp, "%d %d %d %s", <Wnd, &pt.x, &pt.y,szDiskName);
fclose(fp);
}
//::SendMessage(OrgWnd,WM_SETTEXT,0,(LPARAM)(LPCTSTR)"hookhoo");//(LPARAM)(LPCTSTR)szDiskName
return 1;

以上代码,只是szDiskName为乱码,请高手帮改一下

回复
关注
回复
glint 2004-04-09
关注
回复
DarkTemplar1999 2004-04-09
数据类型和指针类型双重强制转换
回复
老熊宝宝 2004-04-09
代码贴完整一点行吗?szDiskName是怎么声明的,工程是怎么设置字符集的?
只用眼睛看这段代码应该没什么问题,我怀疑之所以是乱码和你的字符集设置有关,比如你设置了MBCS,但是你现在用的是Ansi版本的strcpy,就会有乱码的。
还有不要用strcpy,可能会产生缓冲区溢出漏洞的

最好把字串是声明成TCHAR的,然后用_tcsncpy来拷贝字符串,这样无论设什么字符集都可以用的

还有
::SendMessage(OrgWnd,TVM_GETITEM,0,(LPARAM) &tv)
最好这样写,最好不要把类型丢了:
::SendMessage(OrgWnd,TVM_GETITEM,0,(LPARAM)(TV_ITEM *)&tv)
回复
guxing 2004-04-09
谢谢,可以惜我现在办法 试
回复
itmaster 2004-04-08
给你个例子吧
http://www.applevb.com/art/tg3.txt
回复
guxing 2004-04-08
多谢楼上兄弟
等待中
回复
jack518 2004-04-08
期待高手!!!重赏
回复
kyoghost 2004-04-08
不会,想知道结果
回复
newguguo 2004-04-08
不会,想知道结果
回复
jack518 2004-04-08
期待高手
回复
guxing 2004-04-08
lptstrCopy = NULL;
//Get the currently selected item SendMessage
//SendMessage(OrgWnd,TVM_GETNEXTITEM,TVGN_CARET,hitem);
//Get Current Curpos
//pts = MAKEPOINTS( lParam );
lpMouseHookStruct = (LPMOUSEHOOKSTRUCT)lParam;
pt.x = lpMouseHookStruct->pt.x;//(LONG) pts.x;
pt.y = lpMouseHookStruct->pt.y;//(LONG) pts.y;
::ClientToScreen(OrgWnd, &pt );
tf.pt = pt;
tf.flags = TVHT_ONITEMLABEL;
tf.hItem = NULL;
//获得光标所在的Item的句柄
::SendMessage(OrgWnd,TVM_HITTEST,0, (LPARAM)tf.hItem);//TVHITTESTINFO
ltWnd = (HWND)tf.hItem; //((PCWPSTRUCT)hitem)->hwnd;
//分配一定的内存空间用以存储Item的标题
hglbCopy = GlobalAlloc( GMEM_DDESHARE|GMEM_MOVEABLE, 1024 );
lptstrCopy = (LPTSTR)GlobalLock(hglbCopy);

tv.mask = TVIF_TEXT;
//获取标题文本
tv.hItem = (HTREEITEM)ltWnd;

//Item句柄
tv.pszText = lptstrCopy;
tv.cchTextMax = 1023;
//发送TVM_GETITEM获得标题文本
::SendMessage(ltWnd,TVM_GETITEM,0,(LPARAM)tv.hItem);//(LPTVITEM)
//strcpy(szDiskName,((TVITEM *)pitem)->pszText);
strcpy(szDiskName, lptstrCopy );
GlobalUnlock(hglbCopy);

以上代码可以通过
但得到的szDiskName 不对
是乱码
上面是鼠标HOOK中的
请哪位高手帮我改一下
回复
guxing 2004-04-08
D
回复
guxing 2004-04-08
//获得光标所在的Item的句柄
::SendMessage(OrgWnd,TVM_HITTEST,0, (LPARAM)tf.hItem);//TVHITTESTINFO
ltWnd = (TVHITTESTINFO)tf.hItem->hwnd; //((PCWPSTRUCT)hitem)->hwnd;
//分配一定的内存空间用以存储Item的标题
hglbCopy = GlobalAlloc( GMEM_DDESHARE|GMEM_MOVEABLE, 1024 );
//lptstrCopy = GlobalLock(hglbCopy);


tv.mask = TVIF_TEXT;
//获取标题文本
//tv.HTREEITEM = ltWnd;

//Item句柄
tv.pszText = lptstrCopy;
tv.cchTextMax = 1023;
//发送TVM_GETITEM获得标题文本
// ::SendMessage(ltWnd,TVM_GETITEM,0,(LPARAM) (LPTVITEM) tv);//(LPTVITEM)
//strcpy(szDiskName,((TVITEM *)pitem)->pszText);
strcpy(szDiskName, lptstrCopy );
GlobalUnlock(hglbCopy);

哪位高手帮我改一下上面的代码
实现szDiskName中得到树的ITEM的值
多谢,搞定就结帐
回复
远古猛犸象 2004-04-08
使用的时候再转回来
(HTREEITEM)
回复
远古猛犸象 2004-04-08
强制转类型
(LPARAM)tf.hItem
回复
guxing 2004-04-08
就是说我想传入一个结构(可以接收返回值),要怎么写
回复
guxing 2004-04-08
error C2664: 'SendMessageA' : cannot convert parameter 4 from 'struct _TREEITEM *' to 'long'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast

不行的
回复
发动态
发帖子
VC/MFC
创建于2007-09-28

1.5w+

社区成员

VC/MFC相关问题讨论
申请成为版主
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……