lstree.c

来自「CC386 is a general-purpose 32-bit C comp」· C语言 代码 · 共 563 行 · 第 1/2 页

C
563
字号
            case HDN_ENDTRACK:
                SendMessage(ptr->hwndTree, TCF_SETDIVIDER, 0, n->pitem->cxy);
                InvalidateRect(ptr->hwndTree, 0, 1);
                return 0;
            case TVN_GETDISPINFO:
                t = n;
                if (TreeView_GetItemRect(ptr->hwndTree, t->item.hItem, &r, TRUE)
                    )
                {
                    if (ptr->displaycount < DISPLAY_MAX)
                        ptr->displaylist[ptr->displaycount++] = t->item.hItem;
                    strcpy(t->item.pszText, "");
                    t->item.cchTextMax = 0;
                }
                return 0;
            case TVN_DELETEITEM:
                ntv = (NM_TREEVIEW*)lParam;
                if (ptr->displaycount)
                    for (i = 0; i < ptr->displaycount; i++)
                if (ptr->displaylist[i] == ntv->itemOld.hItem)
                {
                    ptr->sel =  - 1;
                    memcpy(&ptr->displaylist[i], &ptr->displaylist[i + 1], (ptr
                        ->displaycount - i - 1) *sizeof(HTREEITEM));
                    ptr->displaycount--;
                    return 0;
                }
                return 0;
            }
            // fall through 
        case WM_COMMAND:
            return SendMessage(GetParent(hwnd), iMessage, wParam, lParam);
        case WM_CREATE:
            ptr = (COLUMNINFO*)HeapAlloc(GetProcessHeap(), 0, sizeof(COLUMNINFO)
                );
            ptr->displaycount = 0;
            ptr->sel =  - 1;
            SetWindowLong(hwnd, 0, (int)ptr);
            GetClientRect(hwnd, &r);
            ptr->hwndHeader = CreateWindow(WC_HEADER, 0, WS_CLIPSIBLINGS |
                WS_CHILD | HDS_HORZ | WS_BORDER, r.left, r.top, r.right -
                r.left, r.bottom - r.top, hwnd, 0, (HINSTANCE)GetWindowLong
                (GetParent(hwnd), GWL_HINSTANCE), 0);
            hdl.prc = &r;
            hdl.pwpos = &wp;
            SendMessage(ptr->hwndHeader, HDM_LAYOUT, 0, (LPARAM) &hdl);
            //         wp.x += 2*GetSystemMetrics(SM_CXDLGFRAME );
            //         wp.cx -= 4 * GetSystemMetrics(SM_CXDLGFRAME) ;
            //         wp.y += 2*GetSystemMetrics(SM_CYDLGFRAME );
            //         wp.cy -= 4 * GetSystemMetrics(SM_CYDLGFRAME) ;
            ptr->watchFont = CreateFontIndirect(&fontdata);
            SendMessage(ptr->hwndHeader, WM_SETFONT, (WPARAM)ptr->watchFont, 0);
            SetWindowPos(ptr->hwndHeader, wp.hwndInsertAfter, wp.x, wp.y, wp.cx,
                wp.cy, wp.flags | SWP_SHOWWINDOW);
            r.left = wp.x;
            r.right = wp.x + wp.cx;
            r.top = wp.y + wp.cy;
            ptr->hwndTree = CreateWindow(szextTreeWindClassName, 0,
                WS_CLIPSIBLINGS | WS_VISIBLE | WS_CHILD | WS_BORDER |
                TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS, r.left, r.top,
                r.right - r.left, r.bottom - r.top, hwnd, (HMENU)448, 
                (HINSTANCE)GetWindowLong(GetParent(hwnd), GWL_HINSTANCE), 0);
            SendMessage(ptr->hwndTree, WM_SETFONT, (WPARAM)ptr->watchFont, 0);
            hie.mask = HDI_WIDTH;
            SendMessage(ptr->hwndHeader, HDM_GETITEM, 0, (LPARAM) &hie);
            SendMessage(ptr->hwndTree, TCF_SETDIVIDER, 0, hie.cxy);
            break;

        case WM_DESTROY:
            ptr = (COLUMNINFO*)GetWindowLong(hwnd, 0);
            DestroyWindow(ptr->hwndHeader);
            DeleteObject(ptr->watchFont);
            HeapFree(GetProcessHeap(), 0, (void*)ptr);
            break;
        case WM_LBUTTONDOWN:
        case WM_RBUTTONDOWN:
            break;
        case WM_SIZE:
            ptr = (COLUMNINFO*)GetWindowLong(hwnd, 0);
            r.left = r.top = 0;
            r.right = LOWORD(lParam);
            r.bottom = HIWORD(lParam);
            hdl.prc = &r;
            hdl.pwpos = &wp;
            SendMessage(ptr->hwndHeader, HDM_LAYOUT, 0, (LPARAM) &hdl);
            //         wp.x += 2*GetSystemMetrics(SM_CXDLGFRAME );
            //         wp.cx -= 4 * GetSystemMetrics(SM_CXDLGFRAME) ;
            //         wp.y += 2*GetSystemMetrics(SM_CYDLGFRAME );
            //         wp.cy -= 4 * GetSystemMetrics(SM_CYDLGFRAME) ;
            SetWindowPos(ptr->hwndHeader, wp.hwndInsertAfter, wp.x, wp.y, wp.cx,
                wp.cy, wp.flags);
            r.left = wp.x;
            r.right = wp.x + wp.cx;
            r.top = wp.y + wp.cy;
            MoveWindow(ptr->hwndTree, r.left, r.top, r.right - r.left, r.bottom
                - r.top, TRUE);
            GetClientRect(ptr->hwndHeader, &r);
            hie.mask = HDI_WIDTH;
            hie.cxy = (r.right - r.left) / 2;
            SendMessage(ptr->hwndHeader, HDM_SETITEM, 0, (LPARAM) &hie);
            SendMessage(ptr->hwndHeader, HDM_SETITEM, 1, (LPARAM) &hie);
            SendMessage(ptr->hwndTree, TCF_SETDIVIDER, 0, hie.cxy);
            InvalidateRect(ptr->hwndHeader, 0, 1);
            break;
        case TCF_SETHEADER:
            ptr = (COLUMNINFO*)GetWindowLong(hwnd, 0);
            h = (TCHeader*)lParam;
            GetWindowRect(ptr->hwndHeader, &r);
            hie.mask = HDI_TEXT | HDI_FORMAT | HDI_WIDTH;
            if (h->colBmp1)
                    hie.mask |= HDI_BITMAP;
            hie.pszText = h->colText1;
            hie.hbm = h->colBmp1;
            hie.cxy = (r.right - r.left) / 2;
            hie.cchTextMax = strlen(h->colText1);
            hie.fmt = HDF_LEFT | HDF_STRING;
            SendMessage(ptr->hwndHeader, HDM_INSERTITEM, 100, (LPARAM) &hie);
            hie.mask = HDI_TEXT | HDI_FORMAT | HDI_WIDTH;
            if (h->colBmp2)
                hie.mask |= HDI_BITMAP;
            hie.pszText = h->colText2;
            hie.hbm = h->colBmp2;
            hie.cxy = (r.right - r.left) / 2;
            hie.cchTextMax = strlen(h->colText2);
            hie.fmt = HDF_LEFT | HDF_STRING;
            SendMessage(ptr->hwndHeader, HDM_INSERTITEM, 100, (LPARAM) &hie);
            SendMessage(ptr->hwndTree, TCF_SETDIVIDER, 0, hie.cxy);
            return 0;
        case WM_PAINT:
            dc = BeginPaint(hwnd, &ps);
            EndPaint(hwnd, &ps);
            break;
        case TCN_PAINT:
            rp = (RECT*)lParam;
            ptr = (COLUMNINFO*)GetWindowLong(hwnd, 0);
            {
                hie.mask = HDI_WIDTH;
                SendMessage(ptr->hwndHeader, HDM_GETITEM, 0, (LPARAM) &hie);
                hie.cxy -= GetSystemMetrics(SM_CXBORDER);
                rp->left = hie.cxy;
                if (rp->left < rp->right)
                {
                    LOGBRUSH lbrush;
                    HBRUSH graybrush;
                    HDC dc;
                    int lined = FALSE;
                    HFONT font = (HFONT)SendMessage(ptr->hwndTree, WM_GETFONT,
                        0, 0);
                    lbrush.lbStyle = BS_SOLID;
                    lbrush.lbColor = 0xff0000;
                    graybrush = CreateBrushIndirect(&lbrush);
                    dc = GetDC(ptr->hwndTree);
                    font = SelectObject(dc, font);
                    if (GetWindowLong(hwnd, GWL_STYLE) &TCS_LINE)
                    {
                        HPEN pen;
                        pen = CreatePen(PS_SOLID, 1, 0);

                        lined = TRUE;
                        pen = SelectObject(dc, pen);
                        MoveToEx(dc, rp->left, rp->top, 0);
                        for (i = 0; i < rp->bottom - rp->top; i += 2)
                        {
                            MoveToEx(dc, rp->left, rp->top + i, 0);
                            LineTo(dc, rp->left, rp->top + i + 1);
                        }
                        pen = SelectObject(dc, pen);
                        DeleteObject(pen);
                    }
                    for (i = 0; i < ptr->displaycount; i++)
                    {
                        COLORREF color, bgcolor;
                        item.hItem = ptr->displaylist[i];
                        item.mask = TVIF_PARAM;
                        TreeView_GetItem(ptr->hwndTree, &item);
                        td = (TCData*)item.lParam;
                        TreeView_GetItemRect(ptr->hwndTree, ptr->displaylist[i],
                            &r, TRUE);
                        if (td->col1Text)
                        {
                            HRGN rgn;
                            rgn = CreateRectRgn(r.left, r.top, rp->left - 2,
                                r.bottom);
                            SelectClipRgn(dc, rgn);
                            if (ptr->sel == i)
                            {
                                color = SetTextColor(dc, 0xffffff);
                                bgcolor = SetBkColor(dc, td->col1Color);
                            }
                            else
                                color = SetTextColor(dc, td->col1Color);
                            TextOut(dc, r.left, r.top, td->col1Text, strlen(td
                                ->col1Text));
                            SetTextColor(dc, color);
                            if (ptr->sel == i)
                                SetBkColor(dc, bgcolor);
                            rgn = SelectClipRgn(dc, NULL);
                            DeleteObject(rgn);
                        }
                        if (td->col2Text)
                        {
                            if (ptr->sel == i)
                            {
                                color = SetTextColor(dc, 0xffffff);
                                bgcolor = SetBkColor(dc, td->col2Color);
                            }
                            else
                                color = SetTextColor(dc, td->col2Color);
                            TextOut(dc, rp->left + (lined ? 3 : 0), r.top, td
                                ->col2Text, strlen(td->col2Text));
                            SetTextColor(dc, color);
                            if (ptr->sel == i)
                                SetBkColor(dc, bgcolor);
                        }
                    }
                    SelectObject(dc, font);
                    ReleaseDC(hwnd, dc);
                    DeleteObject(graybrush);

                }
                ptr->displaycount = 0;
            }
            break;
    }
    return DefWindowProc(hwnd, iMessage, wParam, lParam);
}

