The code below - all i want to do is print the x and y coordinates in the window when the mouse moves. The problem is:
1. not sure how to include the cursor.position.x and y classes into the project.
2. not sure how to convert the values displayed above in the window that shows the text.
I was hoping someone could help me out with this. Thanks.
CODE
#include <windows.h>
#include <tchar.h>
#include <iostream>
//One line below uncommented produces an error but is needed for Cursor.Position.X etc
//using System.Windows.Forms
//Globals for reference
HWND hwndStatic;
TCHAR* Simple = TEXT("A window for <Dream in Code>");
void CreateWindowItems(HWND hwnd, HINSTANCE hInst)
{
//Text "area"
hwndStatic = CreateWindow(TEXT("static"), TEXT(" This is some text"),
WS_CHILD | WS_VISIBLE,
150, 100, 400, 200, hwnd, NULL, hInst, NULL);
return;
}
//message handler
LRESULT CALLBACK WinProc(HWND hwnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
switch(Msg)
{
case WM_CREATE:
//empty for now
break;
case WM_LBUTTONDOWN:
SetWindowText(hwndStatic, "You clicked the left mouse button!");
break;
case WM_RBUTTONDOWN:
SetWindowText(hwndStatic, "You clicked the right mouse button!");
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_MOUSEMOVE:
//int x=Cursor.Position.X;
//int y=Cursor.Position.Y;
//This is where i need help
SetWindowText(hwndStatic, "Test" );
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hwnd, Msg, wParam, lParam);
}
BOOL InitializeWindow(HWND hwnd, HINSTANCE hInst, int CmdShow)
{ //windows basic handlers
WNDCLASS wc;
//The WNDCLASS struct wc is filled here by assigning all members of the struct
wc.lpfnWndProc = WinProc;
wc.hInstance = hInst;
wc.style = CS_BYTEALIGNCLIENT;
wc.lpszMenuName = NULL;
wc.lpszClassName = Simple;
wc.hIcon = LoadIcon(hInst, NULL);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wc.cbWndExtra = 0; wc.cbClsExtra = 0;
if (!RegisterClass(&wc))
MessageBox(hwnd, "Register class failed!", "Yay!", MB_OK);
//Create Window
hwnd = CreateWindow(Simple, Simple, WS_OVERLAPPEDWINDOW |WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, HWND_DESKTOP, NULL, hInst, NULL);
if (!hwnd){
MessageBox(hwnd, "It did not work!", "Yay!", MB_OK);
}
CreateWindowItems(hwnd, hInst);
ShowWindow(hwnd, CmdShow);
UpdateWindow(hwnd);
//All is good
return TRUE;
}
int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmd, int nShow)
{
HWND hwnd = NULL;
MSG Msg;
InitializeWindow(hwnd, hInst, nShow);
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return (int)Msg.wParam;
}