다음을 통해 공유


스크롤 막대를 만드는 방법

겹치는 팝업 또는 자식 창을 만들 때 CreateWindowEx 함수를 사용하고 WS_HSCROLL, WS_VSCROLL 또는 두 스타일을 지정하여 표준 스크롤 막대를 추가할 수 있습니다.

알아야 할 사항

기술

필수 구성 요소

  • C/C++
  • Windows 사용자 인터페이스 프로그래밍

지시

스크롤 막대 만들기

다음 예제에서는 표준 가로 및 세로 스크롤 막대가 있는 창을 만듭니다.

    hwnd = CreateWindowEx( 
        0,                     // no extended styles 
        g_szWindowClass,       // global string containing name of window class
        g_szTitle,             // global string containing title bar text 
        WS_OVERLAPPEDWINDOW |  
            WS_HSCROLL | WS_VSCROLL, // window styles 
        CW_USEDEFAULT,         // default horizontal position 
        CW_USEDEFAULT,         // default vertical position 
        CW_USEDEFAULT,         // default width 
        CW_USEDEFAULT,         // default height 
        (HWND) NULL,           // no parent for overlapped windows 
        (HMENU) NULL,          // use the window class menu 
        g_hInst,               // global instance handle  
        (PVOID) NULL           // pointer not needed 
    ); 

이러한 스크롤 막대에 대한 스크롤 막대 메시지를 처리하려면 주 창 프로시저에 적절한 코드를 포함해야 합니다.

CreateWindowEx 함수를 사용하여 SCROLLBAR 창 클래스를 지정하여 스크롤 막대를 만들 수 있습니다. SBS_HORZ 또는 SBS_VERT 창 스타일로 지정되었는지 여부에 따라 가로 또는 세로 스크롤 막대가 만들어집니다. 스크롤 막대 크기와 부모 창을 기준으로 하는 위치를 지정할 수도 있습니다.

다음 예제에서는 부모 창의 클라이언트 영역 아래쪽에 있는 가로 스크롤 막대를 만듭니다.

// Description:
//   Creates a horizontal scroll bar along the bottom of the parent 
//   window's area.
// Parameters:
//   hwndParent - handle to the parent window.
//   sbHeight - height, in pixels, of the scroll bar.
// Returns:
//   The handle to the scroll bar.
HWND CreateAHorizontalScrollBar(HWND hwndParent, int sbHeight)
{
    RECT rect;

    // Get the dimensions of the parent window's client area;
    if (!GetClientRect(hwndParent, &rect))
        return NULL;

    // Create the scroll bar.
    return (CreateWindowEx( 
            0,                      // no extended styles 
            L"SCROLLBAR",           // scroll bar control class 
            (PTSTR) NULL,           // no window text 
            WS_CHILD | WS_VISIBLE   // window styles  
                | SBS_HORZ,         // horizontal scroll bar style 
            rect.left,              // horizontal position 
            rect.bottom - sbHeight, // vertical position 
            rect.right,             // width of the scroll bar 
            sbHeight,               // height of the scroll bar
            hwndParent,             // handle to main window 
            (HMENU) NULL,           // no menu 
            g_hInst,                // instance owning this window 
            (PVOID) NULL            // pointer not needed 
        )); 
}

스크롤 막대 사용하기

Windows 공용 컨트롤 데모(CppWindowsCommonControls)