yes like @HansPassant mentioned std::cout is not efficient way to display things unless u make your console refresh each time it's updated.
I have remade the whole program, because I needed too, using code:blocks default win32 template and replaced MouseHookProc :)
#include <windows.h>
#include <windowsx.h>
#include <iostream>
#include <sstream>
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK MouseHookProc(int nCode, WPARAM wParam, LPARAM lParam);
char szClassName[ ] = "CodeBlocksWindowsApp";
HWND Label1, Label2; //adding 2 static controls to display things
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
// adding hook
HHOOK mouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseHookProc, hThisInstance, NULL);
HWND hwnd;
MSG messages;
WNDCLASSEX wincl;
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure;
wincl.style = CS_DBLCLKS;
wincl.cbSize = sizeof (WNDCLASSEX);
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL;
wincl.cbClsExtra = 0;
wincl.cbWndExtra = 0;
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
if (!RegisterClassEx (&wincl))
return 0;
hwnd = CreateWindowEx (0, szClassName, "Code::Blocks Template Windows App",
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 544, 375,
HWND_DESKTOP, NULL, hThisInstance, NULL);
ShowWindow (hwnd, nCmdShow);
while (GetMessage (&messages, NULL, 0, 0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return messages.wParam;
}
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
//creating those 2 static controls
Label1 = CreateWindow("STATIC", "Label1", WS_VISIBLE | WS_CHILD, 10,10,60,18, hwnd, NULL, NULL, NULL);
Label2 = CreateWindow("STATIC", "Label2", WS_VISIBLE | WS_CHILD, 70,10,60,18, hwnd, NULL, NULL, NULL);
break;
//adding mousemove event
case WM_MOUSEMOVE: {
int xPos = GET_X_LPARAM(lParam);
int yPos = GET_Y_LPARAM(lParam);
//std::cout << xPos << " - " << yPos << std::endl;
TCHAR Coords[20];
wsprintf(Coords, "%i, %i", xPos, yPos);
SetWindowText(Label1, Coords);
return 0; }
case WM_DESTROY:
PostQuitMessage (0);
break;
default:
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
//replacing MouseHookProc
LRESULT CALLBACK MouseHookProc(int nCode, WPARAM wParam, LPARAM lParam) {
if (nCode >= 0 && wParam == WM_MOUSEMOVE) {
MSLLHOOKSTRUCT* mh = (MSLLHOOKSTRUCT*)lParam;
std::stringstream ss;
ss << mh->pt.x << ", " << mh->pt.y << std::endl;
//OutputDebugStringA(ss.str().c_str());
SetWindowText(Label2, ss.str().c_str());
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
MouseHookProcshould always callCallNextHookExunless you wish to filter out the message altogether. In the latter case your hook procedure should return a nonzero value. Failing to meet the requirements will break other hooks in the system. Make sure to readLowLevelMouseProcthoroughly. Everything I just said is spelled out there.MouseHookProcneeds to be fixed.