欢迎光临
我们一直在努力

C#实现查看文本框(如*号密码框)-.NET教程,C#语言

建站超值云服务器,限时71元/月

今天闲着无聊,试着用c#写查看密码框的程序(不仅限于密码框,应该是任何有文本的控件都可以)

代码见下面.

用c++实现起来超简单的这么个东东..用c#复杂得很,我又不想用unsafe,我总觉得用unsafe的话,干嘛不干脆用c++算了.

int length=300;

intptr thandle=apis.getlocalwindow();//取得当前鼠标所在位置的控件句柄

int address=apis.virtualallocex(process.getcurrentprocess().handle,0,length,0x1000,0x04);//在本进程内分配length大小的内存

apis.sendmessage(thandle,0x000d,new intptr(255),new intptr(address));//发送消息到目标控件,0x000d就是wm_gettext,255的意思是保存返回的值,new intptr(address)是指保存到address指定的地址

byte[] buf=new byte[length];

apis.readprocessmemory(process.getcurrentprocess().handle,address,buf,length,0);//读取刚才保存的内容

messagebox.show(encoding.default.getstring(buf));//显示出来测试一下.

其中apis开头的,是我自己写的api类库,相关声明如下:

[dllimport("user32.dll")]

public static extern intptr windowfrompoint(

point lppoint

);

[dllimport("user32.dll")]

public static extern int getcursorpos(

out point lppoint

);

public static intptr getlocalwindow()//这个只是把上两个结合了一下

{

point point;

getcursorpos(out point);

return windowfrompoint(point);

}

//不加ex的话.上面就不用第一个handle参数了

[ dllimport( "kernel32.dll" )]

public static extern system.int32 virtualallocex(

system.intptr hprocess,

system.int32 lpaddress,

system.int32 dwsize,

system.int16 flallocationtype,

system.int16 flprotect

);

[dllimport("user32.dll")]

public static extern intptr sendmessage(

intptr hwnd,

int msg,

intptr wparam,

intptr lparam

);

[ dllimport( "kernel32.dll" )]

public static extern int readprocessmemory(

system.intptr hprocess,

system.int32 lpbaseaddress,

byte[] lpbuffer,

long nsize,

long lpnumberofbyteswritten

);

*******************************************************************

因为一般要查看的是外部程序,配合快捷键不失不一种好方法.

c#的实现如下:

在初始化窗口的代码里加上

keymodifiers modifiers=keymodifiers.windows;//定义为win+快捷键,也可定义为别的.

registerhotkey(handle, 1001,modifiers,keys.v);//给win+v分配id为1001

override一下wndproc

protected override void wndproc( ref message m )

{

const int wm_hotkey = 0x0312;

switch(m.msg)

{

case wm_hotkey:

switch (m.wparam.toint32())

{

case 1001:

onhotkeyv();

break;

default:break;

}

break;

}

base.wndproc(ref m );

}

在onhotkeyv函数里写上我最开始写的那段代码就可以了.

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » C#实现查看文本框(如*号密码框)-.NET教程,C#语言
分享到: 更多 (0)