【已解决】2个au3代码,能否用其中一个来修改另一个的代码?
本帖最后由 fenhanxue 于 2013-4-18 14:07 编辑就是有2个au3代码,想通过其中一个au3,来控制另一个au3的代码,应该怎么实现拉?
高人提供下思路或方案哈~谢谢谢谢拉
问题描述如下(感觉还是用图片才能说明问题):
也就是想通过程序1中的input的内容,来调整程序2中的这行代码:$Label1 = GUICtrlCreateLabel("", 40, 56, 140, 41)
程序1的代码如下:#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
$Form1 = GUICreate("程序1", 226, 165)
$Button1 = GUICtrlCreateButton("Button1", 56, 80, 97, 49)
$Input1 = GUICtrlCreateInput("", 56, 32, 97, 21)
GUISetState(@SW_SHOW)
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Button1
;;;;;;;;;;;;;这段代码该怎么写;;;;;;;;;;;;;;
EndSwitch
WEnd程序2代码:#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
$Form1 = GUICreate("程序2", 226, 165, 192, 124)
;;;;;;;;;;;;;;;;;;;;下面这行代码随程序1的输入而改变
$Label1 = GUICtrlCreateLabel("测试", 40, 56, 140, 41)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
GUISetState(@SW_SHOW)
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
EndSwitch
WEnd 也許可以試著使用全局變量~ 本帖最后由 netegg 于 2013-4-17 11:01 编辑
可以,但是要传递数据,或者把第一个文件当成一个函数库,第二个文件include下 用配置文件 这样笼统的说,没有实际意义,直接 StringReplace() 你需要修改的地方就行了。
需要替换依据,比如要替换“2中的这行代码:$Label1 = GUICtrlCreateLabel("", 40, 56, 140, 41)”其中的函数名后面的引号内字符。 用配置文件就很简单了 回复 5# afan
亲我想的也是这个 StringReplace()
但是我是2个au3文件,也就是可以用第一个au3StringReplace()第二个au3代码? 回复 7# fenhanxue
你就不会先 $str = FileRead('2.au3') 吗?亲 本帖最后由 mbdnmt 于 2013-4-17 23:32 编辑
可以给加分吧?
1.au3#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
$Form1 = GUICreate("程序1", 226, 165)
$Button1 = GUICtrlCreateButton("Button1", 56, 80, 97, 49)
$Input1 = GUICtrlCreateInput("", 56, 32, 97, 21)
GUISetState(@SW_SHOW)
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Button1
$str = GUICtrlRead($Input1)
$iFhd = FileOpen("2.au3", 0)
; 检查以只读打开的文件
If $iFhd = -1 Then
MsgBox(0, "错误", "无法打开文件.")
Exit
EndIf
; 读取文本
$chars = FileRead($iFhd)
;ConsoleWrite($chars)
FileClose($iFhd)
; 替换
$strNew = StringRegExpReplace($chars, '\$Label1 = GUICtrlCreateLabel.*"\,', '\$Label1 \= GUICtrlCreateLabel\("' & $str & '"\,')
;ConsoleWrite($strNew)
; 重写2.au3
$iFhd = FileOpen("2.au3", 2)
FileWrite($iFhd, $strNew)
FileFlush($iFhd)
FileClose($iFhd)
EndSwitch
WEnd2.au3#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
$Form1 = GUICreate("程序2", 226, 165, 192, 124)
;;;;;;;;;;;;;;;;;;;;下面这行代码随程序1的输入而改变
$Label1 = GUICtrlCreateLabel("测试", 40, 56, 140, 41)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
GUISetState(@SW_SHOW)
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
EndSwitch
WEnd 回复 10# mbdnmt
记得1.au3和2.au3放同一个路径下,楼主可以给加分了吧? 用传递参数或INI应该能实现 程序2代码不变,
程序1
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <winapi.au3>
#include <sendmessage.au3>
$Form1 = GUICreate("程序1", 226, 165)
$Button1 = GUICtrlCreateButton("Button1", 56, 80, 97, 49)
$Input1 = GUICtrlCreateInput("", 56, 32, 97, 21)
Local $hStatic = 0
Local $tBuffer = DllStructCreate('wchar')
GUISetState(@SW_SHOW)
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Button1
If(_WinAPI_IsWindow($hStatic)=0) Then
$hStatic = _WinAPI_GetWindow(WinGetHandle('程序2'),5)
EndIf
DllStructSetData($tBuffer,1,GUICtrlRead($Input1))
_SendMessage($hStatic,$WM_SETTEXT,0,DllStructGetPtr($tBuffer),0,'wparam','ptr')
EndSwitch
WEnd
回复 5# afan
懂了。。。谢啦 回复 14# fenhanxue
我提供的是运行前修改的方法,13楼提供的是程序2运行中的修改方法 回复 15# mbdnmt
恩恩哈~谢谢拉
页:
[1]
2