I wanted to implement a double click method to close the display window. That is, whenever the user double clicked on the client window, my window should close.


case WM_LBUTTONDBLCLK:
 ShowWindow(hWnd, SW_HIDE);
 break;

To implement this I decided to process the WM_LBUTTONDBLCLK message in my window procedure, as follows.

Short and simple, but I was wrong.  I ran this code and tried to double click the window, nothing happend. I changed a few things and tried again, still nothing happening. I finally put a break point inside the WM_LBUTTONDBLCLK case, and realized, that no matter how fast or slow I double clicked, the window never recieves the WM_LBUTTONDBLCLK message.

After digging about in msdn( yes the WM_LBUTTONDBLCLK page), I found out that, for our window to recieve the WM_LBUTTONDBLCLK message, we have to define our window class as having the style CS_DBLCLKS.

ie when we define WNDCLASS or WNDLCASSEX, we have to include CS_DBLCLKS as a style. For example, my window class defination now looks like

WNDCLASSEX wcex;
wcex.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS
.....

On normal windows class, whenever we double click on the client area, windows generates a series of WM_LBUTTONDOWN and WM_LBUTTONUP. i.e. we may recieve something like the following.

WM_LBUTTONDOWN, WM_LBUTTONUP, WM_LBUTTONDOWN, WM_LBUTTONUP .....

There is also no guarentee, that we would recieve all of them simultanously, i.e. there might be other messages present between them. So we cant rely on the sequence to determine a doubleclick. Whenever, we define our window class to have the style of CS_DBLCLKS, Windows replaces the second WM_LBUTTONDOWN message with the WM_LBUTTONDBLCLK, whenever the left button is clicked twice with the system defined intervel. But we will still recieve four mouse messages as follows.

WM_LBUTTONDOWN, WM_LBUTTONUP, WM_LBUTTONDBLCLK, WM_LBUTTONUP ...

The lesson to learn  is that, we will recieve a buttondown and a buttonup message before our dblclk message.  Thus whatever processing we are doing for the down and up events will be executed along with the dblclk logic.

Advertisements