can't get MouseInput to click - c++

been trying to make a basic auto clicker and cannot seem to get MouseInput to click the mouse. I believe it's probably something simple causing this issue. No errors occur it just won't do what it is intended to do.
Could somebody take a look at it and tell me whats wrong with it?
#include <iostream>
#include <Windows.h>
#include <thread>
#include <conio.h>
void loop()
{
int cps;
std::cout << "Enter desired clicks per second: ";
std::cin >> cps;
bool toggled = false;
while (true)
{
static bool bPressedToggle = false;
if (GetKeyState(VK_TAB) < 0)
bPressedToggle = true;
else if (bPressedToggle)
{
bPressedToggle = false;
toggled = !toggled;
std::cout << (toggled ? "Toggled" : "Disabled") << std::endl;
if (!toggled) continue;
}
if (toggled && (GetAsyncKeyState(VK_LBUTTON) & (1 << 16)))
{
POINT pntCurrentCursor;
GetCursorPos(&pntCurrentCursor);
INPUT inpMouseInput;
inpMouseInput.type = INPUT_MOUSE;
inpMouseInput.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
inpMouseInput.mi.dx = pntCurrentCursor.x;
inpMouseInput.mi.dy = pntCurrentCursor.y;
SendInput(1, &inpMouseInput, sizeof(INPUT)); // DOWN
RtlZeroMemory(&inpMouseInput, sizeof(INPUT));
inpMouseInput.type = INPUT_MOUSE;
inpMouseInput.mi.dwFlags = MOUSEEVENTF_LEFTUP;
inpMouseInput.mi.dx = pntCurrentCursor.x;
inpMouseInput.mi.dy = pntCurrentCursor.y;
SendInput(1, &inpMouseInput, sizeof(INPUT)); // UP
Sleep( 1000 / cps);
}
//Sleep(1);
}
}
int main()
{
loop();
return 0;
} ```

As a supplement, the screen coordinates returned by GetCursorPos need to be converted to absolute and passed to SendInput:
dx = (x * 65536) / GetSystemMetrics(SystemMetric.SM_CXSCREEN);
dy = (y * 65536) / GetSystemMetrics(SystemMetric.SM_CYSCREEN);
And #IInspectable had provided enough useful information, for this I have created a simple demo for reference only.
#include <iostream>
#include <Windows.h>
#include <thread>
#include <conio.h>
#define IDT_MOUSETRAP 100
HWND hWnd;
bool toggled = false;
int cps;
HANDLE hThread;
BOOL flag = true;
VOID CALLBACK MyTimerProc(HWND hwnd, UINT message, UINT idTimer, DWORD dwTime)
{
POINT pntCurrentCursor;
GetCursorPos(&pntCurrentCursor);
INPUT inpMouseInput;
inpMouseInput.type = INPUT_MOUSE;
inpMouseInput.mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTDOWN;
inpMouseInput.mi.dx = (pntCurrentCursor.x * 65536) / GetSystemMetrics(SM_CXSCREEN);;
inpMouseInput.mi.dy = (pntCurrentCursor.y * 65536) / GetSystemMetrics(SM_CYSCREEN);;
SendInput(1, &inpMouseInput, sizeof(INPUT)); // DOWN
RtlZeroMemory(&inpMouseInput, sizeof(INPUT));
inpMouseInput.type = INPUT_MOUSE;
inpMouseInput.mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTUP;
inpMouseInput.mi.dx = pntCurrentCursor.x;
inpMouseInput.mi.dy = pntCurrentCursor.y;
SendInput(1, &inpMouseInput, sizeof(INPUT)); // UP
}
DWORD WINAPI MyThreadFunction(LPVOID lpParam)
{
UINT uResult = SetTimer(hWnd, IDT_MOUSETRAP, 1000 / cps, (TIMERPROC)MyTimerProc);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, NULL, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return 0;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static bool bPressedToggle = false;
DWORD ThreadId;
switch (message)
{
case WM_CREATE:
{
std::cout << "Enter desired clicks per second: ";
std::cin >> cps;
}
break;
case WM_KEYDOWN:
{
if (GetKeyState(VK_TAB) < 0)
{
bPressedToggle = true;
if (bPressedToggle)
{
bPressedToggle = false;
toggled = !toggled;
std::cout << (toggled ? "Toggled" : "Disabled") << std::endl;
}
if (toggled == true && flag == true)
{
flag = false;
hThread = CreateThread(NULL, 0, MyThreadFunction, NULL, 0, &ThreadId);
}
else if (toggled == false && flag == false)
{
KillTimer(hWnd, IDT_MOUSETRAP);
flag = true;
}
}
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
void Createyourwindow()
{
WNDCLASSEXW wcex = { 0 };
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = GetModuleHandle(NULL);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszClassName = L"MyClass";
RegisterClassExW(&wcex);
hWnd = CreateWindowW(L"MyClass", L"window", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, GetModuleHandle(NULL), NULL);
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
MSG msg;
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, NULL, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
int main()
{
Createyourwindow();
return 0;
}
Note: Because the message loop of the window is needed, the mouse focus needs to be inside the window when pressing the TAB key.

Related

C++ImGui not fitting window

I'm working on a simple GUI, though my GUI window is unaligned and I cant see any issues in the code. I'm throwing this out there in hopes someone has seen this before because im pretty new to ImGui and have been looking at this code for days lol.
this GUI is using direct x 9.
here's the main .cpp file.
#pragma once
#include "gui.h"
#include "imgui.h"
#include "imgui_impl_dx9.h"
#include "imgui_impl_win32.h"
extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(
HWND window,
UINT message,
WPARAM wideParameter,
LPARAM longParameter
);
long __stdcall WindowProcess(
HWND window,
UINT message,
WPARAM wideParameter,
LPARAM longParameter
)
{
if (ImGui_ImplWin32_WndProcHandler(window, message, wideParameter, longParameter))
return true;
switch (message)
{
case WM_SIZE: {
if (gui::device && wideParameter != SIZE_MINIMIZED)
{
gui::presentParams.BackBufferWidth = LOWORD(longParameter);
gui::presentParams.BackBufferHeight = LOWORD(longParameter);
gui::ResetDevice();
}
}return 0;
case WM_SYSCOMMAND: {
if ((wideParameter & 0xfff0) == SC_KEYMENU)
return 0;
}break;
case WM_DESTROY: {
PostQuitMessage(0);
}return 0;
case WM_LBUTTONDOWN: {
gui::pos = MAKEPOINTS(longParameter);
}return 0;
case WM_MOUSEMOVE: {
if (wideParameter == MK_LBUTTON)
{
const auto points = MAKEPOINTS(longParameter);
auto rect = ::RECT{};
GetWindowRect(gui::window, &rect);
rect.left += points.x - gui::pos.x;
rect.top += points.y - gui::pos.y;
if (gui::pos.x >= 0 &&
gui::pos.x <= gui::w &&
gui::pos.y >= 0 && gui::pos.y <= 19)
SetWindowPos(
gui::window,
HWND_TOPMOST,
rect.left,
rect.top,
0, 0,
SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOZORDER
);
}
}
}
return DefWindowProcW(window, message, wideParameter, longParameter);
}
void gui::CreateHWindow(const char* windowName, const char* className) noexcept
{
windowClass.cbSize = sizeof(WNDCLASSEXA);
windowClass.style = CS_CLASSDC;
windowClass.lpfnWndProc = WindowProcess;
windowClass.cbClsExtra = 0;
windowClass.cbWndExtra = 0;
windowClass.hInstance = GetModuleHandleA(0);
windowClass.hIcon = 0;
windowClass.hCursor = 0;
windowClass.hbrBackground = 0;
windowClass.lpszMenuName = 0;
windowClass.lpszClassName = className;
windowClass.hIconSm = 0;
RegisterClassExA(&windowClass);
window = CreateWindowA(
className,
windowName,
WS_POPUP,
0,
0,
w,
h,
0,
0,
windowClass.hInstance,
0
);
ShowWindow(window, SW_SHOWDEFAULT);
UpdateWindow(window);
}
void gui::DestroyHWindow() noexcept
{
DestroyWindow(window);
UnregisterClass(windowClass.lpszClassName, windowClass.hInstance);
}
bool gui::CreateDevice() noexcept
{
d3d = Direct3DCreate9(D3D_SDK_VERSION);
if (!d3d)
return false;
ZeroMemory(&presentParams, sizeof(presentParams));
presentParams.Windowed = TRUE;
presentParams.SwapEffect = D3DSWAPEFFECT_DISCARD;
presentParams.BackBufferFormat = D3DFMT_UNKNOWN;
presentParams.EnableAutoDepthStencil = TRUE;
presentParams.AutoDepthStencilFormat = D3DFMT_D16;
presentParams.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
if (d3d->CreateDevice(
D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
window,
D3DCREATE_HARDWARE_VERTEXPROCESSING,
&presentParams,
&device) < 0)
return false;
return true;
}
void gui::ResetDevice() noexcept
{
ImGui_ImplDX9_InvalidateDeviceObjects();
const auto result = device->Reset(&presentParams);
if (result == D3DERR_INVALIDCALL)
IM_ASSERT(0);
ImGui_ImplDX9_CreateDeviceObjects();
}
void gui::DestroyDevice() noexcept
{
if (device)
{
device->Release();
device = nullptr;
}
if (d3d)
{
d3d->Release();
d3d = nullptr;
}
}
void gui::CreateGUI() noexcept
{
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ::ImGui::GetIO();
io.IniFilename = NULL;
// GUI style colors
ImGui::StyleColorsDark();
ImGui_ImplWin32_Init(window);
ImGui_ImplDX9_Init(device);
}
void gui::DestroyGUI() noexcept
{
ImGui_ImplDX9_Shutdown();
ImGui_ImplWin32_Shutdown();
ImGui::DestroyContext();
}
void gui::BeginRender() noexcept
{
MSG message;
while (PeekMessage(&message, 0, 0, 0, PM_REMOVE))
{
TranslateMessage(&message);
DispatchMessage(&message);
}
ImGui_ImplDX9_NewFrame();
ImGui_ImplWin32_NewFrame();
ImGui::NewFrame();
}
void gui::EndRender() noexcept
{
ImGui::EndFrame();
device->SetRenderState(D3DRS_ZENABLE, FALSE);
device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
device->Clear(0,0,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_RGBA(100, 0, 230, 255), 1.0f, 0);
if (device->BeginScene() >= 0)
{
ImGui::Render();
ImGui_ImplDX9_RenderDrawData(ImGui::GetDrawData());
device->EndScene();
}
const auto result = device->Present(0, 0, 0, 0);
if (result == D3DERR_DEVICELOST && device->TestCooperativeLevel() == D3DERR_DEVICENOTRESET)
ResetDevice();
}
void gui::Render() noexcept
{
ImGui::SetWindowPos({0,0 });
ImGui::SetNextWindowSize({ w - 100,h - 100});
ImGui::Begin(
"Window",
&exit,
ImGuiWindowFlags_NoResize |
ImGuiWindowFlags_NoSavedSettings |
ImGuiWindowFlags_NoCollapse |
ImGuiWindowFlags_NoMove
);
}
[![GUI Image][1]][1]
[1]: https://i.stack.imgur.com/YBGdn.png

Moving console input text lower

Problem:
I have a console chat. When I am typing a message and another person sends a message, their message gets printed right after the last character i typed, breaking it.
Example:
"HellAndrew: Hi!o!"
I try to type "Hello!", but "Andrew" interferes with a "Hi!"
How I'd like to handle this:
I was thinking that before printing a message from another client I copy the text I currently have in my standard input, print the other user's text then put the text back in the standard input, a row lower.
I tried using ReadConsoleInput/WriteConsoleInput APIs, but it does not do what I want and adds a significant (>10s) delay.
ReadConsoleInputW(GetStdHandle(STD_INPUT_HANDLE), Inputs, 100, &Count);
//printing
WriteConsoleInputW(GetStdHandle(STD_INPUT_HANDLE), Inputs, 100, &Count);
I cannot spot any other console APIs on MSDN that I can use. Perhaps I am using the others above wrong somehow.
I would appreciate some help in using these. Thanks!
After researching your question, I made the following test and can get the expected effect.
After reading another process string, use WriteConsoleOutputCharacter to write characters in different positions.
WriteConsoleOutputCharacterA(hStdOut, lpszString, strlen(lpszString), { 0,0 }, &dwWritten);
The coordinates of the cursor can be modified according to the actual situation, such as to achieve message scrolling.
Note: You can use SetConsoleCursorPosition to locate to another position to accept the input stream firstly.
Like this,
coord.X = 0;
coord.Y = 10;
SetConsoleCursorPosition(hStdOut, coord);
ReadFile(hStdIn, strMessage, 256, &dwRead, NULL);
Updated:
This is the sample I tested and I attach a quick gif demo.
Server.cpp
#include <Windows.h>
#include <stdio.h>
#include <iostream>
#include <thread>
using namespace std;
DWORD dwWritten;
DWORD num;
DWORD dwRead;
char strMessage[256];
HWND h_client;
void td1();
COORD coord;
SHORT i = 0;
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
HANDLE hStdIn = GetStdHandle(STD_INPUT_HANDLE);
if (message == WM_DESTROY) {
PostQuitMessage(0);
}
if (message == WM_COPYDATA)
{
COPYDATASTRUCT* pcds = (COPYDATASTRUCT*)lParam;
if (pcds->dwData == 1)
{
CHAR* lpszString = (CHAR*)(pcds->lpData);
WriteConsoleOutputCharacterA(hStdOut, lpszString, strlen(lpszString)-2, { 0,i++ }, &dwWritten);
}
if (i > 24)
{
system("cls");
i = 0;
coord.X = 0;
coord.Y = 25;
SetConsoleCursorPosition(hStdOut, coord);
printf("----------------------------------------------------------------------------------------------------------\n");
}
}
return DefWindowProc(hwnd, message, wParam, lParam);
};
HINSTANCE hinst;
int main() {
HWND hwnd;
hinst = GetModuleHandle(NULL);
// create a window class:
WNDCLASS wc = {};
wc.lpfnWndProc = WndProc;
wc.hInstance = hinst;
wc.lpszClassName = L"win32";
// register class with operating system:
RegisterClass(&wc);
// create and show window:
hwnd = CreateWindow(L"win32", L"Server", 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL);
std::thread t1(td1);
if (hwnd == NULL) {
return 0;
}
ShowWindow(hwnd, SW_SHOW);
MSG msg = {};
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
void td1()
{
HANDLE hStdIn = GetStdHandle(STD_INPUT_HANDLE);
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
while (1)
{
h_client = FindWindow(L"win32", L"Client");
if (h_client)
{
break;
}
}
coord.X = 0;
coord.Y = 25;
SetConsoleCursorPosition(hStdOut, coord);
printf("----------------------------------------------------------------------------------------------------------");
while (1)
{
coord.X = 0;
coord.Y = 26;
SetConsoleCursorPosition(hStdOut, coord);
memset(strMessage, 0, 256);
ReadFile(hStdIn, strMessage, 256, &dwRead, NULL);
SetConsoleCursorPosition(hStdOut, coord);
for (int i = strlen(strMessage); i > 0; i--)
{
putchar(32);
}
COPYDATASTRUCT cds;
cds.dwData = 1; // can be anything
cds.cbData = sizeof(CHAR) * strlen(strMessage);
cds.lpData = strMessage;
SendMessage(h_client, WM_COPYDATA, (WPARAM)h_client, (LPARAM)(LPVOID)&cds);
}
}
Client.cpp
#include <Windows.h>
#include <stdio.h>
#include <iostream>
#include <thread>
using namespace std;
DWORD dwWritten;
DWORD dwRead;
char strMessage[256];
HWND h_server;
SHORT i = 0;
COORD coord;
void td1();
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
HANDLE hStdIn = GetStdHandle(STD_INPUT_HANDLE);
if (message == WM_DESTROY) {
PostQuitMessage(0);
}
if (message == WM_COPYDATA)
{
COPYDATASTRUCT* pcds = (COPYDATASTRUCT*)lParam;
if (pcds->dwData == 1)
{
CHAR* lpszString = (CHAR*)(pcds->lpData);
int len = pcds->cbData;
WriteConsoleOutputCharacterA(hStdOut, lpszString, len - 2, { 0,i++ }, &dwWritten);
}
if (i > 24)
{
system("cls");
i = 0;
coord.X = 0;
coord.Y = 25;
SetConsoleCursorPosition(hStdOut, coord);
printf("----------------------------------------------------------------------------------------------------------\n");
}
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
HINSTANCE hinst;
int main() {
HWND hwnd;
hinst = GetModuleHandle(NULL);
// create a window class:
WNDCLASS wc = {};
wc.lpfnWndProc = WndProc;
wc.hInstance = hinst;
wc.lpszClassName = L"win32";
// register class with operating system:
RegisterClass(&wc);
// create and show window:
hwnd = CreateWindow(L"win32", L"Client", 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL);
std::thread t1(td1);
if (hwnd == NULL) {
return 0;
}
ShowWindow(hwnd, SW_SHOW);
MSG msg = {};
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
void td1()
{
HANDLE hStdIn = GetStdHandle(STD_INPUT_HANDLE);
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
while (1)
{
h_server = FindWindow(L"win32", L"Server");
if (h_server)
{
break;
}
}
coord.X = 0;
coord.Y = 25;
SetConsoleCursorPosition(hStdOut, coord);
printf("----------------------------------------------------------------------------------------------------------");
while (1)
{
coord.X = 0;
coord.Y = 26;
SetConsoleCursorPosition(hStdOut, coord);
memset(strMessage, 0, 256);
ReadFile(hStdIn, strMessage, 256, &dwRead, NULL);
SetConsoleCursorPosition(hStdOut, coord);
for (int i = strlen(strMessage); i > 0; i--)
{
putchar(32);
}
COPYDATASTRUCT cds;
cds.dwData = 1; // can be anything
cds.cbData = sizeof(CHAR) * (strlen(strMessage) + 1);
cds.lpData = strMessage;
SendMessage(h_server, WM_COPYDATA, (WPARAM)h_server, (LPARAM)(LPVOID)&cds);
}
}
In fact, the code on the client and server is almost the same. I did it by sending WM_COPYDATA message.
This only applies to two different processes on the same computer. For the console chat of two machines, it is necessary to rely on the TCP protocol.

Win32 SendInput mousemove lag and freeze

I am trying to move the mouse (on Windows 10) using SendInput when I perform a physical mouse click. It works fine if I click once or twice, but if clicking for examples 6 times in quick succession the mouse lags for a few seconds then the program stops responding.
Is there any obvious reason why this is happening?
(Edited)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
LRESULT CALLBACK MouseHook(int, WPARAM, LPARAM);
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, LPWSTR, int)
{
HHOOK hook = SetWindowsHookEx(WH_MOUSE_LL, MouseHook, NULL, 0);
MessageBox(NULL, L"Hello", L"Hello", MB_OK);
UnhookWindowsHookEx(hook);
return 0;
}
LRESULT CALLBACK MouseHook(int nCode, WPARAM wParam, LPARAM lParam) {
if (nCode == HC_ACTION) {
switch (wParam) {
case WM_RBUTTONUP:
INPUT buffer;
ZeroMemory(&buffer, sizeof(buffer));
buffer.type = INPUT_MOUSE;
buffer.mi.dx = 0;
buffer.mi.dy = 10;
buffer.mi.mouseData = 0;
buffer.mi.dwFlags = MOUSEEVENTF_MOVE;
buffer.mi.time = 0;
buffer.mi.dwExtraInfo = 0;
SendInput(1, &buffer, sizeof(INPUT));
}
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
Using Raw Input sample:
#include <windows.h>
#include <iostream>
using namespace std;
BOOL registerTouchpadForInput(HWND hWnd)
{
RAWINPUTDEVICE rid;
rid.dwFlags = RIDEV_NOLEGACY | RIDEV_INPUTSINK;
rid.usUsagePage = 1;
rid.usUsage = 2;
rid.hwndTarget = hWnd;
return RegisterRawInputDevices(&rid, 1, sizeof(rid));
}
void getinputdata(LPARAM lparam)
{
HRAWINPUT hInput = (HRAWINPUT)lparam;
RAWINPUT input = { 0 };
UINT size = sizeof(RAWINPUT);
GetRawInputData(hInput, RID_INPUT,&input, &size,sizeof(RAWINPUTHEADER));
if (RIM_TYPEMOUSE == input.header.dwType)
{
if (input.data.mouse.usButtonFlags & RI_MOUSE_RIGHT_BUTTON_DOWN)
{
INPUT buffer;
ZeroMemory(&buffer, sizeof(buffer));
buffer.type = INPUT_MOUSE;
buffer.mi.dx = 0;
buffer.mi.dy = 10;
buffer.mi.mouseData = 0;
buffer.mi.dwFlags = MOUSEEVENTF_MOVE;
buffer.mi.time = 0;
buffer.mi.dwExtraInfo = 0;
SendInput(1, &buffer, sizeof(INPUT));
}
}
return;
}
static LRESULT CALLBACK NVTouch_WindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
BOOL registrationStatus = false;
switch (message)
{
case WM_CREATE:
registrationStatus = registerTouchpadForInput(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_INPUT:
getinputdata(lParam);
return DefWindowProc(hwnd, message, wParam, lParam);
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
int main()
{
WNDCLASSEX wndclass = {
sizeof(WNDCLASSEX),
CS_DBLCLKS,
NVTouch_WindowProc,
0,
0,
GetModuleHandle(0),
LoadIcon(0,IDI_APPLICATION),
LoadCursor(0,IDC_ARROW),
HBRUSH(COLOR_WINDOW + 1),
0,
L"myclass",
LoadIcon(0,IDI_APPLICATION)
};
bool isClassRegistered = false;
isClassRegistered = RegisterClassEx(&wndclass);
if (isClassRegistered)
{
HWND window = CreateWindow(wndclass.lpszClassName, NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, GetModuleHandle(0), NULL);
if (window)
{
ShowWindow(window, SW_SHOWDEFAULT);
MSG msg;
while (GetMessage(&msg, 0, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
}

how to use mouse move in WinAPI without lag?

i hope to make a console when i click right button,
mouse move down little bit
i tried this code,
bool MouseMove(int x, int y)
{
INPUT input;
input.type = INPUT_MOUSE;
input.mi.mouseData = 0;
input.mi.time = 0;
input.mi.dx = x;
input.mi.dy = y;
input.mi.dwFlags = MOUSEEVENTF_MOVE;
SendInput(1, &input, sizeof(input));
return true;
}
HWND hWnd;
HHOOK hMSHook;
LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode >= HC_ACTION)
{
if (wParam == WM_RBUTTONUP)
{
MouseMove(0, 10);
return 1;
}
}
return CallNextHookEx(hKBHook, nCode, wParam, lParam);
}
int _tmain() {
hWnd = ::GetConsoleWindow();
ShowWindow(hWnd, 0);
HMODULE hInstance = GetModuleHandle(NULL);
hMSHook = SetWindowsHookEx(WH_MOUSE_LL, MouseProc, hInstance, NULL);
MSG Msg;
while (GetMessage(&Msg, NULL, 0, 0)) { DispatchMessage(&Msg); }
return 0;
}
It works fine, but if i try it few times,
it causing lag (struggle, slow, laggy)
someone know how to fix it?

0xC0000005: Access violation reading location 0x0000000000000000. c++ [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm having a very annoying issue for something I'm working on. I've tried numerous solutions and researching this error doesnt help solve my exact issue. Is anyone able to help? I've included my Overlay.cpp below...
#include "Overlay.h"
Overlay::Overlay() {
m_hWnd = NULL;
m_hGame = NULL;
m_pDirect3D = nullptr;
m_pDevice = nullptr;
memset(&m_Present, NULL, sizeof(D3DPRESENT_PARAMETERS));
}
Overlay::~Overlay() {
if (m_pDevice) {
m_pDevice->Release();
m_pDevice = nullptr;
}
if (m_pDirect3D) {
m_pDirect3D->Release();
m_pDirect3D = nullptr;
}
}
bool Overlay::Attach(HWND hWnd) {
m_hGame = hWnd;
if (!m_hGame) {
return false;
}
RECT client;
GetClientRect(m_hGame, &client);
m_nSize[0] = client.right;
m_nSize[1] = client.bottom;
WNDCLASSEX wc = { NULL };
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_VREDRAW | CS_HREDRAW;
wc.lpfnWndProc = WndProcedure;
wc.cbClsExtra = NULL;
wc.cbWndExtra = NULL;
wc.hInstance = NULL;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.lpszMenuName = NULL;
wc.lpszClassName = OVERLAY_NAME;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if (!RegisterClassEx(&wc)) {
return false;
}
m_hWnd = CreateWindowEx(WS_EX_TOPMOST | WS_EX_COMPOSITED | WS_EX_TRANSPARENT | WS_EX_LAYERED, OVERLAY_NAME, OVERLAY_NAME, WS_POPUP, CW_USEDEFAULT, CW_USEDEFAULT, m_nSize[0], m_nSize[1], NULL, NULL, NULL, NULL);
if (!m_hWnd) {
return false;
}
MARGINS margin = { -1, -1, -1, -1 };
DwmExtendFrameIntoClientArea(m_hWnd, &margin);
ShowWindow(m_hWnd, SW_SHOWDEFAULT);
UpdateWindow(m_hWnd);
RECT game;
GetWindowRect(m_hGame, &game);
RECT cl;
GetClientRect(m_hGame, &cl);
int w = game.right - game.left;
int h = game.bottom - game.top;
LONG_PTR dwStyle = GetWindowLongPtr(m_hGame, GWL_STYLE);
if (dwStyle & WS_BORDER) {
int x = GetSystemMetrics(SM_CXBORDER);
int y = GetSystemMetrics(SM_CYCAPTION) + GetSystemMetrics(SM_CYBORDER);
game.left += x;
game.top += y;
w -= x;
h -= y;
}
MoveWindow(m_hWnd, game.left, game.top, w, h, TRUE);
m_nSize[0] = w;
m_nSize[1] = h;
return InitDirectX();
}
int Overlay::OnFrame() {
MSG msg;
while (true) {
Sleep(1);
if (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE)) {
if (msg.message == WM_QUIT) {
break;
}
TranslateMessage(&msg);
DispatchMessageA(&msg);
} else {
m_pDevice->Clear(NULL, NULL, D3DCLEAR_TARGET, NULL, 1.0f, NULL);
m_pDevice->BeginScene();
if (!m_pOnFrameList.empty()) {
for (auto& pOnFrame : m_pOnFrameList) {
if (pOnFrame) {
pOnFrame();
}
}
}
m_pDevice->EndScene();
m_pDevice->Present(NULL, NULL, NULL, NULL);
}
}
return (int)msg.wParam;
}
void Overlay::AddOnFrame(const OnFrameFn& pFunction) {
m_pOnFrameList.push_back(pFunction);
}
void Overlay::GetScreenSize(int* width, int* height) {
if (width) {
*width = m_nSize[0];
}
if (height) {
*height = m_nSize[1];
}
}
IDirect3DDevice9* Overlay::GetDevice() const {
return m_pDevice;
}
bool Overlay::InitDirectX() {
m_Present.EnableAutoDepthStencil = TRUE;
m_Present.AutoDepthStencilFormat = D3DFMT_D16;
m_Present.Windowed = TRUE;
m_Present.BackBufferCount = 1;
m_Present.BackBufferFormat = D3DFMT_A8R8G8B8;
m_Present.BackBufferWidth = m_nSize[0];
m_Present.BackBufferHeight = m_nSize[1];
m_Present.MultiSampleType = D3DMULTISAMPLE_NONE;
m_Present.SwapEffect = D3DSWAPEFFECT_DISCARD;
m_Present.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
m_Present.hDeviceWindow = m_hWnd;
m_pDirect3D = Direct3DCreate9(D3D_SDK_VERSION);
if (!m_pDirect3D) {
return false;
}
if (FAILED(m_pDirect3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &m_Present, &m_pDevice))) {
return false;
}
if (!m_pDevice) {
return false;
}
return true;
}
LRESULT WINAPI Overlay::WndProcedure(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
switch (message) {
case WM_DESTROY:
PostQuitMessage(NULL);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return NULL;
}
My error occurs at this line m_pDevice->Clear(NULL, NULL, D3DCLEAR_TARGET, NULL, 1.0f, NULL);
Any help would be much appreciated
NULL means 0x0.
So probably you are passing NULL to m_pDevice->Clear() where its going to be read.
Additionally, make sure that m_pDevice is not nullptr.