u010048134 2013-12-22 18:58
浏览 967

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;

}

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 delta降尺度计算的一些细节,有偿
    • ¥15 Arduino红外遥控代码有问题
    • ¥15 数值计算离散正交多项式
    • ¥30 数值计算均差系数编程
    • ¥15 redis-full-check比较 两个集群的数据出错
    • ¥15 Matlab编程问题
    • ¥15 训练的多模态特征融合模型准确度很低怎么办
    • ¥15 kylin启动报错log4j类冲突
    • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
    • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序