zoukankan      html  css  js  c++  java
  • SetWindoworgEx与滚动条的综合使用

    http://blog.csdn.net/chla/archive/2010/12/06/6058758.aspx

    转贴地址: http://unix-cd.com/vc/www/23/2010-01/14771.html

    这个例子展示了用SetWindoworgEx与滚动条结合起来使用, 我发了很长时间才明白窗口,视口这些玩意。
    代码看起来非常简单, 所以我就没有写过多的注释了:

    这个例子展示了用SetWindoworgEx与滚动条结合起来使用, 我发了很长时间才明白窗口,视口这些玩意。
    代码看起来非常简单, 所以我就没有写过多的注释了:

    源代码:

    #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("Draw 100 circles");
        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 = GetStockObject(WHITE_BRUSH);
        wndclass.lpszMenuName = NULL;
        wndclass.lpszClassName = szAppName;
        
        if(!RegisterClass(&wndclass)) {
            MessageBox(NULL, TEXT("Register failure..."),
                szAppName, MB_ICONERROR);
            return 0;
        }
        
        hwnd = CreateWindow(szAppName,
            szAppName,
            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 cxClient, cyClient;
        HDC hdc;
        PAINTSTRUCT ps;
        SCROLLINFO si;
        int i, j, iVertPos, iHorzPos;
        HBRUSH hBrush;
        
        switch(message) {
            
        case WM_SIZE:
            cxClient = LOWORD(lParam);
            cyClient = HIWORD(lParam);
            si.cbSize = sizeof(si);
            si.fMask = SIF_PAGE | SIF_RANGE;
            si.nPage = cyClient;
            si.nMin = 0;
            si.nMax = 10 * cyClient;
            SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
            si.nPage = cxClient;
            si.nMin = 0;
            si.nMax = 10 * cxClient;
            SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
            return 0;
            
        case WM_VSCROLL:
            si.cbSize = sizeof(si);
            si.fMask = SIF_ALL;
            GetScrollInfo(hwnd, SB_VERT, &si);
            iVertPos = si.nPos;
            
            switch(LOWORD(wParam)) {
            case SB_LINEUP:
                si.nPos -= 10;
                break;
            case SB_LINEDOWN:
                si.nPos += 10;
                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;
            }
            
            si.fMask = SIF_POS;
            SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
            GetScrollInfo(hwnd, SB_VERT, &si);
            if(si.nPos != iVertPos) {
                ScrollWindow(hwnd, 0, iVertPos - si.nPos, NULL, NULL);
                UpdateWindow(hwnd);
            }
            return 0;
            
            case WM_HSCROLL:
                si.cbSize = sizeof(si);
                si.fMask = SIF_ALL;
                GetScrollInfo(hwnd, SB_HORZ, &si);
                iHorzPos = si.nPos;
                
                switch(LOWORD(wParam)) {
                case SB_LINELEFT:
                    si.nPos -= 20;
                    break;
                case SB_LINERIGHT:
                    si.nPos += 20;
                    break;
                case SB_PAGELEFT:
                    si.nPos -= cxClient;
                    break;
                case SB_PAGERIGHT:
                    si.nPos += cxClient;
                    break;
                case SB_THUMBTRACK:
                    si.nPos = si.nTrackPos;
                default:
                    break;
                }
                
                si.fMask = SIF_POS;
                SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
                GetScrollInfo(hwnd, SB_HORZ, &si);
                if(si.nPos != iHorzPos) {
                    ScrollWindow(hwnd, iHorzPos - si.nPos, 0, NULL, NULL);
                    UpdateWindow(hwnd);
                }
                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;
                    GetScrollInfo(hwnd, SB_HORZ, &si);
                    iHorzPos = si.nPos;
                    SetWindowOrgEx(hdc, iHorzPos, iVertPos, NULL);
                    hBrush = CreateSolidBrush(RGB(0, 100, 100));
                    SelectObject(hdc, hBrush);
                    
                    for(i = 0; i < 10; i++)
                        for(j = 0; j < 10; j++)
                        {
                            Ellipse(hdc, j * cxClient + cxClient / 4, i * cyClient + (cyClient / 2 - cxClient / 4),
                                j * cxClient + 3 * cxClient / 4, i * cyClient + (cyClient / 2 + cxClient / 4));
                        }
                        EndPaint(hwnd, &ps);
                        DeleteObject(hBrush);
                        return 0;
                        
                case WM_DESTROY:
                    PostQuitMessage(0);
                    return 0;
        }
        return DefWindowProc(hwnd, message, wParam, lParam);
    }
    
    

    代码
    1 #include <windows.h>
    2 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    3  int WINAPI WinMain(HINSTANCE hInstance,
    4 HINSTANCE hPrevInstance,
    5 PSTR szCmdLine,
    6 int iCmdShow) {
    7 static TCHAR szAppName[] = TEXT("Draw 100 circles");
    8 HWND hwnd;
    9 MSG msg;
    10 WNDCLASS wndclass;
    11
    12 wndclass.style = CS_HREDRAW | CS_VREDRAW;
    13 wndclass.lpfnWndProc = WndProc;
    14 wndclass.cbClsExtra = 0;
    15 wndclass.cbWndExtra = 0;
    16 wndclass.hInstance = hInstance;
    17 wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    18 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
    19 wndclass.hbrBackground = GetStockObject(WHITE_BRUSH);
    20 wndclass.lpszMenuName = NULL;
    21 wndclass.lpszClassName = szAppName;
    22
    23 if(!RegisterClass(&wndclass)) {
    24 MessageBox(NULL, TEXT("Register failure..."),
    25 szAppName, MB_ICONERROR);
    26 return 0;
    27 }
    28
    29 hwnd = CreateWindow(szAppName,
    30 szAppName,
    31 WS_OVERLAPPEDWINDOW | WS_VSCROLL | WS_HSCROLL,
    32 CW_USEDEFAULT,
    33 CW_USEDEFAULT,
    34 CW_USEDEFAULT,
    35 CW_USEDEFAULT,
    36 NULL,
    37 NULL,
    38 hInstance,
    39 NULL);
    40
    41 ShowWindow(hwnd, iCmdShow);
    42 UpdateWindow(hwnd);
    43
    44 while(GetMessage(&msg, NULL, 0, 0)) {
    45 TranslateMessage(&msg);
    46 DispatchMessage(&msg);
    47 }
    48 return msg.wParam;
    49 }
    50 LRESULT CALLBACK WndProc(HWND hwnd,
    51 UINT message,
    52 WPARAM wParam,
    53 LPARAM lParam) {
    54 static int cxClient, cyClient;
    55 HDC hdc;
    56 PAINTSTRUCT ps;
    57 SCROLLINFO si;
    58 int i, j, iVertPos, iHorzPos;
    59 HBRUSH hBrush;
    60
    61 switch(message) {
    62
    63 case WM_SIZE:
    64 cxClient = LOWORD(lParam);
    65 cyClient = HIWORD(lParam);
    66 si.cbSize = sizeof(si);
    67 si.fMask = SIF_PAGE | SIF_RANGE;
    68 si.nPage = cyClient;
    69 si.nMin = 0;
    70 si.nMax = 10 * cyClient;
    71 SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
    72 si.nPage = cxClient;
    73 si.nMin = 0;
    74 si.nMax = 10 * cxClient;
    75 SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
    76 return 0;
    77
    78 case WM_VSCROLL:
    79 si.cbSize = sizeof(si);
    80 si.fMask = SIF_ALL;
    81 GetScrollInfo(hwnd, SB_VERT, &si);
    82 iVertPos = si.nPos;
    83
    84 switch(LOWORD(wParam)) {
    85 case SB_LINEUP:
    86 si.nPos -= 10;
    87 break;
    88 case SB_LINEDOWN:
    89 si.nPos += 10;
    90 break;
    91 case SB_PAGEUP:
    92 si.nPos -= si.nPage;
    93 break;
    94 case SB_PAGEDOWN:
    95 si.nPos += si.nPage;
    96 break;
    97 case SB_THUMBTRACK:
    98 si.nPos = si.nTrackPos;
    99 break;
    100 default:
    101 break;
    102 }
    103
    104 si.fMask = SIF_POS;
    105 SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
    106 GetScrollInfo(hwnd, SB_VERT, &si);
    107 if(si.nPos != iVertPos) {
    108 ScrollWindow(hwnd, 0, iVertPos - si.nPos, NULL, NULL);
    109 UpdateWindow(hwnd);
    110 }
    111 return 0;
    112
    113 case WM_HSCROLL:
    114 si.cbSize = sizeof(si);
    115 si.fMask = SIF_ALL;
    116 GetScrollInfo(hwnd, SB_HORZ, &si);
    117 iHorzPos = si.nPos;
    118
    119 switch(LOWORD(wParam)) {
    120 case SB_LINELEFT:
    121 si.nPos -= 20;
    122 break;
    123 case SB_LINERIGHT:
    124 si.nPos += 20;
    125 break;
    126 case SB_PAGELEFT:
    127 si.nPos -= cxClient;
    128 break;
    129 case SB_PAGERIGHT:
    130 si.nPos += cxClient;
    131 break;
    132 case SB_THUMBTRACK:
    133 si.nPos = si.nTrackPos;
    134 default:
    135 break;
    136 }
    137
    138 si.fMask = SIF_POS;
    139 SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
    140 GetScrollInfo(hwnd, SB_HORZ, &si);
    141 if(si.nPos != iHorzPos) {
    142 ScrollWindow(hwnd, iHorzPos - si.nPos, 0, NULL, NULL);
    143 UpdateWindow(hwnd);
    144 }
    145 return 0;
    146
    147 case WM_PAINT:
    148 hdc = BeginPaint(hwnd, &ps);
    149 si.cbSize = sizeof(si);
    150 si.fMask = SIF_POS;
    151 GetScrollInfo(hwnd, SB_VERT, &si);
    152 iVertPos = si.nPos;
    153 GetScrollInfo(hwnd, SB_HORZ, &si);
    154 iHorzPos = si.nPos;
    155 SetWindowOrgEx(hdc, iHorzPos, iVertPos, NULL);
    156 hBrush = CreateSolidBrush(RGB(0, 100, 100));
    157 SelectObject(hdc, hBrush);
    158
    159 for(i = 0; i < 10; i++)
    160 for(j = 0; j < 10; j++)
    161 {
    162 Ellipse(hdc, j * cxClient + cxClient / 4, i * cyClient + (cyClient / 2 - cxClient / 4),
    163 j * cxClient + 3 * cxClient / 4, i * cyClient + (cyClient / 2 + cxClient / 4));
    164 }
    165 EndPaint(hwnd, &ps);
    166 DeleteObject(hBrush);
    167 return 0;
    168
    169 case WM_DESTROY:
    170 PostQuitMessage(0);
    171 return 0;
    172 }
    173 return DefWindowProc(hwnd, message, wParam, lParam);
    174 }
    175  

    源码本地下载地址: https://files.cnblogs.com/chulia20002001/Ttreelist.rar


    发表于 @2010年12月06日 16:57:00

  • 相关阅读:
    ural(Timus) 1019 Line Painting
    ACMICPC Live Archive 2031 Dance Dance Revolution
    poj 3321 Apple Tree
    其他OJ 树型DP 选课
    poj 3548 Restoring the digits
    ACMICPC Live Archive 3031 Cable TV Network
    递归循环获取指定节点下面的所有子节点
    手动触发asp.net页面验证控件事件
    子级Repeater获取父级Repeater绑定项的值
    没有列名的数据绑定
  • 原文地址:https://www.cnblogs.com/chulia20002001/p/1899663.html
Copyright © 2011-2022 走看看