Subclassed Win32时静态类表现怪异

时间:2012-12-08 22:50:54

标签: c++ c winapi scrollbar

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{

    int wmId, wmEvent;
    PAINTSTRUCT ps;
    HDC hdc;
    static HWND hDirectives;
    static char inBuffer [256] = "hey";
    static LPWSTR lpszBuffer = (LPWSTR)malloc(256*sizeof(LPWSTR));

    switch (message)
    {
    case WM_CREATE:
        hDirectives = CreateWindow(TEXT("static"), NULL, WS_CHILD | WS_BORDER | WS_VISIBLE | SS_RIGHT | SS_SUNKEN | WS_VSCROLL, 
            20, 20, 300, 300, hWnd, (HMENU) ID_DIRECTIVE, ((LPCREATESTRUCT) lParam) ->hInstance, NULL);
        SetWindowLong(hDirectives, GWL_WNDPROC, (LONG) DirectiveProc);
        break;


    case WM_COMMAND:
        wmId    = LOWORD(wParam);
        wmEvent = HIWORD(wParam);
        // Parse the menu selections:
        switch (wmId)
        {
        case IDM_ABOUT:
            DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
            break;
        case IDM_EXIT:
            DestroyWindow(hWnd);
            break;

        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
        }

    case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);
        // TODO: Add any drawing code here...
        EndPaint(hWnd, &ps);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

LRESULT CALLBACK DirectiveProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    PAINTSTRUCT ps;
    HDC hdc;
    HWND hParent = (HWND)GetWindowLong(hWnd, GWL_HWNDPARENT);
    static int nScrollPos;
    static SCROLLINFO si;
    switch(message)
    {
    case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);
        // TODO: Add any drawing code here...
        EndPaint(hWnd, &ps);

    case WM_SIZE:
        si.cbSize = sizeof(si);
        si.fMask  = SIF_RANGE | SIF_PAGE;
        si.nMin = 0;
        si.nMax = 10;
        si.nPage = 5;
        SetScrollInfo(hWnd, SB_VERT, &si, TRUE);
        return 0;

    case WM_VSCROLL:
        si.cbSize = sizeof(si);
        si.fMask  = SIF_ALL;
        GetScrollInfo(hWnd, SB_VERT, &si);

        nScrollPos = si.nPos;
        switch(LOWORD(wParam))
        {
        case SB_THUMBTRACK:
            si.nPos = si.nTrackPos;
            break;
        default:
            break;
        }
        si.fMask = SIF_POS;
        SetScrollInfo(hWnd, SB_VERT, &si, TRUE);
        GetScrollInfo(hWnd, SB_VERT, &si);

        if(si.nPos!=nScrollPos)
        {
            ScrollWindow(hWnd, 0, 5 * (nScrollPos - si.nPos), NULL, NULL);
            UpdateWindow(hParent);
        }
        return 0;
    default:
        DefWindowProc(hWnd, message, wParam, lParam);

    }
    //CallWindowProc(WndProc, hParent, message, wParam, lParam);  wasn't sure if I was supposed to add this
}

所以我不想将滚动功能添加到我的静态控件中,所以我将其子类化,但现在我遇到了问题。我不想做的是我不想在静态控件中显示文本然后滚动它。没有子类化,我可以在窗口中显示文本。现在使用子类化,控件显示为全白色与前一个灰色,并尝试设置文本失败。此外,当我滚动时,即使我在用户滚动时更新我的​​DirectiveProc中的父窗口,静态控件也会在其窗口之外绘制。我的目标是创建一个静态控件,其中包含用户可以滚动的信息。

1 个答案:

答案 0 :(得分:0)

评论者已经通过不遵循MSDN上列出的子类规则来识别您遇到的一些问题: