I see, but subclassing is the easiest way to make custom control.
Your question is about "custom controls", which is distinct from "common controls"
The difference is that common controls provide default functionality provided by system, which you can subclass and extend for your needs.
On the other side custom control mean absolute zero default functionality, you get a blank window which needs to be turned into a control, therefore much more work to do.
Subclassing is far easier, and scrollbar is indeed a child window.
For example to create ScrollBar:
1 2 3 4 5 6 7 8 9 10 11 12
|
HWND hWnd = CreateWindowExW(
WS_EX_LEFT,
WC_SCROLLBAR,
nullptr,
WS_CHILD,
x, y,
width,
height,
hParent, // parent window ex. your main window
reinterpret_cast<HMENU>(ctrl_ID)), // scrollbar ID number
GetModuleHandleW(nullptr),
nullptr);
| |
This is subclass procedure to handle custom control messages:
1 2 3 4 5 6 7 8 9 10
|
LRESULT SubClassProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, UINT_PTR SubclassID, DWORD_PTR ref_data)
{
// UnSublcass when done
if (msg == WM_NCDESTROY)
RemoveWindowSubclass(hWnd, SubclassProc, 1);
// handle messages or just do the default
// note that you call DefSubclassProc not DefWindowProc
return DefSubclassProc(hWnd, msg, wParam, lParam);
}
| |
And then to subclass it: (note that you don't need to register window)
SetWindowSubclass(hWnd , SubClassProc, 1, nullptr);
And that's it, run message loop and handle messages in
SubClassProc
You can't have it more simple than that.
EDIT:
Ah ofc. before all this you need to load the common control library for scrollbars, here is
how to do it:
1 2 3 4 5 6
|
INITCOMMONCONTROLSEX ctrl;
ctrl.dwSize = sizeof(INITCOMMONCONTROLSEX);
ctrl.dwICC = ICC_BAR_CLASSES;
InitCommonControlsEx(&ctrl);
| |