win32编程 - 鼠标消息

本文详细介绍了如何在Windows编程中捕获和处理鼠标事件(如滚轮、左键双击、移动和点击),以及创建一个简单的窗口并使用窗口消息循环。
摘要由CSDN通过智能技术生成
#include <windows.h>
#include <stdio.h>
HANDLE g_hOutput = 0;

void OnMouseWheel(HWND hWnd, WPARAM wParam, LPARAM lParam)
{
    char pszText[256] = { 0 };
    sprintf_s(pszText, 256, "WM_MOUSEWHEEL: 其他按键状态=%d,  滚轮偏移量=%d, X=%d, Y=%d \n", LOWORD(wParam), HIWORD(wParam), LOWORD(lParam), HIWORD(lParam));
    WriteConsole(g_hOutput, pszText, strlen(pszText), NULL, NULL);
}


void OnLButtonDblClk(HWND hWnd, WPARAM wParam, LPARAM lParam)
{
    char pszText[256] = { 0 };
    sprintf_s(pszText, 256, "WM_LBUTTONDBLCLK: 状态=%d,X=%d,Y=%d \n", wParam, LOWORD(lParam), HIWORD(lParam));
    WriteConsole(g_hOutput, pszText, strlen(pszText), NULL, NULL);
}

void OnMouseMove(HWND hWnd, WPARAM wParam, LPARAM lParam)
{
    char pszText[256] = { 0 };
    sprintf_s(pszText, 256, "WM_MOUSEMOVE: 状态=%d,X=%d,Y=%d \n", wParam, LOWORD(lParam), HIWORD(lParam));
    //WriteConsole(g_hOutput, pszText, strlen(pszText), NULL, NULL);
}

void OnLButtonDowm(HWND hWnd, WPARAM wParam, LPARAM lParam)
{
    char pszText[256] = {0};
    sprintf_s(pszText, 256,"WM_LBUTTONDOWN: 状态=%d,X=%d,Y=%d \n", wParam,LOWORD(lParam),HIWORD(lParam));
    WriteConsole(g_hOutput, pszText, strlen(pszText), NULL, NULL);
}

void OnLButtonUp(HWND hWnd, WPARAM wParam, LPARAM lParam)
{
    char pszText[256] = { 0 };
    sprintf_s(pszText, 256, "WM_LBUTTONUP: 状态=%d,X=%d,Y=%d \n", wParam, LOWORD(lParam), HIWORD(lParam));
    WriteConsole(g_hOutput, pszText, strlen(pszText), NULL, NULL);
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT msgID, WPARAM wParam, LPARAM lParam)
{
    switch (msgID)
    {
    case WM_MOUSEWHEEL://滚轮消息
        OnMouseWheel(hWnd, wParam, lParam);
        break;
    case WM_LBUTTONDBLCLK://左键双击
        OnLButtonDblClk(hWnd, wParam, lParam);
        break;
    case WM_MOUSEMOVE://鼠标移动
        OnMouseMove(hWnd, wParam, lParam);
        break;
    case WM_LBUTTONDOWN://左键按下
        OnLButtonDowm(hWnd, wParam, lParam);
        break;
    case WM_LBUTTONUP://左键弹起
        OnLButtonUp(hWnd, wParam, lParam);
        break;

    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    }
    return DefWindowProc(hWnd, msgID, wParam, lParam);
}

//入口函数
int CALLBACK WinMain(HINSTANCE hIns, HINSTANCE hPreIns, LPSTR IpCmdLine, int nCmdShow)
{
    AllocConsole();
    g_hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
    //注册窗口类
    WNDCLASS wc = { 0 };
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wc.hCursor = NULL;
    wc.hIcon = NULL;
    wc.hInstance = hIns;
    wc.lpfnWndProc = WndProc;

    wc.lpszClassName = "Main";
    wc.lpszMenuName = NULL;
    wc.style = CS_HREDRAW | CS_VREDRAW |CS_DBLCLKS;
    RegisterClass(&wc);

    //在内存创建窗口
    HWND hWnd = CreateWindowEx(0, "Main", "window", WS_OVERLAPPEDWINDOW, 100, 100, 500, 500, NULL, NULL, hIns, NULL);


    //显示窗口
    ShowWindow(hWnd, SW_SHOW);
    UpdateWindow(hWnd);

    //消息循环
    MSG nMsg = { 0 };
    while (GetMessage(&nMsg, NULL, 0, 0))
    {
        TranslateMessage(&nMsg);
        DispatchMessage(&nMsg);//将消息给窗口处理函数来处理
    }
    return 0;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值