Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,367 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,637 people online right now. Registration is fast and FREE... Join Now!




Printing x and y coordinates in window

 
Reply to this topicStart new topic

Printing x and y coordinates in window

Viviaci
3 Sep, 2008 - 12:08 PM
Post #1

New D.I.C Head
*

Joined: 25 Aug, 2008
Posts: 9


My Contributions
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;  
}  

User is offlineProfile CardPM
+Quote Post

Viviaci
RE: Printing X And Y Coordinates In Window
4 Sep, 2008 - 12:55 PM
Post #2

New D.I.C Head
*

Joined: 25 Aug, 2008
Posts: 9


My Contributions
figured it out myself after much headache. This actually tracks the coordinates of the mouse within the window... Just Replace the corresponding code block above with the code block below:

CODE
LRESULT CALLBACK WinProc(HWND hwnd, UINT Msg, WPARAM wParam, LPARAM lParam)  
{  
  char buffer[80];
  HDC hdc = GetDC(hwndStatic);

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 mouse_x = (int)LOWORD(lParam);
        int mouse_y = (int)HIWORD(lParam);
        //int ypos = HIWORD(lparam);
        //hdc = GetDC(hwnd);
        sprintf_s(buffer, "WM_MOUSEMOVE CALLED - CURRENT POSITION = ( %d,%d )", mouse_x, mouse_y);
        TextOut(hdc, 0,50, buffer, strlen(buffer));
    //This is where i need help    
        //SetWindowText(hwndStatic, "(%d,%d)");
                      return(0);
                      }break;
    case WM_DESTROY:  
        PostQuitMessage(0);  
        break;    
  }  
  return DefWindowProc(hwnd, Msg, wParam, lParam);  
}  

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 09:36AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month