簡體   English   中英

鼠標坐標在Visual C ++ Win32應用程序中?

[英]mouse coordinates in visual-c++ win32 application?

我已經在visual-c ++中創建了一個win32應用程序,但是此程序無法打印鼠標坐標,所有其他事件均能正常工作,誰能告訴我如何在visual-c ++ win32應用程序中獲取鼠標坐標?

希望能迅速而積極地回應。

// ttt.cpp : Defines the entry point for the application.
//  TO Demonstrate the Mouse Events

#include "windows.h"
#include "stdafx.h"
#include "stdio.h"


LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    int x,y;
    LPCWSTR msgdown = (LPCWSTR)L"Left Mouse Button Down" ;
    LPCWSTR msgup = (LPCWSTR)L"Left Mouse Button UP" ;
    LPCWSTR msgdblclk = (LPCWSTR)L"Left Mouse Button Dbl clk" ;

    LPCWSTR rmsgdown = (LPCWSTR)L"Right Mouse Button Down" ;
    LPCWSTR rmsgup = (LPCWSTR)L"Right Mouse Button UP" ;
    LPCWSTR rmsgdblclk = (LPCWSTR)L"Right Mouse Button Dbl clk" ;

    LPCWSTR rwheel = (LPCWSTR)L"Mousescroll" ;
    //LPCWSTR txtmsg = (LPCWSTR)L"position" ;
    LPCWSTR mouse = (LPCWSTR)L"Mouse" ;
    switch (msg)
    {
        case WM_CLOSE:
        DestroyWindow(hWnd);
        break;

        case WM_DESTROY:
        PostQuitMessage(0);
        break;

        case WM_LBUTTONDOWN:
        MessageBox(hWnd,msgdown,mouse,MB_OK);

        break;

        case WM_LBUTTONUP:
            MessageBox(hWnd,msgup,mouse,MB_OK);
        break;

        case WM_LBUTTONDBLCLK:
        MessageBox(hWnd,msgdblclk,mouse,MB_OK);
        break;      

        case WM_RBUTTONUP:
        MessageBox(hWnd,rmsgup,mouse,MB_OK);
        break;

        case WM_RBUTTONDOWN:
        MessageBox(hWnd,rmsgdown,mouse,MB_OK);
        break;

        case WM_RBUTTONDBLCLK:
        MessageBox(hWnd,rmsgdblclk,mouse,MB_OK);
        break;

        case WM_MOUSEWHEEL:
        MessageBox(hWnd,rwheel,mouse,MB_OK);
        break;


        char text[50];
        POINT p;
        sprintf(text,"Mouse Position: X=%d, Y=%d",p.x,p.y);
        LPCWSTR textmsg = (LPCWSTR)text;
        SetWindowText(hWnd,textmsg);
        break;


        /*POINT pt;
        GetCursorPos(&pt);

        int a = (int)pt.x;
        int b = (int)pt.y;*/
    }
        return DefWindowProc(hWnd, msg, wParam, lParam);
}

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
    LPCTSTR className=(LPCTSTR)"Mouse Test";
    WNDCLASSEX wc;

    wc.cbSize =sizeof(WNDCLASSEX);
    wc.style =CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
    wc.lpfnWndProc =WndProc;
    wc.cbClsExtra =0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon(NULL,IDI_WINLOGO);
    wc.hCursor = LoadCursor(NULL,IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW +1);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = className;
    wc.hIconSm = LoadIcon(NULL,IDI_WINLOGO);

    MessageBoxA(NULL,"mouse events","mouse",MB_OK);



    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL,(LPCWSTR)"Error Registering Class",(LPCWSTR)"Error RegisterClassEx",MB_OK | MB_ICONERROR);
        return 1;
    }

    HWND hwmd = CreateWindowEx(0,className,(LPCWSTR)L"Mouse Test",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,400,300,NULL,NULL,hInstance,NULL);
    ShowWindow(hwmd,SW_SHOWDEFAULT);
    UpdateWindow(hwmd);


    if(!hwmd)
    {
        MessageBox(NULL,(LPCWSTR)"Error Creating Window",(LPCWSTR)"Error CreateWindowEx",MB_OK | MB_ICONERROR);
        return 1;
    }

        MSG msg;

    while(GetMessage(&msg,NULL,0,0)>0)
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return (int)msg.wParam;
}

正如我在評論中提到的那樣,即使您將其變為可訪問的,以下代碼塊1)也將永遠無法到達,而2)將無法工作:

case WM_MOUSEWHEEL:
        MessageBox(hWnd,rwheel,mouse,MB_OK);
        break;


        char text[50]; // no case to get you here!
        POINT p;
        sprintf(text,"Mouse Position: X=%d, Y=%d",p.x,p.y);
        LPCWSTR textmsg = (LPCWSTR)text; // will not work!
        SetWindowText(hWnd,textmsg);
        break;

特別是在WM_MOUSEWHEEL消息中,游標坐標在lParam中傳遞。 LOWORD(lParam)將為x, HIWORD(lParam)將為y。 坐標是相對於屏幕的,而不是相對於窗口的。 使用ScreenToClient()進行轉換。

WM_xBUTTONDOWN / UP和WM_MOUSEMOVE中的lParam的含義相同,但是坐標是相對於窗口的工作區而言的。

使用GetCursorInfo()可以在任何時間獲取鼠標的位置。 如果只想在鼠標實際移動時進行跟蹤,請處理WM_MOUSEMOVE

有關更多信息, 請參見前面的問題/答案。

正如其他人所說,您將需要解決字符串的Unicode / char *問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM