The realization of imitation QQ hanging window

Source: Internet
Author: User
Tags bool message queue thread

QQ friends know that when the QQ window is located on the left side of the desktop, right border or the top of the time, QQ will automatically hide, and once the mouse contact with the above boundaries, the QQ window will automatically expand. QQ This special effect to a certain extent, greatly saving the desktop resources, to bring convenience to users.

QQ Hanging window is the main feature of the combination of Windows and mouse position, and through mouse events to adjust the window display mode. Among them, the window and mouse position can be obtained by GetWindowRect and GetCursorPos, so how to get the mouse event becomes the key to realize the QQ hanging window.

For a window, the mouse event can be grouped into three categories by the trigger position of the mouse event:

1. Client area Mouse message: Mouse in the window of the client area when the message generated, this message is the standard mouse message, MFC through the Wm_mousemove this event to solve the problem.

2. Non-client area mouse message: The mouse is outside the client area (title bar, frame, etc.) when the message generated, this message is a standard mouse message, MFC through the Wm_ncmousemove this event to solve the problem.

3. Outside the window of the mouse message: The mouse is not moving in this window when the message generated, this message is not a standard mouse message, in MFC can not find such an event. So how do you capture such a mouse message?

The mouse message outside the window must be on another window, which is sent to a message queue in another window, maintained by the message queues of other windows.

However, we can monitor the mouse position by setting the global mouse hook and trigger the mouse message. If you set the mouse hook in the window settings, then this mouse hook can only monitor the above mouse events in the first two categories of events, and not be able to monitor the mouse message outside the window, not the real global mouse hook. If you set the mouse hook in a DLL, then the mouse event on the entire screen will be monitored by the mouse process, that is, you can capture the mouse message of other windows and send this mouse message to the message queue of the owning thread in this window. In this window, you must upload the thread ID of this window to the DLL so that the DLL can send other mouse events to the message queue of the specified thread. Specifically implemented as follows:

//------------------------------------------------------------------------------------
Function:sethook-creates Mouse Hook (exported), called by Cappbarmngr
Arguments: _id-calling thread ID, used to send message to it
_width-width of Window
_left-true If window is left side docked, false if not
Returns:false if it is already hooked
True If Hook has been created
//------------------------------------------------------------------------------------
BOOL Sethook (DWORD _id, int _width, BOOL _left)
{
if (S_threadid)
return FALSE; Already hooked!
S_width = _width;
S_left = _left;
G_hook =:: SetWindowsHookEx (Wh_mouse, (HookProc) Mouseproc, g_instance, 0);
S_threadid = _id;
return TRUE; Hook has been created correctly
}
//-------------------------------------------------------------------------------------
Function:mouseproc-callback function for mouse hook
Arguments:ncode-action code, according to MS documentation, must return
inmediatly if less than 0
Wparam-not used
Lparam-not used
Returns:result from next hook in chain
//-------------------------------------------------------------------------------------
Static lresult CALLBACK mouseproc (int ncode, WPARAM WPARAM, LPARAM LPARAM)
{
static Lresult Lresult; Made Static to accelerate processing
Static Point pt; Idem
if (ncode<0 && g_hook)
{
:: CallNextHookEx (G_hook, Ncode, WParam, LParam); Call Next hook in chain
return 0;
}
if (S_threadid)
{
Obtain Absolute screen coordinates
:: GetCursorPos (&AMP;PT);
Static point Ptold;
Only when the mouse occurs when the mouse event occurs, did not think the mouse does not move will produce this mouse process,
It really surprises me that I have to prevent the mouse from getting out of hair.
if (ptold.x!=pt.x && ptold.y!=pt.y)
{
::P ostthreadmessage (S_threadid, wm_user+1000, 0, 0);
Ptold.x = Pt.x;
Ptold.y = Pt.y;
}
}
Return:: CallNextHookEx (G_hook, Ncode, WParam, LParam); Call Next hook in chain
}
//-------------------------------------------------------------------------------------
Function:unsethook-removes Hook from chain
Arguments:none
Returns:false if not hook pending to delete (no thread ID defined)
True if hook has been removed. Also returns True if there is not hook
Handler, this can occur if Init failed when called in second instance
//-------------------------------------------------------------------------------------
BOOL Unsethook ()
{
if (!s_threadid) {
return FALSE; There is no hook pending to close
}
if (G_hook) {//Check if Hook handler is valid
:: UnhookWindowsHookEx (G_hook); Unhook is doing here
S_threadid = 0; Remove thread ID to avoid continue sending
G_hook = NULL; Remove Hook handler to avoid to use it again
}
return TRUE; Hook has been removed
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.