在python中发送自定义消息

.py

import win32api, win32con, win32gui 
import win32gui_struct 
import ctypes 
from ctypes import * 
GUID_DEVINTERFACE_USB_DEVICE = "{A5DCBF10-6530-11D2-901F-00C04FB951ED}" 
WM_RETICULATE_SPLINES =  (win32con.WM_USER + 0x0001)
class MyWindow: 
 
    def __init__(self): 
        win32gui.InitCommonControls() 
        self.hinst = win32api.GetModuleHandle(None) 
        className = 'MyWndClass' 
        message_map = { 
        win32con.WM_DESTROY: self.OnDestroy, 
        win32con.WM_DEVICECHANGE: self.OnDeviceChange, 
        win32con.WM_CREATE: self.OnCreate, 
        WM_RETICULATE_SPLINES: self.RETICULATE, 
        } 
        className = 'MyWndClass' 
        wc = win32gui.WNDCLASS() 
        wc.style = win32con.CS_HREDRAW | win32con.CS_VREDRAW 
        wc.lpfnWndProc = message_map 
        wc.lpszClassName = className 
        win32gui.RegisterClass(wc) 
        style = win32con.WS_OVERLAPPEDWINDOW 
        self.hwnd = win32gui.CreateWindow(className,'My win32api app',style,win32con.CW_USEDEFAULT,win32con.CW_USEDEFAULT,300,300,0,0,self.hinst,None) 
        # register for a device notification - we pass our service handle 
        # instead of a window handle. 
        filter = win32gui_struct.PackDEV_BROADCAST_DEVICEINTERFACE(GUID_DEVINTERFACE_USB_DEVICE) 
        self.hdn = win32gui.RegisterDeviceNotification(self.hwnd, filter, 4) 
        win32gui.ShowWindow(self.hwnd, win32con.SW_SHOW) 
        win32gui.UpdateWindow(self.hwnd)    
    def OnDestroy(self, hwnd, message, wparam, lparam): 
        win32gui.UnregisterDeviceNotification(self.hdn)    
        win32gui.PostQuitMessage(0) 
        return True 
    def OnDeviceChange(self, hwnd, message, wparam, lparam): 
        ctypes.windll.user32.MessageBoxW(0, "DeviceChange", "Message", 1) 
        return True 
    def OnCreate(self, hwnd, message, wparam, lparam): 
        ctypes.windll.user32.MessageBoxW(0, "Create", "Message", 1) 
        return True 
    def RETICULATE(self, hwnd, message, wparam, lparam): 
        x = lparam & 0xffff
        y = (lparam >> 16) & 0xffff
        print(x,y)
        return True 
    
 
w = MyWindow() 
lib = cdll.LoadLibrary(r'D:\repos\Dll_5\Debug\Dll_5.dll')
res = lib.SetHook()
win32gui.PumpMessages()
lib.UnHook()

.dll

// dllmain.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"

#include <windows.h>
#include <stdio.h>
#include <string.h>

#define WM_RETICULATE_SPLINES (WM_USER + 0x0001)

HHOOK tHook;
HMODULE hinstDLL;
static POINT pt;

LRESULT CALLBACK meconnect(int code, WPARAM wParam, LPARAM lParam) {
//    BOOL EnableMouseInPointer = TRUE;
    if (code == HC_ACTION) {
        LPMSG data = (LPMSG)lParam;
        if (data->message == WM_LBUTTONUP) {
            PostMessage(data->hwnd, WM_RETICULATE_SPLINES, data->wParam, data->lParam);

        }
    }
    return(CallNextHookEx(tHook, code, wParam, lParam));
}
extern "C" __declspec(dllexport) BOOL SetHook()
{
    tHook = SetWindowsHookEx(WH_GETMESSAGE, meconnect, hinstDLL, 0);

    if (tHook == NULL)
        return FALSE;
    else
        return TRUE;
}
extern "C" __declspec(dllexport) BOOL UnHook()
{
    return UnhookWindowsHookEx(tHook);
}


BOOL APIENTRY DllMain(HMODULE hModule,
    DWORD  ul_reason_for_call,
    LPVOID lpReserved
)
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
        hinstDLL = hModule;
        break;
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}

猜你喜欢

转载自www.cnblogs.com/strive-sun/p/12516206.html