zoukankan      html  css  js  c++  java
  • Delphi 拦截输入法输入结果

    {
      拦截输入法输入的字符串。向编辑框中输入中文查看效果。
      Delphi XE7
    }
    
    unit Unit1;
    
    interface
    
    uses
        Winapi.Windows,
        Winapi.Messages,
        System.SysUtils,
        System.Variants,
        System.Classes,
        Vcl.Graphics,
        Vcl.Controls,
        Vcl.Forms,
        Vcl.Dialogs,
        Vcl.StdCtrls,
        Winapi.Imm;
    
    type
        TForm1 = class( TForm )
            edt1 : TEdit;
            procedure FormCreate( Sender : TObject );
            private
                { Private declarations }
            public
                { Public declarations }
        end;
    
    var
        Form1       : TForm1;
        DestHwnd    : HWND;
        DestWinPorc : Pointer;
    
    implementation
    
    {$R *.dfm}
    
    procedure OnWM_IME_COMPOSITION( HWND, msg, wParam, lParam : longint );
    var
    
        ResultStr : string;
        hIMC      : Integer;
        dwSize    : Integer;
        StrLength : Integer;
    begin
        if ( lParam and GCS_RESULTSTR ) <> 0 then
            begin
                // 先获取当前正在输入的窗口的输入法句柄
                hIMC := ImmGetContext( Form1.edt1.Handle );
                // 先将ImmGetCompositionString的获取长度设为0来获取字符串大小.
                dwSize := ImmGetCompositionString(
                    hIMC,
                    GCS_RESULTSTR,
                    nil,
                    0 );
    
                // 缓冲区大小要加上字符串的NULL结束符大小,
                // 考虑到UNICODE
                StrLength := dwSize div Integer( sizeof( Char ) );
                OutputDebugString( PWideChar( IntToStr( Length( ResultStr ) ) ) );
                SetLength(
                    ResultStr,
                    StrLength );
                OutputDebugString( PWideChar( ResultStr ) );
                // 再调用一次.ImmGetCompositionString获取字符串
                ImmGetCompositionString(
                    hIMC,
                    GCS_RESULTSTR,
                    PChar( ResultStr ),
                    dwSize );
                // 现在ResultStr里面即是输入的汉字了。
                OutputDebugString( '-------------------' );
                OutputDebugString( PWideChar( ResultStr ) );
                OutputDebugString( '-------------------' );
                ImmReleaseContext(
                    HWND,
                    hIMC );
            end;
    
    end;
    
    function WinProc( HWND, msg, wParam, lParam : longint ) : LRESULT; stdcall;
    begin
        case msg of
            WM_IME_COMPOSITION :
                OnWM_IME_COMPOSITION( HWND, msg, wParam, lParam );
        end;
        result := CallWindowProc(
            DestWinPorc,
            HWND,
            msg,
            wParam,
            lParam );
    end;
    
    procedure TForm1.FormCreate( Sender : TObject );
    begin
        DestHwnd    := Self.edt1.Handle;
        DestWinPorc := Pointer( GetWindowLong( Self.edt1.Handle, GWL_WNDPROC ) );
        SetWindowLong(
            DestHwnd,
            GWL_WNDPROC,
            longint( @WinProc ) );
    end;
    
    end.
    致读者:本人自学编程,知识薄弱,实践经验不够,博客文章难免有错误之处,希望读者能积极指正,感激不尽。 若您有更精妙的解决方案或者对文中有疑问,欢迎留言或联系我讨论问题。
  • 相关阅读:
    atitit.TokenService v3 qb1 token服务模块的设计 新特性.docx
    Atitit attilax在自然语言处理领域的成果
    Atitit 图像清晰度 模糊度 检测 识别 评价算法 原理
    Atitit (Sketch Filter)素描滤镜的实现  图像处理  attilax总结
    atitit。企业的价值观 员工第一 vs 客户第一.docx
    Atitit 实现java的linq 以及与stream api的比较
    Atitit dsl exer v3 qb3 新特性
    Atititi tesseract使用总结
    Atitit 修改密码的功能流程设计 attilax总结
    atitit.TokenService v3 qb1  token服务模块的设计 新特性.docx
  • 原文地址:https://www.cnblogs.com/it89/p/10128320.html
Copyright © 2011-2022 走看看