Come creare un'intestazione
In questo argomento viene illustrato come creare un controllo intestazione e posizionarlo all'interno dell'area client della finestra padre. È possibile creare un controllo intestazione utilizzando la funzione CreateWindowEx e specificando la classe di finestra WC_HEADER e gli appropriati stili di controllo dell'intestazione . Questa classe finestra viene registrata quando viene caricata la DLL del controllo comune. Per assicurarsi che questa DLL venga caricata, usare la funzioneInitCommonControlsEx.
Cosa è necessario sapere
Tecnologie
Prerequisiti
- C/C++
- Programmazione dell'interfaccia utente di Windows
Disposizioni
Nell'esempio di codice C++ seguente viene innanzitutto chiamato la funzioneInitCommonControlsEx per caricare la DLL del controllo comune. Chiama quindi la funzione CreateWindowEx per creare un controllo di intestazione. Il controllo è inizialmente nascosto. Il messaggio HDM_LAYOUT viene utilizzato per calcolare le dimensioni e la posizione del controllo all'interno della finestra padre. Il controllo viene quindi riposizionato e reso visibile.
// 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;
}
Argomenti correlati