捕捉消息框中的文字

把桌面上弹出的消息框中的文字取出来

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.Xml;
using System.Runtime.InteropServices;
namespace WindowsApplication26
... {
public partial class Form1:Form
... {

public class HookMsg
... {

Win32APIfunctions #region Win32APIfunctions
private const int WH_CBT = 0x5 ;
private const int IDC_OK = 0x1 ;
private const int IDC_Text = 0xFFFF ;


[DllImport(
" user32.dll " )]
protected static extern IntPtrSetWindowsHookEx( int code,HookProcfunc,IntPtrhInstance, int threadID);
[DllImport(
" user32.dll " )]
protected static extern int UnhookWindowsHookEx(IntPtrhhook);
[DllImport(
" user32.dll " )]
protected static extern int CallNextHookEx(IntPtrhhook, int code,IntPtrwParam,IntPtrlParam);


[DllImport(
" user32.dll " )]
protected static extern int GetWindowText(IntPtrhwnd,[Out]StringBuilderlpString, int nMaxCount);

[DllImport(
" user32.dll " )]
protected static extern int GetClassName(IntPtrhwnd,[Out]StringBuilderlpString, int nMaxCount);

[DllImport(
" user32.dll " )]
protected static extern int SetWindowText(IntPtrhWnd, string lpString);

[DllImport(
" user32.dll " )]
protected static extern IntPtrGetDlgItem(IntPtrhwnd, int id);
[DllImport(
" User32 " )]
protected static extern int SetDlgItemText(IntPtrhDlg, int nIDDlgItem, string lpString);
[DllImport(
" user32.dll " )]
protected static extern int GetDlgItemText(IntPtrhDlg, int nIDDlgItem,[Out]StringBuilderlpString, int nMaxCount);

[DllImport(
" user32.dll " ,EntryPoint = " MessageBox " )]
protected static extern int _MessageBox(IntPtrhwnd, string text, string caption,
int options);

[DllImport(
" user32.dll " )]
protected static extern IntPtrGetActiveWindow();

[DllImport(
" user32.dll " )]
protected static extern void DestroyWindow(IntPtrhwnd);

#endregion


private static System.IntPtrm_hhook;

public delegate int HookProc( int code,IntPtrwParam,IntPtrlParam);

protected static HookProcm_filterFunc;

static HookMsg()
... {
if (m_filterFunc == null )
m_filterFunc
= new HookProc(CoreHookProc);
}


public static void Install()
... {

m_hhook
= SetWindowsHookEx(WH_CBT,m_filterFunc,IntPtr.Zero,AppDomain.GetCurrentThreadId());
}


public static void Uninstall()
... {
UnhookWindowsHookEx(m_hhook);
}


// CallBack
protected static int CoreHookProc( int code,IntPtrwParam,IntPtrlParam)
... {

if (code == 5 )
... {

StringBuildersb
= new StringBuilder();
sb.Capacity
= 255 ;
// Title
GetWindowText(wParam,sb, 255 );
string strTitle = " jinjazz看到了: " + sb.ToString();

// Text
GetDlgItemText(wParam,IDC_Text,sb, 255 );
string strText = " jinjazz看到了: " + sb.ToString();

// 获取按钮
int style = 0 ;
for ( int i = 0 ;i <= 5 ;i ++ )
... {
if (GetDlgItem(wParam,i) != IntPtr.Zero)
style
+= i;
}


SetDlgItemText(wParam,IDC_Text,strText);
SetWindowText(wParam,strTitle);
SetWindowText(GetDlgItem(wParam,(
int )DialogResult.No), " jinjazzOK " );
SetWindowText(GetDlgItem(wParam,(
int )DialogResult.Cancel), " jinjazzCancel " );
SetWindowText(GetDlgItem(wParam,(
int )DialogResult.Abort), " jinjazzAbort " );
SetWindowText(GetDlgItem(wParam,(
int )DialogResult.Ignore), " jinjazzIgnore " );
SetWindowText(GetDlgItem(wParam,(
int )DialogResult.None), " jinjazzNone " );
SetWindowText(GetDlgItem(wParam,(
int )DialogResult.OK), " jinjazzOK " );
SetWindowText(GetDlgItem(wParam,(
int )DialogResult.Retry), " jinjazzRetry " );
SetWindowText(GetDlgItem(wParam,(
int )DialogResult.Yes), " jinjazzYes " );
}


// returnCallNextHookEx(this.m_hhook,code,wParam,lParam);
return 0 ;
}

}


public Form1()
... {
InitializeComponent();
}


private void Form1_Load( object sender,EventArgse)
... {

}


private void button1_Click( object sender,EventArgse)
... {
MessageBox.Show(
this , " 确定按钮 " , " 标题 " );
}


private void button2_Click( object sender,EventArgse)
... {
MessageBox.Show(
this , " 确定按钮 " , " 标题 " ,MessageBoxButtons.YesNoCancel);
}


private void button3_Click( object sender,EventArgse)
... {
MessageBox.Show(
this , " 确定按钮 " , " 标题 " ,MessageBoxButtons.AbortRetryIgnore);
}


private void checkBox1_CheckedChanged( object sender,EventArgse)
... {
if ( this .checkBox1.Checked)
... {
HookMsg.Install();
}

else
... {
HookMsg.Uninstall();
}

}

}

}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值