void Executor::clickTo(WId hwnd, int x, int y) { RECT rect; GetWindowRect(hwnd, &rect); static int x_border = 0; static int y_border = 0; if (x_border == 0) { QRect wndRect(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top); RECT clrect; GetClientRect(hwnd, &clrect); QRect clntRect(clrect.left, clrect.top, clrect.right - clrect.left, clrect.bottom - clrect.top); x_border = (int)(wndRect.width() - clntRect.width()) / 2.; y_border = wndRect.height() - clntRect.height() - x_border; } x += rect.left + x_border; y += rect.top + y_border; HwndToTop(hwnd); long X = (long)(x * ABSOLUTE_COORD)/screen_res_x_; long Y = (long)(y * ABSOLUTE_COORD)/screen_res_y_; Hooker hook(Hooker::Mouse); //запомнить текущие координаты мышки POINT pt; GetCursorPos(&pt); long cX = (long)(pt.x * ABSOLUTE_COORD)/screen_res_x_; long cY = (long)(pt.y * ABSOLUTE_COORD)/screen_res_y_; mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, X, Y, 0, 0); Sleep(100); mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTDOWN, X, Y, 0, 0); Sleep(100); mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTUP, X, Y, 0, 0); Sleep(100); //вернуть координаты мышки откуда взяли mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, cX, cY, 0, 0); }
LRESULT FlatButton::WndProc(UINT nmsg, WPARAM wparam, LPARAM lparam) { switch(nmsg) { case WM_MOUSEMOVE: { bool active = false; if (IsWindowEnabled(_hwnd)) { DWORD pid_foreground; HWND hwnd_foreground = GetForegroundWindow(); //@@ may be better look for WM_ACTIVATEAPP ? GetWindowThreadProcessId(hwnd_foreground, &pid_foreground); if (GetCurrentProcessId() == pid_foreground) { POINT pt = {GET_X_LPARAM(lparam), GET_Y_LPARAM(lparam)}; ClientRect clntRect(_hwnd); // highlight the button? if (pt.x>=clntRect.left && pt.x<clntRect.right && pt.y>=clntRect.top && pt.y<clntRect.bottom) active = true; } } if (active != _active) { _active = active; if (active) { TRACKMOUSEEVENT tme = {sizeof(tme), /*TME_HOVER|*/TME_LEAVE, _hwnd/*, HOVER_DEFAULT*/}; _TrackMouseEvent(&tme); } InvalidateRect(_hwnd, NULL, TRUE); } return 0;} case WM_LBUTTONUP: { POINT pt = {GET_X_LPARAM(lparam), GET_Y_LPARAM(lparam)}; ClientRect clntRect(_hwnd); // no more in the active rectangle? if (pt.x<clntRect.left || pt.x>=clntRect.right || pt.y<clntRect.top || pt.y>=clntRect.bottom) goto cancel_press; goto def;} case WM_CANCELMODE: cancel_press: { TRACKMOUSEEVENT tme = {sizeof(tme), /*TME_HOVER|*/TME_LEAVE|TME_CANCEL, _hwnd/*, HOVER_DEFAULT*/}; _TrackMouseEvent(&tme); _active = false; ReleaseCapture();} // fall through case WM_MOUSELEAVE: if (_active) { _active = false; InvalidateRect(_hwnd, NULL, TRUE); } return 0; default: def: return super::WndProc(nmsg, wparam, lparam); } }