关闭

继续写……带滚动条的窗口

524人阅读 评论(0) 收藏 举报

  写完第一个窗口后发现没有滚动条,开始以为不久加个滚动条吗小菜,后来发现我错了,那个叫麻烦啊,而且极易出错,弄了n久终于弄出来了,哎麻烦啊抓狂

老规矩上代码:

#include "stdafx.h"
#include<windows.h>
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevinstance,PSTR szCmdLine,int iCmdShow)
{
    static TCHAR szAppName[]=TEXT("hello windows 7");
    HWND hwnd;
    MSG msg;
    WNDCLASS wndclass;
    wndclass.style=CS_HREDRAW|CS_VREDRAW;
    wndclass.lpfnWndProc=WndProc;
    wndclass.cbClsExtra=0;
    wndclass.cbWndExtra=0;
    wndclass.hInstance=hInstance;
    wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
    wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
    wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
    wndclass.lpszMenuName=NULL;
    wndclass.lpszClassName=szAppName;
    RegisterClassW(&wndclass);
    hwnd=CreateWindow(szAppName,TEXT("Display"),WS_OVERLAPPEDWINDOW | WS_VSCROLL | WS_HSCROLL,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);
    ShowWindow(hwnd,iCmdShow);
    UpdateWindow(hwnd);
    while(GetMessage(&msg,NULL,0,0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT message, WPARAM wParam,LPARAM lParam)
{
    static int cxChar, cxCaps, cyChar, cxClient, cyClient, iMaxWidth ;
    int  x, y, iVertPos, iHorzPos ;
    HDC hdc;
    PAINTSTRUCT ps;
    SCROLLINFO si ;
    TEXTMETRIC tm ;
    int iLength;
        int a[10][10];
    int i,j;
    for(i=0;i<10;i++)
    {
        a[i][0]=1;
        a[i][i]=1;
    }
    for(i=2;i<10;i++)
        for(j=1;j<i;j++)
            a[i][j]=a[i-1][j]+a[i-1][j-1];
    switch(message)
    {
    case WM_CREATE:
        hdc = GetDC (hwnd) ;
        GetTextMetrics (hdc, &tm) ;
        cxChar = tm.tmAveCharWidth ;
        cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2) * cxChar / 2 ;      
        cyChar = tm.tmHeight + tm.tmExternalLeading ;
        ReleaseDC (hwnd, hdc) ;
        iMaxWidth=cxChar*60;                return 0;
    case WM_SIZE:
        cxClient = LOWORD (lParam) ;
        cyClient = HIWORD (lParam) ;
        // Set vertical scroll bar range and page size
        si.cbSize = sizeof (si) ;
        si.fMask = SIF_RANGE | SIF_PAGE ;
        si.nMin = 0 ;
        si.nMax =10;
        si.nPage = cyClient / cyChar ;
        SetScrollInfo (hwnd, SB_VERT, &si, TRUE) ;
        // Set horizontal scroll bar range and page size
        si.cbSize = sizeof (si) ;
        si.fMask = SIF_RANGE | SIF_PAGE |SIF_DISABLENOSCROLL;
        si.nMin = 0 ;
        si.nMax =  2 + iMaxWidth / cxChar;
        si.nPage = cxClient / (cxChar);
        SetScrollInfo (hwnd, SB_HORZ, &si, TRUE) ;
        return 0 ;
    case WM_VSCROLL:
        // Get all the vertical scroll bar information
        si.cbSize = sizeof (si) ;
        si.fMask = SIF_ALL ;
        GetScrollInfo (hwnd, SB_VERT, &si) ;
        // Save the position for comparison later on
        iVertPos = si.nPos ;
        switch (LOWORD (wParam))
        {
        case SB_TOP:
        si.nPos = si.nMin ;
        break ;
        case SB_BOTTOM:
        si.nPos = si.nMax ;
        break ;
        case SB_LINEUP:
        si.nPos -= 1 ;
        break ;
        case SB_LINEDOWN:
        si.nPos += 1 ;
        break ;
        case SB_PAGEUP:
        si.nPos -= si.nPage ;
        break ;
        case SB_PAGEDOWN:
        si.nPos += si.nPage ;
        break ;
        case SB_THUMBTRACK:
        si.nPos = si.nTrackPos ;
        break ;
        default:
        break ;
        }
        // Set the position and then retrieve it. Due to adjustments
        // by Windows it may not be the same as the value set.
        si.fMask = SIF_POS ;
        SetScrollInfo (hwnd, SB_VERT, &si, TRUE) ;
        GetScrollInfo (hwnd, SB_VERT, &si) ;
        // If the position has changed, scroll the window and update it
        if (si.nPos != iVertPos)
        {
        ScrollWindow (hwnd, 0, cyChar * (iVertPos - si.nPos),
        NULL, NULL) ;
        UpdateWindow (hwnd) ;
        }
        return 0 ;
    case WM_HSCROLL:
        // Get all the vertical scroll bar information
        si.cbSize = sizeof (si) ;
        si.fMask = SIF_ALL ;
        // Save the position for comparison later on
        GetScrollInfo (hwnd, SB_HORZ, &si) ;
        iHorzPos = si.nPos ;
        switch (LOWORD (wParam))
        {
        case SB_LINELEFT:
        si.nPos -= 1 ;
        break ;
        case SB_LINERIGHT:
        si.nPos += 1 ;
        break ;
        case SB_PAGELEFT:
        si.nPos -= si.nPage ;
        break ;
        case SB_PAGERIGHT:
        si.nPos += si.nPage ;
        break ;
        case SB_THUMBTRACK:
        si.nPos = si.nTrackPos ;
        break ;
        default :
        break ;
        }
        // Set the position and then retrieve it. Due to adjustments
        // by Windows it may not be the same as the value set.
        si.fMask = SIF_POS ;
        SetScrollInfo (hwnd, SB_HORZ, &si, TRUE) ;
        GetScrollInfo (hwnd, SB_HORZ, &si) ;
        // If the position has changed, scroll the window
        if (si.nPos != iHorzPos)
        {
        ScrollWindow (hwnd, cxChar* (iHorzPos - si.nPos), 0,
        NULL, NULL) ;
        }
        return 0 ;

        case WM_KEYDOWN:
switch (wParam)
{
case VK_UP:
SendMessage (hwnd, WM_VSCROLL, SB_LINEUP, 0) ;
break ;
case VK_DOWN:
SendMessage (hwnd, WM_VSCROLL, SB_LINEDOWN, 0) ;
break ;
case VK_LEFT:
SendMessage (hwnd, WM_HSCROLL, SB_PAGEUP, 0) ;
break ;
case VK_RIGHT:
SendMessage (hwnd, WM_HSCROLL, SB_PAGEDOWN, 0) ;
break ;
}
return 0 ;

    case WM_PAINT:
        hdc=BeginPaint(hwnd,&ps);
        si.cbSize = sizeof (si) ;
        si.fMask = SIF_POS ;
        GetScrollInfo (hwnd, SB_VERT, &si) ;
        iVertPos = si.nPos ;
        // Get horizontal scroll bar position
        GetScrollInfo (hwnd, SB_HORZ, &si) ;
        iHorzPos = si.nPos ;
        int j;TCHAR szBuffer[10] ;
        for(i=0;i<10;i++)
            {
                y = cyChar * (i - iVertPos) ;
            for(j=0;j<=i;j++)
                {
                    x = cxChar * (j*6 - iHorzPos) ;
                iLength = wsprintf (szBuffer, TEXT ("%i"),a[i][j]);
                TextOut(hdc,x,y,szBuffer,iLength);
                
                }
            }
            
        
        EndPaint(hwnd,&ps);
        return 0;
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }
    return DefWindowProc(hwnd,message,wParam,lParam);
}





0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:194345次
    • 积分:2503
    • 等级:
    • 排名:第15268名
    • 原创:75篇
    • 转载:10篇
    • 译文:1篇
    • 评论:76条
    最新评论