定时式获取鼠标处xy坐标和窗口对象(winapi)

来源:互联网 发布:网易班车路线 知乎 编辑:程序博客网 时间:2024/06/05 06:33

看了一下.hook比较麻烦.反正弄个小东西,就使用了这个方式;

 

定时式获取鼠标处xy坐标和窗口对象(winapi) - qidizi - qidizi 的博客

 

#include <windows.h>

/*
 * ini解析模块
 * 下载地址:http://ndevilla.free.fr/iniparser/
 * 使用方式:解压后把src中的所有文件复制到本工程中的iniparser目录中
 * 接着把里面的c后缀文件添加到工程中,否则会出现链接不了的问题
 */


        
    
#define ID_TEXT     2
#define INI_NAME "keys.ini"
#define IDT_TIME    10

/*
 * 函数声明部分
 */

void create_default_ini_file(char *ini_name);
void  parse_ini_file(char * ini_name);

/*
 * 声明结束
 */
 
 
/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/*  Make the class name into a global variable  */
char szClassName[ ] = "通用全局快捷键发送程序";
dictionary    *    ini ;

int WINAPI WinMain (HINSTANCE hThisInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszArgument,
                    int nFunsterStil)

{
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default color as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           "通用全局快捷键发送程序",       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           544,                 /* The programs width */
           375,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nFunsterStil);
    UpdateWindow (hwnd) ;
    
    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    
  
    return messages.wParam;
}

/* 消息处理 */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{        
                 
                
            HDC                   hdc ;
            PAINTSTRUCT           ps ;
            POINT mmPoint;
            char wTitle[256], gTitle[120];
            HWND mHwnd;


           switch (message) {        
               case   WM_CREATE :        
                      
                    
                      SetTimer (hwnd, IDT_TIME, 10, NULL);//运行就设置定时刷新
                      return 0 ;  
                case WM_TIMER:
                    GetCursorPos (&mmPoint);//获取坐标
                    mHwnd = WindowFromPoint (mmPoint);//由坐标得够本
                    int gType = GetWindowText (mHwnd, gTitle, 100);//够本得窗口标题
                    wsprintf (wTitle, TEXT ("X : %d, Y : %d, mHnd:%d, gType: %d, wtitle: %s "), mmPoint.x, mmPoint.y, mHwnd, gType, gTitle);
                    SetWindowText (hwnd, wTitle);//在程序窗口显示标题
                    return 0;
                case WM_DESTROY:
                    PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
                    KillTimer (hwnd, IDT_TIME);//取消定时
                    break;
                default:                      /* for messages that we don't deal with */
                    return DefWindowProc (hwnd, message, wParam, lParam);    
        }
}



原创粉丝点击