2 u010048134 u010048134 于 2013.12.23 02:58 提问

win32 滚动条的问题 不知道哪里的问题。 找了好久了。@求大婶@

请问 为什么往下翻页 没问题 往上翻译 文字就只显示一半@

#include "stdafx.h"
#include "demo.h"
#define MAX_LOADSTRING 100
#define MY_LINE 100
HINSTANCE hInst; //
TCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本
TCHAR szWindowClass[MAX_LOADSTRING];

ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
MSG msg;
HACCEL hAccelTable;
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_MY, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MY));
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

    return (int) msg.wParam;

}
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style                        = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc        = WndProc;
    wcex.cbClsExtra                = 0;
    wcex.cbWndExtra                = 0;
    wcex.hInstance                = hInstance;
    wcex.hIcon                        = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MY));
    wcex.hCursor                = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground        = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName        = MAKEINTRESOURCE(IDC_MY);
    wcex.lpszClassName        = szWindowClass;
    wcex.hIconSm                = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassEx(&wcex);

}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;

hInst = hInstance; // 将实例句柄存储在全局变量中

hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW |WS_VSCROLL,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

if (!hWnd)
{
return FALSE;
}

ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

return TRUE;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
static int cyChar;
TEXTMETRIC rm;
int x, y;
static int ivscroll,cyClint;
int mybeg; //= max(0,ivscroll + ps.rcPaint.top / cyChar);
int myend; //= min(MY_LINE-1,ivscroll +ps.rcPaint.bottom / cyChar);
SCROLLINFO si; //新的滚动条

    switch (message)
    {

    case WM_CREATE:
            hdc = GetDC(hWnd);
            GetTextMetrics(hdc,&rm);

            cyChar = rm.tmHeight + rm.tmExternalLeading;
            ReleaseDC(hWnd,hdc);
            return 0;
    case WM_SIZE:
            cyClint = HIWORD(lParam);
            si.cbSize = sizeof(si);
            si.fMask = SIF_RANGE | SIF_PAGE;
            si.nPage = cyClint /cyChar;
            si.nMin = 0;
            si.nMax = MY_LINE-1;
            SetScrollInfo(hWnd,SB_VERT,&si,TRUE);
            return 0;
    case WM_COMMAND:
            wmId    = LOWORD(wParam);
            wmEvent = HIWORD(wParam);
            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);
            }
            break;
    case WM_PAINT:
            hdc = BeginPaint(hWnd, &ps);
            // TODO: 在此添加任意绘图代码...
            TCHAR szBuffer[1024];
            si.cbSize = sizeof(si);
            si.fMask = SIF_POS;
            GetScrollInfo(hWnd,SB_VERT,&si);
            ivscroll = si.nPos;

            mybeg = max(0,ivscroll + ps.rcPaint.top / cyChar);
            myend = min(MY_LINE-1,ivscroll + ps.rcPaint.bottom / cyChar);
            for(int i = mybeg; i != myend;++i)
            {
                    x = 0;
                    y = cyChar*(i-ivscroll);
                    _sntprintf(szBuffer,1024,TEXT("hello %d"),i+1);
                    SetTextAlign(hdc,TA_LEFT | TA_TOP);
                    TextOut(hdc,x,y+5,szBuffer,lstrlen(szBuffer));
                    SetTextAlign(hdc,TA_RIGHT | TA_TOP);
            }
            EndPaint(hWnd, &ps);
            break;
    case WM_VSCROLL:
            si.cbSize =  sizeof(si);
            si.fMask = SIF_ALL;
            GetScrollInfo(hWnd,SB_VERT,&si);
            ivscroll = si.nPos;
            switch (LOWORD(wParam))
            {
            case SB_LINEDOWN:
                    si.nPos += 1;
                    break;
            case SB_PAGEDOWN:
                    si.nPos += si.nPage;
                    break;
            case SB_PAGEUP:
                    si.nPos -= si.nPage;
                    break;
            case SB_LINEUP:
                    si.nPos -= 1;
                    break;
            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 != ivscroll)
            {
                    ScrollWindow(hWnd,0,cyChar*(ivscroll - si.nPos),NULL,NULL);
                    UpdateWindow(hWnd);

            }



            break;
    case WM_DESTROY:
            PostQuitMessage(0);
            break;
    default:
            return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;

}
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;

    case WM_COMMAND:
            if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
            {
                    EndDialog(hDlg, LOWORD(wParam));
                    return (INT_PTR)TRUE;
            }
            break;
    }
    return (INT_PTR)FALSE;

}

Csdn user default icon
上传中...
上传图片
插入图片