找回密码
 加入
搜索
查看: 1441|回复: 11

_GUICtrlEdit_ReplaceSel不支持中文

[复制链接]
发表于 2009-5-29 08:48:05 | 显示全部楼层 |阅读模式
本帖最后由 sd007 于 2009-5-29 10:55 编辑

_GUICtrlEdit_ReplaceSel不支持中文,请问怎么解决?
#AutoIt3Wrapper_au3check_parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#include <GuiEdit.au3>
#include <GuiStatusBar.au3>
#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>

Opt('MustDeclareVars', 1)

$Debug_Ed = False ; Check ClassName being passed to Edit functions, set to True and use a handle to another control to see it work

_Main()

Func _Main()
    Local $StatusBar, $hEdit, $hGUI
    Local $sFile = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\AutoIt v3\AutoIt", "InstallDir") & "\include\changelog.txt"
    Local $aPartRightSide[3] = [200, 378, -1]
    
    ; Create GUI
    $hGUI = GUICreate("Edit Replace Sel", 400, 300)
    $hEdit = GUICtrlCreateEdit("", 2, 2, 394, 268, BitOR($ES_WANTRETURN, $WS_VSCROLL))
    $StatusBar = _GUICtrlStatusBar_Create($hGUI, $aPartRightSide)
    _GUICtrlStatusBar_SetIcon($StatusBar, 2, 97, "shell32.dll")
    GUISetState()

    ; Set Margins
    _GUICtrlEdit_SetMargins($hEdit, BitOR($EC_LEFTMARGIN, $EC_RIGHTMARGIN), 10, 10)

    ; Set Text
    _GUICtrlEdit_SetText($hEdit, FileRead($sFile))
    
    ; Set Sel
    _GUICtrlEdit_SetSel($hEdit, 0, 8)

    MsgBox(4160, "Information", "Replace Sel")
    _GUICtrlEdit_ReplaceSel($hEdit, "中国")

    ; Loop until user exits
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
    GUIDelete()
EndFunc   ;==>_Main
发表于 2009-5-29 09:03:23 | 显示全部楼层
"中国" 改为"中国 "
 楼主| 发表于 2009-5-29 09:46:32 | 显示全部楼层
2个汉字用这种办法是可以的,但如果更多汉字或者汉字+英文就不好用了
我试着修改_GUICtrlEdit_ReplaceSel函数,但不成功,还是乱码
Func _GUICtrlEdit_ReplaceSel($hWnd, $sText, $fUndo = True)
        If $Debug_Ed Then _GUICtrlEdit_ValidateClassName($hWnd)
        If Not IsHWnd($hWnd) Then $hWnd = GUICtrlGetHandle($hWnd)

        Local $struct_MemMap
        If StringIsASCII($sText) Then
                Local $struct_String = DllStructCreate("char Text[" & StringLen($sText) + 1 & "]")
        Else
            Local $struct_String = DllStructCreate("wchar Text[" & StringLen($sText) * 2 & "]")
        EndIf
       
        Local $sBuffer_pointer = DllStructGetPtr($struct_String)
        DllStructSetData($struct_String, "Text", $sText)
        ;MsgBox(0,"",DllStructGetData($struct_String, "Text"));这里能取到正确的中文
        If StringIsASCII($sText) Then
                _MemInit($hWnd, StringLen($sText) + 1, $struct_MemMap)
        Else
                _MemInit($hWnd, StringLen($sText) * 2, $struct_MemMap)
        EndIf
        _MemWrite($struct_MemMap, $sBuffer_pointer)
        _SendMessage($hWnd, $EM_REPLACESEL, $fUndo, $sBuffer_pointer, true, "wparam", "ptr")
        _MemFree($struct_MemMap)
EndFunc   ;==>_GUICtrlEdit_ReplaceSel
发表于 2009-5-29 10:10:11 | 显示全部楼层
可以用        SendX("中华人民共和国")
Func SendX($string)
        Local $clup
        $clup = ClipGet()
        ClipPut($string)
        Send("+{ins}")
        ClipPut($clup)
EndFunc   ;==>SendX
发表于 2009-5-29 10:14:22 | 显示全部楼层
本帖最后由 sensel 于 2009-5-29 10:15 编辑

StringLen惹的祸。你可以想办法弄出字符串中中文字符的个数,然后在字符串后加上等量的空格。
发表于 2009-5-29 10:26:34 | 显示全部楼层
补空格代码:

Local $sString, $sCHN

$sString = "a0c中w*w文k/s字c符a"
$sCHN = StringRegExpReplace($sString, "[\x00-\xFF]", "")
For $i = 1 To StringLen($sCHN)
        $sString &= " "
Next

MsgBox(0, "Length: " & StringLen($sCHN), '[' & $sString & ']')
 楼主| 发表于 2009-5-29 10:54:42 | 显示全部楼层
解决了,谢谢pcbar 、sensel
发表于 2009-5-29 19:09:42 | 显示全部楼层
看下楼主自己发的这个帖子,说不定有所启示。
http://www.autoitx.com/forum.php ... 5966&highlight=
 楼主| 发表于 2009-5-30 07:22:40 | 显示全部楼层
谢谢pusofalse, 我的记性太差了
发表于 2009-5-30 08:19:28 | 显示全部楼层
Re sd007:
#include <GUIEdit.au3>
#include <GUIConstants.au3>

$hGUI = GUICreate("", 400, 300)

$hEdit = _GUICtrlEdit_Create($hGUI, "", 5, 5, 390, 280)
GUISetState()

_GUICtrlEdit_SetText($hEdit, FileRead("test.txt"))
_GUICtrlEdit_SetSel($hEdit, 15, 20)


$sText = "中国"
_SendMessage($hEdit, $EM_REPLACESEL, 0, $sText, 0, "wparam", "str")


Do
Until guiGetMsg() = -3
发表于 2009-5-30 08:44:40 | 显示全部楼层
到底是不是AU的问题?
 楼主| 发表于 2009-5-30 15:48:21 | 显示全部楼层
Re sd007:#include
#include

$hGUI = GUICreate("", 400, 300)

$hEdit = _GUICtrlEdit_Create($hGUI, "", 5, 5, 390, 280)
GUISetState()

_GUICtrlEdit_SetText($hEdit, FileRead("test.txt"))
_GUICt ...
pusofalse 发表于 2009-5-30 08:19


呵呵,还可以用这么简单的方法呀
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-5-10 08:17 , Processed in 0.079486 second(s), 19 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表