iCAx开思工具箱
标题:
MFC树形控件窗口子例化
[打印本页]
作者:
liuhuapin1981
时间:
2016-12-25 11:02
标题:
MFC树形控件窗口子例化
前几天在网上看到一篇关于MFC树形控件在CATIA窗口上子例化的贴(https://bbs.csdn.net/topics/392061410?page=1)。根据原贴的提示,我在百度网盘的窗口也实现了一样的功能。现总结分享一下:
1.找到目标窗口的句柄:CWnd* pWnd= FindWindow(NULL, _T("欢迎使用百度网盘"));
2.在目标窗口上创建树形控件:m_TreeCtrl.Create(WS_VISIBLE | TVS_HASBUTTONS | TVS_LINESATROOT | TVS_HASLINES
| TVS_NOTOOLTIPS | TVS_EDITLABELS,
CRect(0, 100, 350, 600), pWnd, IDC_TREE1);
m_TreeCtrl.SetBkColor(RGB(230, 250, 250));
3.子例化树形控件:
3.1.设置树形控件窗口新的过程函数并保存原来的过程函数:oldProc = (WNDPROC)::SetWindowLong(g_pTreeCtrl->m_hWnd
, GWL_WNDPROC, (LONG)newProc);
3.2.把树形控件的原过程函数指针存储起来:SetProp(g_pTreeCtrl->m_hWnd, _T("MyProc"), oldProc);
3.3.定义树形控件新的过程函数体:
LRESULT CALLBACK newProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
// afxDump << Msg << "\r\n";
switch (Msg)
{
case WM_MOUSEMOVE://MN_MOUSEMOVE: not comes !
{
if (g_pWnd != NULL)
{
CString str;
CPoint point;
GetCursorPos(&point);//获得鼠标点击的位置
g_pWnd->ScreenToClient(&point);//转化为客户坐
int x = point.x;
int y = point.y;
str.Format(_T("当前坐标 x:%d,y:%d"), x, y);
//g_pWnd->SetWindowText(str);
HDC hDC = GetDC(g_pWnd->m_hWnd);
CDC *pDC = CDC::FromHandle(hDC);
pDC->Rectangle(0,0,300,30);
CRect rect = CRect(20,5,280,25);
pDC->DrawText(str, &rect,0);
//g_pTreeCtrl->Invalidate(TRUE);
//
}
//HWND hdlg = pWnd->GetSafeHwnd();
//
//afxDump << Msg << "\r\n";
break;
// only inside menu
}
case WM_LBUTTONDOWN://MN_BUTTONDOWN://
{
CString m_Lable;
CString str;
CPoint point;
GetCursorPos(&point);//获得鼠标点击的位置
g_pTreeCtrl->ScreenToClient(&point);//转化为客户坐标
UINT uFlags;
HTREEITEM CurrentItem;
CurrentItem = g_pTreeCtrl->HitTest(point, &uFlags);//获
m_Lable = g_pTreeCtrl->GetItemText(CurrentItem);
//AfxMessageBox(m_Lable);
int x = point.x;
int y = point.y;
HDC hDC = GetDC(g_pWnd->m_hWnd);
CDC *pDC = CDC::FromHandle(hDC);
pDC->Rectangle(0, 0, 280, 30);
CRect rect = CRect(20, 5, 270, 25);
pDC->DrawText(m_Lable, &rect, 0);
break;
}
case 0x01EF://MN_BUTTONUP://
//afxDump << Msg << "\r\n";
break;
case WM_DESTROY:
case WM_NCDESTROY:
oldProc = 0;
break;
}
return CallWindowProc((WNDPROC)::GetProp(hWnd, _T("MyProc")), hWnd, Msg, wParam, lParam);
}
4.实现的效果图片:F:\vs2013pro\MFC树形控件.PNG
作者:
liuhuapin1981
时间:
2016-12-25 11:08
F:\\vs2013pro\\MFC树形控件.PNG
作者:
liuhuapin1981
时间:
2016-12-25 11:17
图片贴不上来:有 兴趣的可以观看演示视屏:https://v.youku.com/v_show/id_XMTg4MjA1NjA0OA==.html?from=s1.8-1-1.2&spm=a2h0k.8191407.0.0
作者:
liuhuapin1981
时间:
2016-12-27 20:05
终于把效果图片上传成功。
作者:
liuhuapin1981
时间:
2017-2-22 19:58
原本没有树,摸得多了就长出树了。
作者:
liuhuapin1981
时间:
2017-2-23 18:39
优化继续进行中
作者:
fengzaipiao2017
时间:
2017-9-1 16:17
你好,我想问一下,第3步子例化树形控件的代码应该放在什么位置?m_TreeCtrl与g_pTreeCtrl是同一个吗?有什么区别?
欢迎光临 iCAx开思工具箱 (https://t.icax.org/)
Powered by Discuz! X3.3