Jak utworzyć kontrolkę nagłówka
W tym temacie pokazano, jak utworzyć kontrolkę nagłówka i umieścić ją w obszarze klienta okna głównego. Kontrolkę nagłówka można utworzyć przy użyciu funkcji CreateWindowEx i określania klasy okna WC_HEADER oraz odpowiednich stylów kontrolek nagłówka . Ta klasa okna jest rejestrowana po załadowaniu wspólnej biblioteki DLL kontrolki. Aby upewnić się, że ta biblioteka DLL jest załadowana, użyj funkcji InitCommonControlsEx.
Co musisz wiedzieć
Technologie
Warunki wstępne
- C/C++
- Programowanie interfejsu użytkownika systemu Windows
Instrukcje
Poniższy przykład kodu C++ najpierw wywołuje funkcję InitCommonControlsEx, aby załadować wspólną bibliotekę DLL kontrolki. Następnie wywołuje funkcję CreateWindowEx w celu utworzenia kontrolki nagłówka. Kontrolka jest początkowo ukryta. Komunikat HDM_LAYOUT służy do obliczania rozmiaru i położenia kontrolki w oknie nadrzędnym. Kontrola jest następnie przestawiana i ustawiana jako widoczna.
// 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;
}
Tematy pokrewne