|
在《SDK实现分隔条》中,我介绍了如何实现动态的分隔条。本文将要介绍的,是如何实现静态的分隔条(即画线实现分隔的分隔条)。
上边的分隔条是一种“动态”的分隔条,就是说在移动分隔条的同时窗口的大小也发生了改变。以下我再介绍一种“静态”的分隔条,即在拖动分隔条的时候出现一条竖线,由这一条线来指示分隔条的分隔结果。 如果说动态的分隔条是在和矩形做游戏,那么静态的分隔条就是在和图形做游戏了。首先请大家看我的代码: LRESULT CALLBACK ProcSplitter(HWND hwnd, UINT Msg, WPARAM wParam, LPARAM lParam) { static int x; switch (Msg) { case WM_LBUTTONDOWN: { HDC hdc; RECT rectTree; SetCapture(hwnd); GetWindowRect(hTree, &rectTree); hdc = GetDC(GetParent(hwnd)); SelectObject(hdc, CreatePen(PS_SOLID, 2, 0)); SetROP2(hdc, R2_NOTXORPEN); x = rectTree.right - rectTree.left; MoveToEx(hdc, x, 0, NULL); LineTo(hdc, x, rectTree.bottom - rectTree.top); ReleaseDC(GetParent(hwnd), hdc); } break; case WM_LBUTTONUP: { HDWP hdwp; HDC hdc; RECT rect, rectTree, rectStatus; GetClientRect(GetParent(hwnd), &rect); GetWindowRect(hTree, &rectTree); GetClientRect(hStatus, &rectStatus); hdc = GetDC(GetParent(hwnd)); SelectObject(hdc, CreatePen(PS_SOLID, 2, 0)); SetROP2(hdc, R2_NOTXORPEN); MoveToEx(hdc, x, 0, NULL); LineTo(hdc, x, rectTree.bottom - rectTree.top); ReleaseDC(GetParent(hwnd), hdc); ReleaseCapture(); hdwp = BeginDeferWindowPos(3); DeferWindowPos(hdwp, hTree, NULL, 0, 0, x, rect.bottom - rectStatus.bottom, SWP_NOMOVE | SWP_NOZORDER); DeferWindowPos(hdwp, hSplitter, NULL, x, 0, 0, 0, SWP_NOSIZE | SWP_NOZORDER); DeferWindowPos(hdwp, hList, NULL, x + 2, 0, rect.right - x - 2, rect.bottom - rectStatus.bottom, SWP_NOZORDER); EndDeferWindowPos(hdwp); } break; case WM_MOUSEMOVE: { if ((wParam & MK_LBUTTON) == MK_LBUTTON && GetCapture() == hwnd) { HDC hdc; RECT rectTree; GetWindowRect(hTree, &rectTree); hdc = GetDC(GetParent(hwnd)); SelectObject(hdc, CreatePen(PS_SOLID, 2, 0)); SetROP2(hdc, R2_NOTXORPEN); MoveToEx(hdc, x, 0, NULL); LineTo(hdc, x, rectTree.bottom - rectTree.top); x = rectTree.right - rectTree.left + (short)LOWORD(lParam); MoveToEx(hdc, x, 0, NULL); LineTo(hdc, x, rectTree.bottom - rectTree.top); ReleaseDC(GetParent(hwnd), hdc); } } break; default: return DefWindowProc(hwnd, Msg, wParam, lParam); } return 0; } 这段代码的核心部分,就是它的画线和擦线部分。在这里我玩弄了一个小把戏,就是利用了SetROP2函数的R2_NOTXORPEN模式:在这个模式下作图,只要在先前画过线的地方再画一道线,那么用户看到的效果就是原来的线被擦除了。这样一来,只需要再使用一个static变量x,就可以完成这个过程了。 当然分隔条的种类有很多,例如VCL中的TSplitter中就包括了不同样式的分隔条,不过效果无非是改变了画线的样式之类,在此我就不多讨论了。 |