将DWORD事件常量从WinEventProc转换为c#中的名称

本文关键字:转换 事件 DWORD 常量 WinEventProc | 更新日期: 2023-09-27 18:08:32

我使用SetWinEventHookWinEventProc捕获窗口消息

在WinEventProc中有一个参数"DWORD event",带有一个数字来表示事件

我想把这个数字转换成一个名字

是否有一个c#函数来做到这一点?

我的代码是基于这里的答案在Windows消息上设置Hook

将DWORD事件常量从WinEventProc转换为c#中的名称

public enum SystemEventContants : uint
{
    EVENT_SYSTEM_SOUND = 0x1,
    EVENT_SYSTEM_ALERT = 0x2,
    EVENT_SYSTEM_FOREGROUND = 0x3,
    EVENT_SYSTEM_MENUSTART = 0x4,
    EVENT_SYSTEM_MENUEND = 0x5,
    EVENT_SYSTEM_MENUPOPUPSTART = 0x6,
    EVENT_SYSTEM_MENUPOPUPEND = 0x7,
    EVENT_SYSTEM_CAPTURESTART = 0x8,
    EVENT_SYSTEM_CAPTUREEND = 0x9,
    EVENT_SYSTEM_MOVESIZESTART = 0xa,
    EVENT_SYSTEM_MOVESIZEEND = 0xb,
    EVENT_SYSTEM_CONTEXTHELPSTART = 0xc,
    EVENT_SYSTEM_CONTEXTHELPEND = 0xd,
    EVENT_SYSTEM_DRAGDROPSTART = 0xe,
    EVENT_SYSTEM_DRAGDROPEND = 0xf,
    EVENT_SYSTEM_DIALOGSTART = 0x10,
    EVENT_SYSTEM_DIALOGEND = 0x11,
    EVENT_SYSTEM_SCROLLINGSTART = 0x12,
    EVENT_SYSTEM_SCROLLINGEND = 0x13,
    EVENT_SYSTEM_SWITCHSTART = 0x14,
    EVENT_SYSTEM_SWITCHEND = 0x15,
    EVENT_SYSTEM_MINIMIZESTART = 0x16,
    EVENT_SYSTEM_MINIMIZEEND = 0x17
}
public enum ObjectEventContants : uint
{
    EVENT_OBJECT_CREATE = 0x8000,
    EVENT_OBJECT_DESTROY = 0x8001,
    EVENT_OBJECT_SHOW = 0x8002, 
    EVENT_OBJECT_HIDE = 0x8003,  
    EVENT_OBJECT_REORDER = 0x8004,
    EVENT_OBJECT_FOCUS = 0x8005,
    EVENT_OBJECT_SELECTION = 0x8006,
    EVENT_OBJECT_SELECTIONADD = 0x8007,
    EVENT_OBJECT_SELECTIONREMOVE = 0x8008,
    EVENT_OBJECT_SELECTIONWITHIN = 0x8009,
    EVENT_OBJECT_STATECHANGE = 0x800A,
    EVENT_OBJECT_LOCATIONCHANGE = 0x800B,
    EVENT_OBJECT_NAMECHANGE = 0x800C,
    EVENT_OBJECT_DESCRIPTIONCHANGE = 0x800D,  
    EVENT_OBJECT_VALUECHANGE = 0x800E,
    EVENT_OBJECT_PARENTCHANGE= 0x800F,  
    EVENT_OBJECT_HELPCHANGE= 0x8010,
    EVENT_OBJECT_DEFACTIONCHANGE = 0x8011, 
    EVENT_OBJECT_ACCELERATORCHANGE = 0x8012
}
// possible marshaling unmanaged type conflict/problem between 32/64 bit
public enum SystemObjectIDs 
{
    OBJID_WINDOW = 0x00000000,
    OBJID_SYSMENU = 0xFFFFFFFF,
    OBJID_TITLEBAR = 0xFFFFFFFE,
    OBJID_MENU = 0xFFFFFFFD,
    OBJID_CLIENT = 0xFFFFFFFC,
    OBJID_VSCROLL = 0xFFFFFFFB,
    OBJID_HSCROLL = 0xFFFFFFFA,
    OBJID_SIZEGRIP = 0xFFFFFFF9,
    OBJID_CARET = 0xFFFFFFF8,
    OBJID_CURSOR = 0xFFFFFFF7,
    OBJID_ALERT = 0xFFFFFFF6,
    OBJID_SOUND = 0xFFFFFFF5,
    OBJID_QUERYCLASSNAMEIDX = 0xFFFFFFF4,
    OBJID_NATIVEOM = 0xFFFFFFF0
}
public enum Flags
{
    WINEVENT_OUTOFCONTEXT = 0x0000,
    WINEVENT_SKIPOWNTHREAD = 0x0001,
    WINEVENT_SKIPOWNPROCESS = 0x0002,
    WINEVENT_INCONTEXT = 0x0004
}
来源:winuser.h

这可能有点痛苦,但是您可以尝试创建一个T4模板来挖掘平台sdk C头文件,解析值,并为您创建枚举。如果您这样做,那么如果您获得具有新值的平台SDK更新,它们将自动包含到您的enum中(假设您再次生成T4输出)。