首頁 > web前端 > PS教程 > 主體

C#下實作空白窗體上中文輸入,可以實現類PS的文字工具

高洛峰
發布: 2017-02-15 10:23:44
原創
1685 人瀏覽過

C#下實現空白窗體上中文輸入

關鍵字:類PS的文字工具,中文輸入.重複截取中文

  

最近在研究做一個類PS的文字工具N多的資料,問了N多個人,總算功夫不負有人心.終於給弄出來了.寫出來給大家一起討論.

 

在空白窗體上打開輸入法,在C#裡的空白視窗是無論如何也是打不開輸入法.設定了this.ImeMode= ImeMode.NoControl也是無法開啟輸入法的錄影視窗.去Microsoft 開發論壇上問了一些問題.感謝週雪峰版主與Riquel_Dong 版主給的指點.用了API函數: ImmAssociateContext(IntPtr hWnd, IntPtr hIMC);終於把輸入法給調了出來,它的功能是把輸入與指定的視窗進行關聯.




:

f (m.Msg == WM_IME_SETCONTEXT && m.WParam.ToInt32() == 1)


ImmAssociateContext(this.Handle, m_hImc );

}


 呢.


當打開輸入法輸入文字時,會發送WM_IME_CHAR的消息.我們在這個消息下處理下就可以得到漢字了

: IntPtr hIMC, int dwIndex, StringBuilder  lpBuf, int dwBufLen);取得錄影視窗上輸入的漢字或NUICODE類別的字.當然,這裡就不研究別的了.只說漢字問題.取字的代碼如下:

case WM_IME_CHAR:


     int size = ImmGetCompositionString(m_hImc, GCS_COMPSTR, null, 0);


   izeof( Char);


                      


                        sb.Append(str.To                MessageBox.Show( str.ToString());


                   

                        isShowChina = true;


                  

       


                    break;

OK,.好像是大功告服務計劃」我的天啊,問題出在哪裡呢.


去查了一下MSDN.對WM_IME_CHAR有這樣的說明:


ble and the application passes this message to DefWindowProc


是不是問題就出在這裡了.是發送訊息兩次的問題.

,

方案:加上判斷

if (m.WParam.ToInt32() == PM_REMOVE)


測試.終於沒有問題了


 代碼貼文上


onentModel;

using System .Drawing;


using System.Data;

using System.Text;

using System.Windows.Forms;

using System.Runtime.InteropServices;Windows.Forms;

using System.Runtime.InteropServices; public partial class UserControl1 : UserControl

    {


        IntPtr m_hImc;


     bool isShowChina = false;

 


      private const int WM_IME_CHAR = 0x0286;


    

        private const int WM_IME_COMPOSITION = 0x010F;


           [DllImport (" Imm32.dll")]

        public static extern IntPtr ImmGetContext(IntPtr hWnd);


        [DllImport("Imm32.dll")]

        public static extern IntPtr ImmAssociateContext(IntPtr hndIM)Int     [DllImport(" imm32 .dll")]

        static extern int ImmGetCompositionString( IntPtr hIMC, int dwIndex, StringBuilder lpBuf, int dwBufLen);


        private int GRESCS   private const HC_ ACTION = 0;


       private const PM_REMOVE = 0x0001 ;



        StringBuilder sb = new StringBuilder();


 ,

       public UserControl1()

        {

InitializeComponent ( );

            


        }


       {

           m_hI mc = ImmGetContext(this.Handle);

       }

受保護訊息m)

        {

            base.WndProc(ref m);

if  == 1)

          {

                ImmAssociateContext(this.Handle, m_hImc ) ;


             

            }

    

                case WM_ CHAR:

                 對應   Key | ModifierKeys);

                    char a = (char)e.KeyData; //           sb.Append(a);

                    isShowChina = false;

                   break;

                      if (m.WParam.ToInt32() == PM_REMOVE ) //如果不做這個判斷,會印出重複的中文

                    {

                               int size = Imm GetCompositionString(m_hImc, GCS_COMPSTR, null, 0); sizeof (Char);

                        ImmGetCompositionString(m_hI      sb.Append(str.ToString());

                      MessageBox.Show  intoText();

                        isShowChina = true;

              

                    break;

              ///

        /// 列印文字

        ///

        /// intoText()//

        {

           Graphics g  = this.CreateGraphics();

  10);

         

        }

    }

}


更多C#下實現空白窗體上中文輸入,可以實現類PS的文字工具 相關文章請關注PHP中文網!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!