前两天学习了MFC的拆分窗口,今天来学习Win32 SDK下如何拆分窗口.
win32是没有像MFC那样直接有函数方法拆分窗口,只能自己处理了.
1.在WM_CREATE消息中创建两个控件,TreeView,ListView
//创建左边树形视图控件TreeView,扩展风格样式为3D风格WS_EX_CLIENTEDGE HWND m_hTree = CreateWindowEx(WS_EX_CLIENTEDGE, WC_TREEVIEW, NULL, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS, 0, 0, 0, 0, hWnd, NULL, lpCreate->hInstance, NULL); //创建右边列表视图控件ListView HWND m_hList = CreateWindowEx(WS_EX_CLIENTEDGE, WC_LISTVIEW, NULL, WS_VISIBLE | WS_CHILD | WS_CLIPSIBLINGS|LVS_REPORT, 0, 0, 0, 0, hWnd, NULL, lpCreate->hInstance, NULL); //获取窗口客服区大小 RECT rc; GetClientRect(hWnd, &rc); m_splitterPos = rc.right /4; //设定分界线水平位置在窗口水平的1/4 m_hCursor = LoadCursor(NULL, IDC_SIZEWE); //设定在分界线上的鼠标样式为东西方向的箭头光标
2.在WM_SIZE消息中调整两个控件的位置
LRESULT OnSize(UINT nType, int cx, int cy) { //根据分界线的位置,重新设置树形控件和列表控件的位置 //在分界线的位置-1是留下能捕获鼠标消息的位置,不然挤太紧了鼠标移动消息捕获不了. MoveWindow(m_treeCtrl.GetHandle(), 0, 0, m_splitterPos-1, cy, true); MoveWindow(m_listCtrl.GetHandle(), m_splitterPos+1, 0, cx - m_splitterPos, cy, true); //获取列表控件的尺寸 RECT rc; GetClientRect(m_listCtrl.GetHandle(), &rc); //根据列表控件新的大小,重新设置每列的宽度 int nListWidth = rc.right - 20; int nListColwidth = nListWidth / 10; int nCols[] = { nListColwidth*2, nListColwidth*2, nListColwidth, nListColwidth, nListColwidth*4 }; for (int i=0;i<5;i++) { ListView_SetColumnWidth(m_listCtrl.GetHandle(), i, nCols[i]); } return 0; }
3.捕获鼠标消息WM_LBUTTONDOWN,和WM_LBUTTONUP
LRESULT OnLButtonDown(HWND hWnd,UINT nFlags, POINT pos) { if (pos.x >= m_splitterPos - 10 && pos.x <= m_splitterPos + 10) { m_IsSplit = TRUE; //标识可以拖拽 /*SetCursor(m_hCursor); SetCapture(hWnd);*/ } return 0; } LRESULT OnLButtonUp(HWND hWnd, UINT nFlags, POINT pos) { if (m_IsSplit) { m_IsSplit = FALSE; //标识不可拖拽 ReleaseCapture(); } return 0; }
4.在鼠标WM_MOUSEMOVE消息中实现鼠标拖拽
LRESULT OnMouseMove(HWND hWnd, UINT nFlags, POINT pos) { if (pos.x >= m_splitterPos - 10 && pos.x <= m_splitterPos + 10) { //m_IsSplit = TRUE; SetCursor(m_hCursor);//设置鼠标样式为东西方向 SetCapture(hWnd); //捕获鼠标 } else { ReleaseCapture(); //释放鼠标 } if (nFlags == MK_LBUTTON && m_IsSplit)//鼠标拖拽两边控件随着更改位置 { RECT rc; GetClientRect(hWnd, &rc); m_splitterPos = pos.x;
//根据新的分界位置发送WM_SIZE消息,重新调整左右控件的位置 SendMessage(hWnd, WM_SIZE, 0, MAKELPARAM(rc.right, rc.bottom)); } return 0; }
5.效果
为啥截屏没有鼠标样式.