Using hooks to realize the modification of QQ Chat window in Delphi

Source: Internet
Author: User
Tags exit
Someone once for the company do not let QQ, MSN and other chat tools and worry about it? Look at the following applet and you will dismiss this view.

When the company made a campus card project, inside the computer room client needs to use the system's low-level keyboard hook wh_keyboard_ll, this is my first contact to Windows in the hook, because Wh_keyboard_ll and other hooks are not the same, There is no definition in Delphi's Windows Help and Windows.pas files, but there is a wh_keyboard_ll introduction to Microsoft's MSDN, so you're interested in Windows ' system hooks as you work through them. After having done the mouse hook wh_mouse, low-level mouse hook wh_mouse_ll and message hook Wh_getmessage Small example, are very interesting.

This time I'm using another system hook: WH_CBT.

Looking at the help of Delphi, found that there are many functions in Cbtproc:

Hcbt_activate
Hcbt_createwnd
Hcbt_destroywnd
Hcbt_minmax
Hcbt_movesize
Hcbt_setfocus
Hcbt_syscommand

(specifically do not introduce, see Help write very clearly)

Then we started experimenting and finally finished this little program. Let me briefly introduce:

Run the program automatically hidden, when you extract the QQ or MSN messages will be surprised to find the chat window of the annoying "and xx chat" title is not, and the title replaced by "My Documents." If you change the icon for this Chat window to a document-style icon, who can see that it's your chat window? (Replacement of the icon is a lot of methods, you can use the program, you can also use the software directly modify the resources of the QQ file, this Part I did not do, here is just to provide you with a train of thought, interested friends can try it, hehe).

The following code posts:

Library HOOKPRJ;

Uses
Sysutils,
Classes,
Qqtitlehook in ' Qqtitlehook.pas ';

Exports
Enablewheelhook, Disablewheelhook;

Begin
hkqqchat:= 0;
End.
==========================================================
Unit Qqtitlehook;

Interface

Uses
Windows, Messages, Sysutils, Dialogs, Commctrl, strutils;

Var
Hkqqchat:hhook;
Handle to Chat window
Hwqqchat:hwnd;
The title of the chat window
tlqqchat:string;
Window class name
clsname:string;
Buf:array [0..1024] of char;

Const
QQ Chat window class name
csqq = ' #32770 ';

function Titlehookproc (code:integer; wparam:wparam; lparam:lparam): Lresult; stdcall;
function Enumwindowstitlefunc (handle:thandle; lparam:lparam): Boolean; stdcall;
function Enablewheelhook:boolean; stdcall; Export
function Disablewheelhook:boolean; stdcall; Export

Implementation
 
handle function of Hook
function Titlehookproc (code:integer; wparam:wparam; lparam:lparam): Lresult; stdcall;
Begin
result:= 0;
If Code<0 Then
Begin
result:= CallNextHookEx (Hkqqchat, Code, WParam, LParam);
Exit;
End
Else
If Code = Hcbt_activate Then
Begin
Gets the handle to the activation window, as well as the window class name, and then determines whether the window class name is #32770.
hwqqchat:= HWND (WParam);
GetClassName (Hwqqchat, buf, 1024);
clsname:= string (BUF);
If Clsname = Csqq Then
Begin
If the window class name is #32770, all the windows are enumerated, and the window handle is passed into the
"This is just a demo, because many windows have a #32770 class name, so it's very efficient to judge,"
"Interested friends can be based on the characteristics of QQ Chat window to increase the judgment conditions, so as to improve efficiency." 】
EnumWindows (@EnumWindowsTitleFunc, hwqqchat);
End
End
End

function Enumwindowstitlefunc (handle:thandle; lparam:lparam): Boolean; stdcall;
Begin
if (Handle = LParam) and Boolean (GetWindowText (Handle, BUF, 256)) then
Begin
Get the window caption according to the window handle
tlqqchat:= string (BUF);
Then determine if the title contains "with ... Chat "and other related characters, if included then this window for QQ Chat window
if ((POS(' and ', Tlqqchat) >0) and (POS (' chat ', tlqqchat) >0) Then
Begin
Make sure the window title is changed after the chat window.
Tlqqchat: = Ansireplacestr (Tlqqchat, ' and ', ' My Documents ');
Tlqqchat: = Ansireplacestr (tlqqchat, ' chat ', ');
SetWindowText (Handle, Pchar (tlqqchat));
End
"Ditto, this landFang daHome is free to control, not limited to QQ, MSN and other chat windows. 】
"and want to change the title to what is also free control, if you can according to the modified window icon to determine the title"
"For example, by modifying the window icon to replace the Delphi icon, and then the title modified to Delphi7, who can see the flaw?" haha
if (POS (' Group-', Tlqqchat) >0) or (POS (' Advanced Group-', Tlqqchat) >0) Then
Begin
Tlqqchat: = Ansireplacestr (Tlqqchat, ' group-', ' My Documents ');
Tlqqchat: = Ansireplacestr (Tlqqchat, ' advanced ', ');
SetWindowText (Handle, Pchar (tlqqchat));
End
Msn
If POS ('-Dialogs ', tlqqchat) >0 Then
Begin
Tlqqchat: = Ansireplacestr (Tlqqchat, '-dialogue ', ' My Documents ');
SetWindowText (Handle, Pchar (tlqqchat));
End
End
Result: =true;
End

Start Hook
function Enablewheelhook:boolean; stdcall; Export
Begin
If Hkqqchat=0 Then
Begin
Hkqqchat: = SetWindowsHookEx (WH_CBT, @TitleHookProc, hinstance, 0);
Result: = True;
End
Else
Result: = False;
End

Uninstall Hook
function Disablewheelhook:boolean; stdcall; Export
Begin
If Hkqqchat<>0 Then
Begin
UnhookWindowsHookEx (Hkqqchat);
Hkqqchat: = 0;
Result: = True;
End
Else
Result: = False;
End

End.
====================================

Call the application is very simple, call Enablewheelhook after the hide can be, exit when Disablewheelhook OK.

program only to provide you with a train of thought, there are many imperfect places, I hope that interested friends contact me and improve the common exchange.

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.