- 1 year ago
How to disable the vertical header tracking line in Win32 ListView Control
In a Win32 ListView control, the vertical header tracking line, also known as the column tracking line, appears when the user resizes a column header by dragging it. If you want to disable this behavior and prevent the tracking line from being displayed, you can use the LVM_SETEXTENDEDLISTVIEWSTYLE
message to modify the extended styles of the ListView control.
Here's how you can achieve this in C++ using the Win32 API:
cpp#include <windows.h>
#include <commctrl.h>
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
case WM_CREATE: {
// Create a ListView control with LVS_EX_HEADERDRAGDROP style
HWND hListView = CreateWindow(WC_LISTVIEW, L"",
WS_CHILD | WS_VISIBLE | WS_BORDER | LVS_REPORT | LVS_EX_HEADERDRAGDROP,
10, 10, 300, 200, hwnd, NULL, NULL, NULL);
// Disable column header tracking line
ListView_SetExtendedListViewStyle(hListView, LVS_EX_HEADERDRAGDROP | LVS_EX_FULLROWSELECT);
return 0;
}
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
WNDCLASS wc = {0};
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = L"MyWindowClass";
RegisterClass(&wc);
HWND hwnd = CreateWindow(wc.lpszClassName, L"ListView Example",
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
400, 300, NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, nCmdShow);
MSG msg = {0};
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
In the above code, the key line is ListView_SetExtendedListViewStyle(hListView, LVS_EX_HEADERDRAGDROP | LVS_EX_FULLROWSELECT);
, where the LVS_EX_HEADERDRAGDROP
style is applied to the ListView control along with LVS_EX_FULLROWSELECT
for improved row selection behavior.
By using this code, the vertical header tracking line will be disabled for the ListView control, and users won't be able to see the line when resizing the column headers.