| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1190 人关注过本帖, 1 人收藏
标题:向指定窗口发送键击消息
只看楼主 加入收藏
rain6867
该用户已被删除
收藏(1)
 问题点数:0 回复次数:2 
向指定窗口发送键击消息
提示: 作者被禁止或删除 内容自动屏蔽
搜索更多相关主题的帖子: 发送键 窗口 
2008-06-23 14:54
makebest
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:3
帖 子:658
专家分:962
注 册:2005-3-17
收藏
得分:0 
{****************************************************}
{              SendKeys Unit for Delphi 32           }
{    Copyright (c) 1999 by Borut Batagelj (Slovenia) }
{                       Aleksey Kuznetsov (Ukraine)  }
{            Home Page: www.             }
{            E-Mail: info@               }
{****************************************************}

unit SendKeys;

interface

uses
    Windows, SysUtils;

const
    SK_BKSP = #8;
    SK_TAB = #9;
    SK_ENTER = #13;
    SK_ESC = #27;
    SK_ADD = #107;
    SK_SUB = #109;
    SK_F1 = #228;
    SK_F2 = #229;
    SK_F3 = #230;
    SK_F4 = #231;
    SK_F5 = #232;
    SK_F6 = #233;
    SK_F7 = #234;
    SK_F8 = #235;
    SK_F9 = #236;
    SK_F10 = #237;
    SK_F11 = #238;
    SK_F12 = #239;
    SK_HOME = #240;
    SK_END = #241;
    SK_UP = #242;
    SK_DOWN = #243;
    SK_LEFT = #244;
    SK_RIGHT = #245;
    SK_PGUP = #246;
    SK_PGDN = #247;
    SK_INS = #248;
    SK_DEL = #249;
    SK_SHIFT_DN = #250;
    SK_SHIFT_UP = #251;
    SK_CTRL_DN = #252;
    SK_CTRL_UP = #253;
    SK_ALT_DN = #254;
    SK_ALT_UP = #255;

procedure SendKeyString(Text: String);
procedure SendKeysToTitle(WindowTitle: String; Text: String);
procedure SendKeysToHandle(WindowHandle: hWnd; Text: String);
procedure MakeWindowActive(wHandle: hWnd);
function GetHandleFromWindowTitle(TitleText: String): hWnd;

implementation

procedure SendKeyString(Text: String);
var
    i: Integer;
    Shift: Boolean;
    vk, ScanCode: Word;
    ch: Char;
    c, s: Byte;
const
    vk_keys: Array[0..9] of Byte =
        (VK_HOME, VK_END, VK_UP, VK_DOWN, VK_LEFT,
        VK_RIGHT, VK_PRIOR, VK_NEXT, VK_INSERT, VK_DELETE);
    vk_shft: Array[0..2] of Byte = (VK_SHIFT, VK_CONTROL, VK_MENU);
    flags: Array[False..True] of Integer = (KEYEVENTF_KEYUP, 0);
begin
    Shift := False;
    for i := 1 to Length(Text) do
    begin
        ch := Text[i];
        if ch >= #250 then
        begin
            s := Ord(ch) - 250;
            Shift := not Odd(s);
            c := vk_shft[s shr 1];
            ScanCode := MapVirtualKey(c,0);
            Keybd_Event(c, Scancode, Flags[shift], 0);
        end
        else
        begin
            vk := 0;
            if ch >= #240 then
                c := vk_keys[Ord(ch) - 240]
            else
                if ch >= #228 then {228 (F1) => $70 (vk_F1)}
                    c := Ord(ch) - 116
                else
                    if ch < #110 then
                        c := Ord(ch)
                    else
                    begin
                        vk := VkKeyScan(ch);
                        c := LoByte(vk);
                    end;
            ScanCode := MapVirtualKey(c,0);
            if not Shift and (Hi(vk) > 0) then { $2A = scancode of VK_SHIFT }
                Keybd_Event(VK_SHIFT, $2A, 0, 0);
            Keybd_Event(c,scancode, 0, 0);
            Keybd_Event(c,scancode, KEYEVENTF_KEYUP, 0);
            if not Shift and (Hi(vk) > 0) then
                Keybd_Event(VK_SHIFT, $2A, KEYEVENTF_KEYUP, 0);
        end;
    end;
end;

procedure MakeWindowActive(wHandle: hWnd);
begin
    if IsIconic(wHandle) then
        ShowWindow(wHandle, SW_RESTORE)
    else
        BringWindowToTop(wHandle);
end;

function GetHandleFromWindowTitle(TitleText: String): hWnd;
var
    StrBuf: Array[0..$FF] of Char;
begin
    Result := FindWindow(PChar(0), StrPCopy(StrBuf, TitleText));
end;

procedure SendKeysToTitle(WindowTitle: String; Text: String);
var
    Window: hWnd;
begin
    Window := GetHandleFromWindowTitle(WindowTitle);
    MakeWindowActive(Window);
    SendKeyString(Text);
end;

procedure SendKeysToHandle(WindowHandle: hWnd; Text: String);
begin
    MakeWindowActive(WindowHandle);
    SendKeyString(Text);
end;

end.
2008-06-23 16:41
xu2000
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:51
帖 子:3890
专家分:56
注 册:2006-4-8
收藏
得分:0 
好漂亮的代码,论坛中真是有高人。

我会拿出我全部的钱财,以保你衣食无忧。我会献出我所有的智慧,以助你一帆风顺。我会想到所有的笑语,以令你展眉开颜。我会挤出最长的时间,以使你终生幸福。        [本人原创的结婚宣言]
2008-06-23 16:54
快速回复:向指定窗口发送键击消息
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020665 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved