Bagikan melalui


Cara Membuat Bilah Gulir

Saat membuat jendela tumpang tindih, pop-up, atau anak, Anda dapat menambahkan bilah gulir standar dengan menggunakan fungsi CreateWindowEx dan menentukan WS_HSCROLL, WS_VSCROLL, atau kedua gaya.

Apa yang perlu Anda ketahui

Teknologi

Prasyarat

  • C/C++
  • Pemrograman Antarmuka Pengguna Windows

Petunjuk

Membuat Bilah Gulir

Contoh berikut membuat jendela dengan bilah gulir horizontal dan vertikal standar.

    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 
    ); 

Untuk memproses pesan bilah gulir untuk bilah gulir ini, Anda harus menyertakan kode yang sesuai dalam prosedur jendela utama.

Anda dapat menggunakan fungsi CreateWindowEx untuk membuat bilah gulir dengan menentukan kelas jendela SCROLLBAR. Ini membuat bilah gulir horizontal atau vertikal, tergantung pada apakah SBS_HORZ atau SBS_VERT ditentukan sebagai gaya jendela. Ukuran bilah gulir dan posisinya relatif terhadap jendela induknya juga dapat ditentukan.

Contoh berikut membuat bilah gulir horizontal yang diposisikan di sepanjang bagian bawah area klien jendela induk.

// 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 
        )); 
}

Menggunakan Bilah Gulir

Demo kontrol umum Windows (CppWindowsCommonControls)