Bagikan melalui


Cara Membuat Kontrol Header

Topik ini menunjukkan cara membuat kontrol header dan memosisikannya dalam area klien jendela induk. Anda dapat membuat kontrol header dengan menggunakan fungsi CreateWindowEx dan menentukan kelas jendela WC_HEADER dan gaya kontrol header yang sesuai. Kelas jendela ini didaftarkan ketika DLL kontrol umum dimuat. Untuk memastikan bahwa DLL ini dimuat, gunakan fungsiInitCommonControlsEx.

Apa yang perlu Anda ketahui

Teknologi

Prasyarat

  • C/C++
  • Pemrograman Antarmuka Pengguna Windows

Peraturan

Contoh kode C++ berikut terlebih dahulu memanggil fungsi InitCommonControlsEx untuk memuat DLL kontrol umum. Kemudian memanggil fungsi CreateWindowEx untuk membuat kontrol header. Kontrol awalnya disembunyikan. Pesan HDM_LAYOUT digunakan untuk menghitung ukuran dan posisi kontrol dalam jendela induk. Kontrol kemudian diposisikan ulang dan dibuat terlihat.

// DoCreateHeader - creates a header control that is positioned along 
//     the top of the parent window's client area. 
// Returns the handle to the header control. 
// hwndParent - handle to the parent window. 
// 
// Global variable 
//    g_hinst - handle to the application instance 
extern HINSTANCE g_hinst; 
//
// child-window identifier
int ID_HEADER;
//
HWND DoCreateHeader(HWND hwndParent) 
{ 
        HWND hwndHeader; 
        RECT rcParent; 
        HDLAYOUT hdl; 
        WINDOWPOS wp; 
 
        // Ensure that the common control DLL is loaded, and then create 
        // the header control. 
        INITCOMMONCONTROLSEX icex;  //declare an INITCOMMONCONTROLSEX Structure
        icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
        icex.dwICC = ICC_LISTVIEW_CLASSES;   //set dwICC member to ICC_LISTVIEW_CLASSES    
                                             // this loads list-view and header control classes.
    InitCommonControlsEx(&icex); 
 
        if ((hwndHeader = CreateWindowEx(0, WC_HEADER, (LPCTSTR) NULL, 
                WS_CHILD | WS_BORDER | HDS_BUTTONS | HDS_HORZ, 
                0, 0, 0, 0, hwndParent, (HMENU) ID_HEADER, g_hinst, 
                (LPVOID) NULL)) == NULL) 
            return (HWND) NULL; 
 
        // Retrieve the bounding rectangle of the parent window's 
        // client area, and then request size and position values 
        // from the header control. 
        GetClientRect(hwndParent, &rcParent); 
 
        hdl.prc = &rcParent; 
        hdl.pwpos = ℘ 
        if (!SendMessage(hwndHeader, HDM_LAYOUT, 0, (LPARAM) &hdl)) 
            return (HWND) NULL; 
 
        // Set the size, position, and visibility of the header control. 
        SetWindowPos(hwndHeader, wp.hwndInsertAfter, wp.x, wp.y, 
            wp.cx, wp.cy, wp.flags | SWP_SHOWWINDOW); 
 
        return hwndHeader; 
}

Tentang Kontrol Header

Referensi Kontrol Header

Menggunakan Kontrol Header