Skip to main content

[原创软件] 人人网自动抢沙发(附源码)

· 3 min read

2011年9月的时候,我们网络部的同学和学长们在人人网开始了抢沙发比赛。于是写了一个人人网自动抢沙发的小工具,让我们部门永远都能坐上沙发。今天分享下源码,仅供学习参考。

功能说明

  • 自动检测人人网好友发的新状态
  • 自动点击抢沙发按钮
  • 自动关闭留言窗口

实现原理

通过 Windows API 检测特定窗口类名和尺寸,模拟鼠标点击实现自动留言。

主要源码分享

#include <windows.h>
#include <process.h>
#include "resource.h"

#define CW_SCREEN 1
#define CW_WORKAREA 3

// 窗口居中显示
BOOL WINAPI CenterMainWindow(HWND hWnd, DWORD dwFlags)
{
RECT rtWindow = {0};
RECT rtContainer = {0};

GetWindowRect(hWnd, &rtWindow);
rtWindow.right -= rtWindow.left;
rtWindow.bottom -= rtWindow.top;

switch(dwFlags)
{
case CW_SCREEN:
rtContainer.right = GetSystemMetrics(SM_CXSCREEN);
rtContainer.bottom = GetSystemMetrics(SM_CYSCREEN);
break;
case CW_WORKAREA:
SystemParametersInfo(SPI_GETWORKAREA, 0, &rtContainer, 0);
break;
}

return SetWindowPos(hWnd, HWND_TOP,
(rtContainer.right - rtWindow.right) / 2,
(rtContainer.bottom - rtWindow.bottom) / 2,
0, 0, SWP_NOSIZE | SWP_HIDEWINDOW);
}

// 检测是否为人人网留言窗口
bool IsRenrenCommentWindow(HWND hwnd)
{
TCHAR buff[256];
GetClassName(hwnd, buff, 255);
if (wcscmp(buff, L"GTK2-W") == 0)
{
RECT rct;
GetClientRect(hwnd, &rct);
if(rct.right == 272 && rct.bottom > 110)
{
return true;
}
}
return false;
}

POINT cursorPos; // 鼠标位置
RECT commentWndRect; // 留言窗口位置

// 自动点击留言按钮并关闭窗口
BOOL CALLBACK AutoPostComment(HWND hwnd, LPARAM lParam)
{
if(IsRenrenCommentWindow(hwnd))
{
OutputDebugString(L"找到留言窗口");
GetWindowRect(hwnd, &commentWndRect);
GetCursorPos(&cursorPos);

// 点击发送按钮
int sendBtnX = commentWndRect.right - commentWndRect.left - 10;
int sendBtnY = commentWndRect.bottom - commentWndRect.top - 30;
for (int i = 0; i < 5; i++)
{
SendMessage(hwnd, WM_LBUTTONDOWN, 0, (sendBtnY << 16) + sendBtnX);
SendMessage(hwnd, WM_LBUTTONUP, 0, (sendBtnY << 16) + sendBtnX);
}

// 点击关闭按钮
int closeBtnX = commentWndRect.right - commentWndRect.left - 100;
int closeBtnY = commentWndRect.bottom - commentWndRect.top - 50;
SendMessage(hwnd, WM_LBUTTONDOWN, 0, (closeBtnY << 16) + closeBtnX);
SendMessage(hwnd, WM_LBUTTONUP, 0, (closeBtnY << 16) + closeBtnX);

PostMessage(hwnd, WM_CLOSE, 0, 0);
}
return 1;
}

// 查找并处理所有留言窗口
void ProcessCommentWindows()
{
EnumWindows(AutoPostComment, NULL);
}

// 安装定时器钩子
void InstallCommentHook(PVOID pvoid)
{
OutputDebugString(L"开始监控留言窗口");
SetTimer(NULL, 0, 200, (TIMERPROC)ProcessCommentWindows);

MSG msg;
while (GetMessage(&msg, 0, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

// 主窗口消息处理
LRESULT MainDlgProc(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
HWND hDlg = CreateDialog(hInstance, (LPCTSTR)IDD_DIALOG1, 0, (DLGPROC)MainDlgProc);
ShowWindow(hDlg, nCmdShow);

_beginthread(InstallCommentHook, 0, NULL);

while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}

// 主窗口过程
LRESULT MainDlgProc(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
switch (Msg)
{
case WM_CLOSE:
DestroyWindow(hDlg);
return TRUE;

case WM_DESTROY:
PostQuitMessage(0);
return TRUE;

case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
switch (wmId)
{
// 添加按钮处理
}
break;

case WM_INITDIALOG:
CenterMainWindow(hDlg, CW_SCREEN);
break;
}
return FALSE;
}