//-------------------------------------------------------------------------

void RegisterextTreeWindow(void)
{
    WNDCLASS wc;
    GetClassInfo(0, WC_TREEVIEW, &wc);
    oldproc = wc.lpfnWndProc;
    wc.lpfnWndProc = &extTreeWndProc;
    wc.lpszClassName = szextTreeWindClassName;
    wc.hInstance = hInstance;
    wc.cbWndExtra += (4-wc.cbWndExtra % 4);
    wndoffstree = wc.cbWndExtra;
    wc.cbWndExtra += 4;
    RegisterClass(&wc);

    GetClassInfo(0, "edit", &wc);
    oldeditproc = wc.lpfnWndProc;
    wc.lpfnWndProc = &extEditWndProc;
    wc.lpszClassName = szextEditWindClassName;
    wc.hInstance = hInstance;
    wc.cbWndExtra += (4-wc.cbWndExtra % 4);
    wndoffsedit = wc.cbWndExtra;
    wc.cbWndExtra += 4;
    RegisterClass(&wc);

    memset(&wc, 0, sizeof(wc));
    wc.style = 0;
    wc.lpfnWndProc = &ColumnTreeWndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = sizeof(void*);
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon(0, IDI_APPLICATION);
    wc.hCursor = LoadCursor(0, IDC_ARROW);
    wc.hbrBackground = CreateSolidBrush(0);
    wc.lpszMenuName = 0;
    wc.lpszClassName = szColumnTreeWindClassName;
    RegisterClass(&wc);

}

//-------------------------------------------------------------------------

HWND CreateextTreeWindow(HWND parent, int style, RECT *r, TCHeader *h)
{
    HWND hwnd = CreateWindowEx(0, szColumnTreeWindClassName, "",
        WS_CLIPSIBLINGS | WS_VISIBLE | WS_CHILD | style, r->left, r->top, r
        ->right - r->left, r->bottom - r->top, parent, (HMENU)0, (HINSTANCE)
        GetWindowLong(parent, GWL_HINSTANCE), 0);

    if (hwnd)
        SendMessage(hwnd, TCF_SETHEADER, 0, (LPARAM)h);
    return hwnd;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?