找回密码
 加入
搜索
查看: 11720|回复: 13

[AU3基础] 文本输入框字体颜色变换

 火.. [复制链接]
发表于 2012-7-26 15:22:54 | 显示全部楼层 |阅读模式
悬赏10金钱未解决
本帖最后由 xyhqqaa 于 2012-7-26 15:24 编辑

菜鸟求助,如果实现文本字体颜色的切换,成为绿色,爆为红色,但现在好像只是整体变化,使用富文本好像可以实现,但是好像没看到具体函数,求指教。或者说个参数,我去找找也行- -。。。。跪谢了。。效果:


#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiEdit.au3>

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 373, 330, 286, 240)
$Edit1 = GUICtrlCreateEdit("", 0, 48, 369, 281)
$Checkbox1 = GUICtrlCreateCheckbox("成", 32, 24, 97, 17)
$Checkbox2 = GUICtrlCreateCheckbox("爆", 240, 24, 97, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        Exit
                Case $Checkbox1
                        If GUICtrlRead($Checkbox1)=$GUI_CHECKED Then 
                                GUICtrlSetState($Checkbox2,$GUI_unCHECKED)
                                 _GUICtrlEdit_AppendText($Edit1, "成")
                                 GUICtrlSetColor($Edit1,0x0000FF )

    
                                EndIf 
                        Case $Checkbox2
                                If GUICtrlRead($Checkbox2)=$GUI_CHECKED Then 
                                GUICtrlSetState($Checkbox1,$GUI_unCHECKED)
                                 _GUICtrlEdit_AppendText($Edit1, "爆")
                                GUICtrlSetColor($Edit1,0xFF0000)
                                 
                                EndIf 
        EndSwitch
WEnd

附件: 您需要 登录 才可以下载或查看,没有账号?加入
发表于 2012-7-26 16:44:28 | 显示全部楼层
本帖最后由 xiehuahere 于 2012-7-26 17:12 编辑

回复 1# xyhqqaa


    友情帮顶!
  
  你说的那个富文本可以,是不是这个?
  http://www.autoitscript.com/foru ... dit-setsel-problem/

插入内容后,通过_GuiCtrlRichEdit_SetSel选取要改变颜色的文本,然后用_GUICtrlRichEdit_SetCharColor改变文本颜色。
发表于 2012-7-26 18:56:05 | 显示全部楼层
这种效果,得到你这10个金钱不容易,我敲了这么多代码,不容易呀,好久没有敲这么多au3代码了。




#include <WindowsConstants.au3>
#include <GuiConstantsEx.au3>
#include <SendMessage.au3>
#include <EditConstants.au3>
#include <GuiRichEdit.au3>

Global $hGui, $hRichText

$hGui = GUICreate("Colored Text", 300, 200)
$hRichText = _RichText_Create($hGui, 0, 0, 290, 190)
_RichText_InsertText($hRichText, "成成爆成爆")

GUISetState()

_RichText_SetSel($hRichText, 0, 1)
_RichText_SetColor($hRichText, 0x0000FF, True)
_RichText_SetSel($hRichText, 2, 3)
_RichText_SetColor($hRichText, 0x00FF00, True)
_RichText_SetSel($hRichText, 4, 5)
_RichText_SetColor($hRichText, 0xFF0000, True)
_RichText_SetSel($hRichText, 0, 0)

Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE


Func _RichText_Create($hGui, $iLeft, $iTop, $iWidth, $iHeight, $sStyle = 0, $sExStyle = 0)
        Local $hRichEdit, $sDefStyle, $hLib
        
        If Not IsHWnd($hGui) Then $hGui = HWnd($hGui)
        $sDefStyle = BitOR($WS_CHILD, $WS_VISIBLE, $WS_CLIPSIBLINGS)
        If $sStyle <> -1 Then $sDefStyle = BitOR($sDefStyle, $ES_WANTRETURN, $ES_NOHIDESEL, $WS_HSCROLL, $WS_VSCROLL, $ES_MULTILINE)

        $hLib = DllCall("kernel32.dll", "long", "LoadLibrary", "str", "RichEd20.dll")
        If Not @error Then $hLib = $hLib[0]

        $hRichEdit = DllCall("user32.dll", "long", "CreateWindowEx", "long", $sExStyle, "str", "RichEdit20A", "str", "", _
                        "long", $sDefStyle, "long", $iLeft, "long", $iTop, "long", $iWidth, "long", $iHeight, _
                        "hwnd", $hGui, "long", 0, "hwnd", $hGui, "long", 0)
        
        If Not @error Then
                _WinAPI_SetFont($hRichEdit[0], _WinAPI_GetStockObject($DEFAULT_GUI_FONT))
                Return $hRichEdit[0]
        Else
                SetError(1, 1, 1)
        EndIf

        Return 0
EndFunc


Func _RichText_InsertText($hRichEdit, $sText = "")
        Local $lResult, $tStruct
        
        If Not IsHWnd($hRichEdit) Then $hRichEdit = HWnd($hRichEdit)
        $tStruct = DllStructCreate("dword;uint")
        DllStructSetData($tStruct, 1, $ST_SELECTION)
        DllStructSetData($tStruct, 2, $CP_ACP)
        Return _SendMessage($hRichEdit, $EM_SETTEXTEX, DllStructGetPtr($tStruct), $sText, 0, "ptr", "str")
EndFunc

Func _RichText_SetSel($hRichEdit, $iStart, $iEnd)
        Local Const $EM_HIDESELECTION = ($WM_USER + 63)
        
        If Not IsHWnd($hRichEdit) Then $hRichEdit = HWnd($hRichEdit)
        _SendMessage($hRichEdit, $EM_SETSEL, $iStart, $iEnd)
        _SendMessage($hRichEdit, $EM_HIDESELECTION, 0)
EndFunc


Func _RichText_SetColor($hWnd, $hColor, $iSelec = True)
        Local $tCharFormat, $pCharFormat
        
        $tCharFormat = DllStructCreate("uint cbSize;int dwMask;dword dwEffects;long yHeight;long yOffset;uint crTextColor;" _
                         & "byte bCharSet;byte bPitchAndFamily;char szFaceName[32];ushort wWeight;short sSpacing;uint crBackColor;uint lcid;dword dwReserved;short sStyle;ushort wKerning" _
                         & ";byte bUnderlineType;byte bAnimation;byte bRevAuthor;byte bReserved1")
        DllStructSetData($tCharFormat, "cbSize", DllStructGetSize($tCharFormat))
        DllStructSetData($tCharFormat, "dwMask", 0x40000000)
        DllStructSetData($tCharFormat, "crTextColor", $hColor)
        $pCharFormat = DllStructGetPtr($tCharFormat)
        Return _SendMessage($hWnd, $WM_USER + 68, $iSelec, $pCharFormat)
EndFunc

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×

评分

参与人数 2金钱 +40 贡献 +2 收起 理由
xyhqqaa + 20 嘻嘻。您辛苦了。。。
zldfsz + 20 + 2 +

查看全部评分

发表于 2012-7-26 19:39:06 | 显示全部楼层
#include <GUIConstantsEx.au3>
#include <GDIPlus.au3>
#include <WinAPI.au3>
#Include <GuiRichEdit.au3>
#include <Color.au3>

Local $Msg,$hGUI, $EditID,$Text1,$Text2,$Var1,$Var2,$Change
Local $DeColor[3]=[0x80, 0x90, 0xff]

Local $UseColor = _ColorSetCOLORREF($DeColor)



$hGUI=GUICreate("测试",500,300)
GUISetState(@SW_SHOW)

;创建控件
$EditID=_GUICtrlRichEdit_Create($hGUI,"",0,0,500,300)






$Text1=_GUICtrlRichEdit_GetText($EditID)

_GUICtrlRichEdit_SetCharColor($EditID,"304050")

$Change=1

Func CheckText()

        $Text2=_GUICtrlRichEdit_GetText($EditID)
        
        $Var1=StringLen($Text1)
        $Var2=StringLen($Text2)
        
        
        If $Var1<$Var2 Then 
                
                
               _GUICtrlRichEdit_SetSel($EditID,$Var1,$Var2)
                   If $Change=1 Then  
                      _GUICtrlRichEdit_SetCharColor($EditID)
                          $Change=0
                   Else
                          _GUICtrlRichEdit_SetCharColor($EditID,"304050")
                          $Change=1
                  EndIf
                  
                   ControlFocus($EditID,"","")
                   Send("{RIGHT}")
                   
                 
                 
         EndIf
         
         $Text1=$Text2
EndFunc     
; 运行界面,直到窗口被关闭
        While 1
                $Msg=GUIGetMsg()
                If $Msg= $GUI_EVENT_CLOSE Then
                        Exit
                Else
                CheckText()
                EndIf
                
        WEnd
        
        GUIDelete()

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×

评分

参与人数 2金钱 +40 贡献 +2 收起 理由
xyhqqaa + 20 嘻嘻。您辛苦了
zldfsz + 20 + 2

查看全部评分

发表于 2012-7-26 19:40:48 | 显示全部楼层
回复 1# xyhqqaa


    不知道是不是你想要的效果,颜色是相间的,更多的你可以自定义。
你可以用随机数函数随机生成颜色,以此来实现彩色记录。
发表于 2012-7-26 19:42:11 | 显示全部楼层
要实现这功能不一定要用富文本,偷懒一下
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiEdit.au3>
#include <IE.au3>

Global $str1 = '<font size=3 color=#00ff00>成</font>'
Global $str2 = '<font size=3 color=#ff0000>爆</font>'
#region ### START Koda GUI section ### Form=
$oIE = _IECreateEmbedded()
$Form1 = GUICreate("Form1", 373, 330, 286, 240)
$obj1 = GUICtrlCreateObj($oIE, 0, 48, 369, 281)
$Checkbox1 = GUICtrlCreateCheckbox("成", 32, 24, 97, 17)
$Checkbox2 = GUICtrlCreateCheckbox("爆", 240, 24, 97, 17)
_IENavigate($oIE, "about:blank")
GUISetState(@SW_SHOW)
#endregion ### END Koda GUI section ###

While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        Exit
                Case $Checkbox1
                        GUICtrlSetState($Checkbox2, $GUI_unCHECKED)
                        If BitAND(GUICtrlRead($Checkbox1),$GUI_CHECKED) Then
                                $oIE.document.write($str1)
                        EndIf
                Case $Checkbox2
                        GUICtrlSetState($Checkbox1, $GUI_unCHECKED)
                        If BitAND(GUICtrlRead($Checkbox2),$GUI_CHECKED) Then
                                $oIE.document.write($str2)
                        EndIf
        EndSwitch
WEnd

评分

参与人数 3金钱 +56 贡献 +2 收起 理由
xyhqqaa + 20 嘻嘻,您辛苦了。。一个一个测试。。论坛大 ...
xiehuahere + 16 内嵌浏览器,好思路!
zldfsz + 20 + 2 这方法不错

查看全部评分

发表于 2012-7-26 20:02:11 | 显示全部楼层
很给力哦 呵呵
发表于 2012-7-26 22:16:35 | 显示全部楼层
本帖最后由 xiehuahere 于 2012-7-26 22:18 编辑

回复 4# 甜茶少年


    你这个程序退出时会出现内存错误(XP SP3,就打开退出,其他啥都没做),看看。。。
  

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
发表于 2012-7-26 22:23:32 | 显示全部楼层
回复 3# happytc

总是对数据结构很懵
发表于 2012-7-26 23:30:53 | 显示全部楼层
回复  happytc

总是对数据结构很懵
zldfsz 发表于 2012-7-26 22:23


系统学过C/C++,就很是明白了。

主要是au3本身不支持这些。所以有可能用起来半懂不懂的。
发表于 2012-7-27 07:47:52 | 显示全部楼层
回复 8# xiehuahere


    这个,我是WIN7的,没有出现这个错误。
等我晚上回来虚拟机测试一下。
发表于 2012-7-27 09:44:43 | 显示全部楼层
回复 12# 甜茶少年


    可能跟我的系统有关,另一台XP机器上没出现这个问题。
发表于 2012-7-27 09:51:33 | 显示全部楼层
楼上的都是人才啊,学习学习了。
发表于 2012-7-27 12:51:32 | 显示全部楼层
各路高手代码学习了.
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-4-28 17:45 , Processed in 0.124468 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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