自定义对话框 splitter Wnd

下面的代码中, DC的画图坐标是屏幕坐标。当鼠标 按下,移动  时绘制的拖拽splitter是直接在屏幕上绘制的,坐标也是屏幕坐标。

 

其中涉及到画笔的异或操作,下面是测试代码:

 

 CRect lrect;
 lrect.left = lrect.top = 0;
 lrect.right = lrect.bottom = 100;
 CPen lPen(0, 5, RGB(255,0,0));
 CPen *loPen = dc.SelectObject( &lPen );

// 设置画笔的异或操作

 int loOop2= dc.SetROP2(R2_NOTXORPEN);

// ResultColr = OrigColr^CurPenColr; 

// 结果颜色为:原象素 异或 当前画笔颜色

dc.MoveTo(0,0);
 dc.LineTo(100, 100);
 
 Sleep(1000);

//恢复原象素

 dc.MoveTo(0,0);
 dc.LineTo(100, 100);

 

 dc.SetROP2(loOop2); 
 dc.SelectObject(loPen);

 

 

核心代码如下:

 

鼠标按下:

void CSplitterControl::OnLButtonDown(UINT nFlags, CPoint point) { CStatic::OnLButtonDown(nFlags, point); m_bIsPressed = TRUE; //捕获鼠标 SetCapture(); CRect rcWnd; GetWindowRect(rcWnd); if (m_nType == SPS_VERTICAL) m_nX = rcWnd.left + rcWnd.Width() / 2; //在竖直条中间 else m_nY = rcWnd.top + rcWnd.Height() / 2; // 在水平条中间 if (m_nType == SPS_VERTICAL) m_nSavePos = m_nX; else m_nSavePos = m_nY; // 在整个屏幕上绘制, 需要屏幕坐标 CWindowDC dc(NULL); DrawLine(&dc, m_nX, m_nY); }

 

 

 

光标移动的操作:

void CSplitterControl::OnMouseMove(UINT nFlags, CPoint point) { if (m_bIsPressed) { CWindowDC dc(NULL); DrawLine(&dc, m_nX, m_nY); CPoint pt = point; ClientToScreen(&pt); GetParent()->ScreenToClient(&pt); // 屏幕坐标转换为相对父窗口客户端坐标 // 检查坐标是否超出范围 if (pt.x < m_nMin) pt.x = m_nMin; if (pt.y < m_nMin) pt.y = m_nMin; if (pt.x > m_nMax) pt.x = m_nMax; if (pt.y > m_nMax) pt.y = m_nMax; // 转换到频道坐标 GetParent()->ClientToScreen(&pt); m_nX = pt.x; m_nY = pt.y; DrawLine(&dc, m_nX, m_nY); } CStatic::OnMouseMove(nFlags, point); }

 

鼠标释放:

void CSplitterControl::OnLButtonUp(UINT nFlags, CPoint point) { if (m_bIsPressed) { ClientToScreen(&point); CWindowDC dc(NULL); DrawLine(&dc, m_nX, m_nY); CPoint pt(m_nX, m_nY); m_bIsPressed = FALSE; CWnd *pOwner = GetOwner(); if (pOwner && IsWindow(pOwner->m_hWnd)) { CRect rc; int delta; pOwner->GetClientRect(rc); pOwner->ScreenToClient(&pt); MoveWindowTo(pt); if (m_nType == SPS_VERTICAL) delta = m_nX - m_nSavePos; else delta = m_nY - m_nSavePos; SPC_NMHDR nmsp; nmsp.hdr.hwndFrom = m_hWnd; nmsp.hdr.idFrom = GetDlgCtrlID(); nmsp.hdr.code = SPN_SIZED; nmsp.delta = delta; // 通知父窗口调整各窗口的大小 pOwner->SendMessage(WM_NOTIFY, nmsp.hdr.idFrom, (LPARAM)&nmsp); } } CStatic::OnLButtonUp(nFlags, point); // 释放鼠标捕获 ReleaseCapture(); }

 

 父窗口接收到splitter 改变通知:

 

if (message == WM_NOTIFY) { if (wParam == IDC_SPLITTER1) { SPC_NMHDR* pHdr = (SPC_NMHDR*) lParam; CSplitterControl::ChangeWidth(&m_wndType, pHdr->delta); CSplitterControl::ChangeWidth(&m_lstItem, -pHdr->delta, CW_RIGHTALIGN); CSplitterControl::ChangeWidth(&m_txtContent, -pHdr->delta, CW_RIGHTALIGN); CSplitterControl::ChangeWidth(&m_wndSplitter2, -pHdr->delta, CW_RIGHTALIGN); } }

 

你可能感兴趣的:(自定义对话框 splitter Wnd)