Home

WndProc

WndProc is the window procedure in the Win32 API. It is a callback function that processes messages for a window. The system calls the window procedure with a message identifier and parameters describing the message. The function is defined with the signature LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) and is supplied when registering a window class via WNDCLASS or WNDCLASSEX (lpfnWndProc).

Messages represent events or requests such as creation, painting, destruction, resizing, keyboard input, mouse input, or

Window procedures can be customized for different window classes; developers may subclass an existing window by

system
commands
(e.g.,
WM_PAINT,
WM_SIZE,
WM_DESTROY,
WM_KEYDOWN,
WM_LBUTTONDOWN).
The
WndProc
should
handle
the
messages
that
it
cares
about
and
pass
unhandled
messages
to
DefWindowProc
to
provide
default
processing.
Applications
typically
run
a
message
loop
(GetMessage/TranslateMessage/DispatchMessage)
in
the
UI
thread;
DispatchMessage
calls
the
window
procedure
for
each
message
addressed
to
that
window.
The
return
value
is
message-specific;
if
the
procedure
processes
the
message,
it
should
return
0;
otherwise
DefWindowProc’s
return
value
is
used.
replacing
its
WndProc
with
SetWindowLongPtr
and
restoring
it
later.
WndProc
runs
in
the
thread
that
owns
the
window's
message
queue,
and
interacts
with
other
UI
components
through
messages
and
window
handles.
Although
the
Windows
API
widely
uses
WndProc,
newer
frameworks
may
encapsulate
it
in
higher‑level
abstractions,
while
WndProc
remains
a
core
building
block
of
traditional
Win32
programming.