Mar 8, 2015 at 1:54pm UTC
Hi all,
I'm new both here and in c++ programming, I just started today to program small c++ codes using Visual studio ultimate 2012, and i need help
I'm writing a code for sending mouse motions and events to a window that will respond to them and fire them.
I created a window containing a button holding event, when I click the button it will check if the notepad for example running.
if it is running it will send mouse events to it.
here is the code which I copied some of it from here and from which i wrote:
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow){
// The main window class name.
static TCHAR szWindowClass[] = _T("win32app");
// The string that appears in the application's title bar.
static TCHAR szTitle[] = _T("Omar first Win32 Application");
//HINSTANCE static hInst;
LRESULT CALLBACK WndProc(HWND,UINT, WPARAM, LPARAM);
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
if (!RegisterClassEx(&wcex))
{
MessageBox(NULL, _T("Call to RegisterClassEx failed!"), _T("Win32 Guided Tour"),NULL);
return 1;
}
HWND hwnd = CreateWindow (szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 400,500, NULL, NULL, hInstance, NULL);
if (!hwnd)
{
MessageBox(NULL, _T("Call to create Window Failed!!"),_T("Win32 Guided Tour"), NULL);
return 1;
}
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
MSG msg;
while ((GetMessage(&msg, NULL,0,0)))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam;
}
void MouseMove (int x, int y )
{
double fScreenWidth = ::GetSystemMetrics( SM_CXSCREEN )-1;
double fScreenHeight = ::GetSystemMetrics( SM_CYSCREEN )-1;
double fx = x*(65535.0f/fScreenWidth);
double fy = y*(65535.0f/fScreenHeight);
INPUT Input={0};
Input.type = INPUT_MOUSE;
Input.mi.dwFlags = MOUSEEVENTF_MOVE|MOUSEEVENTF_ABSOLUTE;
Input.mi.dx = fx;
Input.mi.dy = fy;
::SendInput(1,&Input,sizeof(INPUT));
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT message, WPARAM wParam, LPARAM lParam){
PAINTSTRUCT ps;
HDC hdc;
TCHAR greeting[] = _T("Hello Omar :)");
switch(message)
{
case WM_CREATE:{
HWND hwndButton = CreateWindowEx(NULL,TEXT("Button") ,TEXT("START"),WS_VISIBLE | WS_TABSTOP | WS_CHILD, 50,220,100,24, hwnd, (HMENU) 1, NULL, NULL);
break;
}
case WM_COMMAND: {
switch (LOWORD(wParam)){
case 1:
HWND handle = FindWindow(NULL, TEXT("Untitled - Notepad"));
if (!handle){
::MessageBox (hwnd,TEXT("Window Not Found"),TEXT("Window Not Found"),MB_OK);
}
else
{
::SetActiveWindow(handle);
::SetForegroundWindow(handle);
INPUT input = {0};
input.type = INPUT_MOUSE;
input.mi.dwFlags = MOUSEEVENTF_RIGHTDOWN;
::SendInput((UINT)handle,&input, sizeof(INPUT));
::ZeroMemory(&input, sizeof(INPUT));
input.type = INPUT_MOUSE;
input.mi.dwFlags = MOUSEEVENTF_RIGHTUP;
::SendInput((UINT)handle,&input, sizeof(INPUT));
}
break;
}
break;
}
case WM_PAINT:{
hdc = BeginPaint(hwnd, &ps);
TextOut(hdc,5,5, greeting, _tcslen(greeting));
EndPaint(hwnd, &ps);
break;
}
case WM_DESTROY:{
PostQuitMessage(0);
break;
}
default:{
return DefWindowProc(hwnd, message, wParam,lParam);
break;
}
}
return 0;
}
First,
Is is possible to send events "without moving the cursor"?
I guess it's possible!
Second,
how can we know if the events fired in the intended window?
thanks :)
Last edited on Mar 8, 2015 at 1:54pm UTC