c++模拟QQ尾巴功能
2012-10-10 10:54:05 来源:WEB开发网核心提示: 基本原理如下:因为QQ消息大都是通过回车键或回车键加其他键的组合键来发送的,所以只要利用键盘钩子钩住系统中的回车键的WM_KEYDOWN消息即可,c++模拟QQ尾巴功能,检测到回车键按下时,首先检测当前拥有输入焦点的窗口是否是QQ聊天窗口,完全可以做到自定义,</p><p>因为只是简单的使用了
基本原理如下:
因为QQ消息大都是通过回车键或回车键加其他键的组合键来发送的,所以只要利用键盘钩子钩住系统中的回车键的WM_KEYDOWN消息即可。
检测到回车键按下时,
首先检测当前拥有输入焦点的窗口是否是QQ聊天窗口,这里对QQ聊天窗口的判断是,检查窗口类名是否是“TXGuiFoundation”(这个值通过spy++获取的)。
然后将“QQ尾巴”内容设置到windows剪切板中。
然后模拟点击 ctrl + V ,将剪切板中的内容粘贴到当前拥有焦点的窗口(QQ聊天窗口)中。
最后继续回车键按下的消息处理。
<div class="dp-highlighter bg_csharp"><div class="bar"><div class="tools"><b>[csharp]</b> <a href="#" class="ViewSource" title="view plain" onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><a href="#" class="CopyToClipboard" title="copy" onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><a href="#" class="PrintSource" title="print" onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a><a href="#" class="About" title="?" onclick="dp.sh.Toolbar.Command('About',this);return false;">?</a><div style="position: absolute; left: 0px; top: 0px; width: 0px; height: 0px; z-index: 99; "><embed id="ZeroClipboardMovie_2" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="0" height="0" name="ZeroClipboardMovie_2" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=2&width=0&height=0" wmode="transparent"></div></div></div><ol start="1" class="dp-c"><li class="alt"><span><span>LRESULT CALLBACK CMainWnd::KeyBoardProc(</span><span class="keyword">int</span><span> nCode, WPARAM wParam, LPARAM lParam) </span><span class="comment">// 钩子处理函数</span><span> </span></span></li><li class=""><span>{ </span></li><li class="alt"><span> <span class="keyword">if</span><span>(nCode >= 0) </span></span></li><li class=""><span> { </span></li><li class="alt"><span> <span class="keyword">if</span><span>(UINT(wParam) == WM_KEYDOWN) </span><span class="comment">// 判断是否为WM_KEYDOWN消息</span><span> </span></span></li><li class=""><span> </span></li><li class="alt"><span> { </span></li><li class=""><span> PKBDLLHOOKSTRUCT paramer = (PKBDLLHOOKSTRUCT)lParam; </span></li><li class="alt"><span> </span></li><li class=""><span> <span class="keyword">if</span><span>(m_f2down && paramer->vkCode == VK_RETURN) </span><span class="comment">// 判断是否按下回车键(这里把F2键设置成QQ尾巴的开关键)</span><span> </span></span></li><li class="alt"><span> { </span></li><li class=""><span> <span class="comment">// 获取用于焦点的创建句柄</span><span> </span></span></li><li class="alt"><span> <span class="keyword">const</span><span> DWORD tid = ::GetWindowThreadProcessId(GetForegroundWindow(),NULL); </span></span></li><li class=""><span> ::AttachThreadInput(tid, GetCurrentThreadId(),TRUE); </span></li><li class="alt"><span> CBaseWnd * pWndFocus = CBaseWnd::FromHandle(::GetFocus()); </span></li><li class=""><span> <span class="keyword">if</span><span>(pWndFocus != NULL) </span></span></li><li class="alt"><span> { </span></li><li class=""><span> <span class="keyword">if</span><span>(*(pWndFocus->GetClsName()) == TEXT(</span><span class="string">"TXGuiFoundation"</span><span>)) </span><span class="comment">// 检测是否是QQ聊天窗口</span><span> </span></span></li><li class="alt"><span> { </span></li><li class=""><span> <span class="comment">// 在剪切板中填充自己的信息</span><span> </span></span></li><li class="alt"><span> CClipboard cli; </span></li><li class=""><span> cli.Open(pWndFocus); </span></li><li class="alt"><span> LPCTSTR lpt = cli.ReadText(); <span class="comment">// 保存剪切板原来的值</span><span> </span></span></li><li class=""><span> cli.Clear(); <span class="comment">// 清空剪切板</span><span> </span></span></li><li class="alt"><span> cli.WriteText(TEXT(<span class="string">"\r\n\r\n(本消息由该成员通过iphone4 for qq发送,体验手机QQ请登录:http://mobile.qq.com) \r\n"</span><span>)); </span></span></li><li class=""><span> </span></li><li class="alt"><span> cli.Close(); <span class="comment">// 这里必须关闭剪切板,不然下面就无法向指定的窗口发送粘贴消息了</span><span> </span></span></li><li class=""><span> </span></li><li class="alt"><span> <span class="comment">// 发送粘贴键盘消息</span><span> </span></span></li><li class=""><span> CKMInput kmInput; </span></li><li class="alt"><span> kmInput.Key_Two_Higher(VK_CONTROL, <span class="string">'V'</span><span>, 10);</span><span class="comment">// 发送ctrl + v</span><span> </span></span></li><li class=""><span> </span></li><li class="alt"><span> cli.Open(pWndFocus); </span></li><li class=""><span> cli.Clear(); </span></li><li class="alt"><span> cli.WriteText(lpt); <span class="comment">// 恢复剪切板中值</span><span> </span></span></li><li class=""><span> cli.Close(); </span></li><li class="alt"><span> } </span></li><li class=""><span> } </span></li><li class="alt"><span> } </span></li><li class=""><span> </span></li><li class="alt"><span> <span class="keyword">if</span><span>(paramer->vkCode == VK_F2) </span><span class="comment">// 按下F2键,打开或关闭QQ尾巴开关</span><span> </span></span></li><li class=""><span> m_f2down = !m_f2down; </span></li><li class="alt"><span> </span></li><li class=""><span> </span></li><li class="alt"><span> </span></li><li class=""><span> </span></li><li class="alt"><span> } </span></li><li class=""><span> } </span></li><li class="alt"><span> </span></li><li class=""><span> <span class="keyword">return</span><span> ::CallNextHookEx(hookKey.GetHook(), nCode, wParam, lParam); </span><span class="comment">// 处理完成后,继续消息的传递</span><span> </span></span></li><li class="alt"><span>} </span></li></ol></div><pre name="code" class="csharp" style="display: none; ">LRESULT CALLBACK CMainWnd::KeyBoardProc(int nCode, WPARAM wParam, LPARAM lParam) // 钩子处理函数 { if(nCode >= 0) { if(UINT(wParam) == WM_KEYDOWN) // 判断是否为WM_KEYDOWN消息 { PKBDLLHOOKSTRUCT paramer = (PKBDLLHOOKSTRUCT)lParam; if(m_f2down && paramer->vkCode == VK_RETURN) // 判断是否按下回车键(这里把F2键设置成QQ尾巴的开关键) { // 获取用于焦点的创建句柄 const DWORD tid = ::GetWindowThreadProcessId(GetForegroundWindow(),NULL); ::AttachThreadInput(tid, GetCurrentThreadId(),TRUE); CBaseWnd * pWndFocus = CBaseWnd::FromHandle(::GetFocus()); if(pWndFocus != NULL) { if(*(pWndFocus->GetClsName()) == TEXT("TXGuiFoundation")) // 检测是否是QQ聊天窗口 { // 在剪切板中填充自己的信息 CClipboard cli; cli.Open(pWndFocus); LPCTSTR lpt = cli.ReadText(); // 保存剪切板原来的值 cli.Clear(); // 清空剪切板 cli.WriteText(TEXT("\r\n\r\n(本消息由该成员通过iphone4 for qq发送,体验手机QQ请登录:http://mobile.qq.com) \r\n")); cli.Close(); // 这里必须关闭剪切板,不然下面就无法向指定的窗口发送粘贴消息了 // 发送粘贴键盘消息 CKMInput kmInput; kmInput.Key_Two_Higher(VK_CONTROL, 'V', 10);// 发送ctrl + v cli.Open(pWndFocus); cli.Clear(); cli.WriteText(lpt); // 恢复剪切板中值 cli.Close(); } } } if(paramer->vkCode == VK_F2) // 按下F2键,打开或关闭QQ尾巴开关 m_f2down = !m_f2down; } } return ::CallNextHookEx(hookKey.GetHook(), nCode, wParam, lParam); // 处理完成后,继续消息的传递 } </pre> <pre></pre> <br> <br> <p><strong>实际效果如图:</strong></p> <p><img src="http://www.15597.com/skins/15597/logo.gif" alt=""></p> <p>在每次单击回车键时,都会在聊天内容下面添加一个自己的尾巴。</p> <p></p> <p><strong>遗留问题:</strong></p> <p>为了简单,这里没有处理点击发送按钮发送消息的情况。其实这种情况也一样可以通过钩子来解决。</p> <p>还有一点是,QQ尾巴内容是在代码中写死的,完全可以做到自定义。</p> <p>因为只是简单的使用了钩子,所以会遭到杀毒工具的警告。</p>
更多精彩
赞助商链接