文本输入框字体颜色变换
本帖最后由 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 本帖最后由 xiehuahere 于 2012-7-26 17:12 编辑
回复 1# xyhqqaa
友情帮顶!
你说的那个富文本可以,是不是这个?
http://www.autoitscript.com/forum/topic/109876-guictrlrichedit-setsel-problem/
插入内容后,通过_GuiCtrlRichEdit_SetSel选取要改变颜色的文本,然后用_GUICtrlRichEdit_SetCharColor改变文本颜色。 这种效果,得到你这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
$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, _WinAPI_GetStockObject($DEFAULT_GUI_FONT))
Return $hRichEdit
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;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
#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=
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() 回复 1# xyhqqaa
不知道是不是你想要的效果,颜色是相间的,更多的你可以自定义。
你可以用随机数函数随机生成颜色,以此来实现彩色记录。 要实现这功能不一定要用富文本,偷懒一下
#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
很给力哦 呵呵 本帖最后由 xiehuahere 于 2012-7-26 22:18 编辑
回复 4# 甜茶少年
你这个程序退出时会出现内存错误(XP SP3,就打开退出,其他啥都没做),看看。。。
回复 3# happytc
总是对数据结构很懵 回复happytc
总是对数据结构很懵
zldfsz 发表于 2012-7-26 22:23 http://www.autoitx.com/images/common/back.gif
系统学过C/C++,就很是明白了。
主要是au3本身不支持这些。所以有可能用起来半懂不懂的。 回复 8# xiehuahere
这个,我是WIN7的,没有出现这个错误。
等我晚上回来虚拟机测试一下。 回复 12# 甜茶少年
可能跟我的系统有关,另一台XP机器上没出现这个问题。 楼上的都是人才啊,学习学习了。 各路高手代码学习了.
页:
[1]