目前共有5篇帖子。
字體大小:較小 - 100% (默認)  內容轉換:不轉換
 
點擊 回復
1073 4
用Windows API制作窗口切分滑块的方法
巨大八爪鱼
武林盟主 二十一級
回復
1樓 發表於:2016-2-12 21:13
http://www.catch22.net/tuts/splitter-windows
巨大八爪鱼
武林盟主 二十一級
回復
2樓 發表於:2016-2-12 21:14
https://social.msdn.microsoft.com/Forums/en-US/e5a9a559-fd77-421c-b076-284f83c1159c/resizing-windows-and-their-controls?forum=windowsgeneraldevelopmentissues
巨大八爪鱼
武林盟主 二十一級
回復
3樓 發表於:2016-2-13 21:59
#include <Windows.h>

#define SPLITTER_HEIGHT 4

BOOL fDragMode = FALSE;
int splitpos = 100, old_splitpos;

void DrawXorBar(HDC hdc, int x1, int y1, int width, int height)
{
    static WORD dotPatternBmp[8] = {0x00aa, 0x0055, 0x00aa, 0x0055, 0x00aa, 0x0055, 0x00aa, 0x0055};
    HBITMAP hbmp = CreateBitmap(8, 8, 1, 1, dotPatternBmp);
    HBRUSH hbr = CreatePatternBrush(hbmp);
    SetBrushOrgEx(hdc, x1, y1, NULL);
    HBRUSH hbrOld = (HBRUSH)SelectObject(hdc, hbr);
    PatBlt(hdc, x1, y1, width, height, PATINVERT);
    SelectObject(hdc, hbrOld);
    DeleteObject(hbr);
    DeleteObject(hbmp);
}

void DrawXorBar(HWND hWnd, HDC hdc, int pos)
{
    RECT rcClient;
    GetClientRect(hWnd, &rcClient);
    DrawXorBar(hdc, rcClient.left, pos, rcClient.right - rcClient.left, SPLITTER_HEIGHT);
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    HDC hdc;
    switch (uMsg)
    {
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    case WM_LBUTTONDOWN:
        fDragMode = TRUE;
        SetCapture(hWnd);
        
        hdc = GetDC(hWnd);
        DrawXorBar(hWnd, hdc, splitpos);
        ReleaseDC(hWnd, hdc);
        DeleteDC(hdc);

        old_splitpos = splitpos;
        break;
    case WM_LBUTTONUP:
        if (fDragMode == TRUE)
        {
            fDragMode = FALSE;
            ReleaseCapture();

            hdc = GetDC(hWnd);
            DrawXorBar(hWnd, hdc, old_splitpos);
            ReleaseDC(hWnd, hdc);
            DeleteDC(hdc);
        }
        break;
    case WM_MOUSEMOVE:
        if (fDragMode == TRUE)
        {
            POINT pos;
            GetCursorPos(&pos);
            ScreenToClient(hWnd, &pos);
            splitpos = pos.y;
            hdc = GetDC(hWnd);
            DrawXorBar(hWnd, hdc, old_splitpos);
            DrawXorBar(hWnd, hdc, splitpos);
            ReleaseDC(hWnd, hdc);
            DeleteDC(hdc);
            old_splitpos = splitpos;
        }
        break;
    default:
        return DefWindowProc(hWnd, uMsg, wParam, lParam);
    }
    return FALSE;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASS wc;
    wc.cbClsExtra = wc.cbWndExtra = 0;
    wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
    wc.hCursor = LoadCursor(NULL, IDC_SIZENS);
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wc.hInstance = hInstance;
    wc.lpfnWndProc = WndProc;
    wc.lpszClassName = "Splitter Example";
    wc.lpszMenuName = NULL;
    wc.style = NULL;
    RegisterClass(&wc);

    HWND hWnd = CreateWindow(wc.lpszClassName, "Splitter", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
    if (!hWnd)
        return 1;
    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);

    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam;
}
巨大八爪鱼
武林盟主 二十一級
回復
4樓 發表於:2016-2-13 22:01
巨大八爪鱼
武林盟主 二十一級
回復
5樓 發表於:2016-2-14 19:02
【补充】
ReleaseDC(hWnd, hdc);
后面的DeleteDC(hdc);要去掉
回覆帖子
內容:
用戶名: 您目前是匿名發表。
驗證碼:
看不清?換一張
(快捷鍵:Ctrl+Enter)
本帖信息
點擊數:1073 回複數:4
作者:巨大八爪鱼
最後回覆:巨大八爪鱼
最後回復時間:2016-2-14 19:02
本吧精品帖子
公告板