一个操作mdb数据库,结合Listview展示的脚本,几个问题请教高手帮忙。源码已经放出。
本帖最后由 zerozha 于 2012-2-17 12:42 编辑源码如下,基本功能都正常了。源码很多地方借鉴了论坛里的内容,在这里感谢了!
这个源码现在有几个问题:
1、419行处:FileDelete 删除文件为何失败?
2、967行处:_editRecoder($myIndex) ,弹出新窗口,进程会僵死,不响应了。是何原因?
3、想提高使用体验,想让用户增加记录和修改记录时,记住listview的位置,修改的话,刷新数据后还是返回到当前滚动条位置,新增的话,滚动条滚动到最新添加的位置。如何实现?
4、程序中将png转成jpg,然后用GUICtrlSetImage显示,如何改成直接显示png的?这样就不用转来转去了。
5、如果将图片用二进制存进数据库,读出来后能直接显示么?感觉存成图片再显示不是很方便啊。#NoTrayIcon
#region ;**** 参数创建于 ACNWrapper_GUI ****
#AutoIt3Wrapper_Icon=cat_2.ico
#AutoIt3Wrapper_Outfile=通用资产管理工具.exe
#AutoIt3Wrapper_UseUpx=n
#AutoIt3Wrapper_Res_Comment=通用资产管理工具
#AutoIt3Wrapper_Res_Description=管理和维护设备资产信息。
#AutoIt3Wrapper_Res_Fileversion=0.0.0.4
#AutoIt3Wrapper_Res_Fileversion_AutoIncrement=p
#AutoIt3Wrapper_Res_LegalCopyright=查明胜 Mason@zhenhao.me
#AutoIt3Wrapper_Add_Constants=n
#AutoIt3Wrapper_Run_Tidy=y
#endregion ;**** 参数创建于 ACNWrapper_GUI ****
#include <GDIPlusConstants.au3>
#include <StructureConstants.au3>
#include <ListBoxConstants.au3>
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiStatusBar.au3>
#include <ListViewConstants.au3>
#include <WindowsConstants.au3>
#include <GUIListBox.au3>
#include <WindowsConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <StaticConstants.au3>
#include <GUIListView.au3>
#include <Access.au3>
#include <Excel.au3>
#include <GDIPlus.au3>
#include <WinAPI.au3>
#include <TabConstants.au3>
Opt('MustDeclareVars', 1)
;防止程序被重复运行
If WinExists("Assets Tools") Then Exit
AutoItWinSetTitle("Assets Tools")
#CS
全局参数设置
#CE
;版权信息
Global $Program_name = "开发测试设备管理工具"
Global $Program_desc = "用于管理开发、测试机房设备数据。"
Global $Program_ver = "版本:0.1"
Global $Program_copyright = "版权所有:查明胜 Mason@zhenhao.me"
;一些目录
Global $dirTMP = @ScriptDir & "\tmp"
Global $dirData = @ScriptDir & "\data"
Global $dirImage = @ScriptDir & "\images"
;数据库参数
Global $adSource = $dirData & "\DAT.MDB"
;资产主要信息表
Global $adTable = 'Assets'
;参数表
Global $csTable = 'Vars'
Global $T = '*'
Global $ID = ''
Global $Key_Word = " WHERE 1=1"
;初始化窗体
;Global $Main, $About, $Config
;初始化控件
Global $Combo_Fields = "机柜号|设备型号|设备类型|序列号|IP地址|所属应用|信息点|操作系统|MAC地址|机房区域|归属|备注"
Global $ListView_Cols = "序号|" & $Combo_Fields;ListView窗口列字段
Global $Mdb_Table_Fields = $ListView_Cols & "|QRcode";数据库表字段
#CS
主程序
#CE
;初始化目录结构
If Not FileExists($dirTMP) Then DirCreate($dirTMP)
If Not FileExists($dirData) Then DirCreate($dirData)
If Not FileExists($dirImage) Then DirCreate($dirImage)
If Not FileExists($adSource) Then _init_db()
Dim $Main, $Form_ADD, $ListView1, $StatusBar1, $Combo_key, $Input_key
Dim $Input1, $Input2, $Input3, $Input4, $Input5, $Input6, $Input7, $Input8, $Input9, $Input10, $Input11, $Input12, $input13
Dim $Combo_3, $Combo_8, $Combo_10, $Combo_11
Dim $pic, $Button_SAV, $Button_del, $Button_PRT
;
_Main()
Func _Main()
#region ### START Koda GUI section ### Form=c:\users\mason-home\desktop\开发测试设备管理工具.kxf
$Main = GUICreate($Program_name, 808, 649, 225, 25, BitOR($GUI_SS_DEFAULT_GUI, $WS_MAXIMIZEBOX, $WS_SIZEBOX, $WS_THICKFRAME, $WS_TABSTOP))
;菜单栏
Dim $MenuItem1, $MenuItem2, $MenuItem3, $MenuItem4, $MenuItem5, $MenuItem6, $MenuItem7, $MenuItem8, $MenuItem9, $MenuItem10, $MenuItem11, $MenuItem12
$MenuItem1 = GUICtrlCreateMenu("设备管理")
$MenuItem4 = GUICtrlCreateMenuItem("增加", $MenuItem1)
$MenuItem5 = GUICtrlCreateMenuItem("修改", $MenuItem1)
$MenuItem6 = GUICtrlCreateMenuItem("删除", $MenuItem1)
$MenuItem7 = GUICtrlCreateMenuItem("", $MenuItem1)
$MenuItem8 = GUICtrlCreateMenuItem("导出Excel", $MenuItem1)
$MenuItem9 = GUICtrlCreateMenuItem("", $MenuItem1)
$MenuItem10 = GUICtrlCreateMenuItem("退出", $MenuItem1)
$MenuItem2 = GUICtrlCreateMenu("设置")
$MenuItem11 = GUICtrlCreateMenuItem("参数", $MenuItem2)
$MenuItem3 = GUICtrlCreateMenu("帮助")
$MenuItem12 = GUICtrlCreateMenuItem("关于", $MenuItem3)
Dim $Group1, $Label_search, $Button_Search, $Button_List
$Group1 = GUICtrlCreateGroup("设备清单", 8, 16, 785, 497, -1, $WS_EX_TRANSPARENT)
$Combo_key = GUICtrlCreateCombo("", 192, 484, 145, 25, BitOR($CBS_DROPDOWN, $CBS_AUTOHSCROLL))
GUICtrlSetData(-1, $Combo_Fields, "机柜号")
$Input_key = GUICtrlCreateInput("", 376, 484, 121, 21)
$Label_search = GUICtrlCreateLabel("请输入查询条件:", 88, 488, 100, 17)
$Button_Search = GUICtrlCreateButton("查询", 528, 480, 75, 25)
$Button_List = GUICtrlCreateButton("显示全部", 640, 480, 75, 25)
Local $iExWindowStyle = BitOR($WS_EX_DLGMODALFRAME, $WS_EX_CLIENTEDGE)
Local $iExListViewStyle = BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_GRIDLINES, $LVS_EX_DOUBLEBUFFER)
$ListView1 = GUICtrlCreateListView($ListView_Cols, 16, 32, 762, 438, -1, $iExWindowStyle)
_GUICtrlListView_SetExtendedListViewStyle($ListView1, $iExListViewStyle)
GUICtrlCreateGroup("", -99, -99, 1, 1)
Dim $Group2, $Button_add, $Button_edit, $Button_del, $Button_export, $Button_about, $Button_config
$Group2 = GUICtrlCreateGroup("设备维护", 8, 520, 785, 81)
;GUICtrlSetResizing(-1, $GUI_DOCKAUTO + $GUI_DOCKWIDTH + $GUI_DOCKHEIGHT)
$Button_add = GUICtrlCreateButton("增加", 32, 552, 75, 25)
$Button_edit = GUICtrlCreateButton("修改", 157, 552, 75, 25)
$Button_del = GUICtrlCreateButton("删除", 282, 552, 75, 25)
$Button_export = GUICtrlCreateButton("导出Excel", 406, 552, 75, 25)
$Button_about = GUICtrlCreateButton("关于", 656, 552, 75, 25)
$Button_config = GUICtrlCreateButton("设置", 531, 552, 75, 25)
GUICtrlCreateGroup("", -99, -99, 1, 1)
;在ListView窗口添加右键菜单
Dim $RC_menu, $RC1, $RC2
$RC_menu = GUICtrlCreateContextMenu($ListView1)
$RC1 = GUICtrlCreateMenuItem("修改", $RC_menu)
$RC2 = GUICtrlCreateMenuItem("删除", $RC_menu)
;状态栏
$StatusBar1 = _GUICtrlStatusBar_Create($Main)
Dim $StatusBar1_PartsWidth =
_GUICtrlStatusBar_SetParts($StatusBar1, $StatusBar1_PartsWidth)
_GUICtrlStatusBar_SetText($StatusBar1, "数据库:" & $adSource, 0)
_GUICtrlStatusBar_SetText($StatusBar1, @TAB & "共有条记录", 1)
GUISetState(@SW_SHOW)
#endregion ### END Koda GUI section ###
;注册Windows消息函数
GUIRegisterMsg($WM_SIZE, "_WM_SIZE")
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
;载入数据
_readTable()
Dim $nMsg
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $ListView1
_GUICtrlListView_SortItems($ListView1, GUICtrlGetState($ListView1))
#CS
响应按钮操作
#CE
Case $Button_Search
_searchRecord()
Case $Button_List
$Key_Word = " Where 1=1"
_readTable()
Case $Button_add
_editRecoder("")
Case $Button_edit
;修改
Local $n_id = _GUICtrlListView_GetItemText($ListView1, Number(_GUICtrlListView_GetSelectedIndices($ListView1)));选中行的第一列
If $n_id = 0 Then
MsgBox(48, '提示:', '请选择要修改的条目!')
Else
Dim $Index
;$Index = Number(_GUICtrlListView_GetSelectedIndices($ListView1))
;MsgBox(48, '提示:', $Index)
_editRecoder($n_id)
;_GUICtrlListView_SetItemFocused($ListView1, $Index)
EndIf
Case $Button_del
_delRecod()
Case $Button_export
_to_EXCEL()
Case $Button_about
_about()
Case $Button_config
_config()
#CS
响应菜单操作
#CE
Case $MenuItem4;add
_editRecoder("")
Case $MenuItem5;edit
Local $n_id = _GUICtrlListView_GetItemText($ListView1, Number(_GUICtrlListView_GetSelectedIndices($ListView1)));选中行的第一列
If $n_id = 0 Then
MsgBox(48, '提示:', '请选择要修改的条目!')
Else
_editRecoder($n_id)
EndIf
Case $MenuItem6;del
_delRecod()
Case $MenuItem8
_to_EXCEL()
Case $MenuItem10
Exit
Case $MenuItem11
_config()
Case $MenuItem12
_about()
#CS
响应鼠标右键
#CE
Case $RC1
;修改
Local $n_id = _GUICtrlListView_GetItemText($ListView1, Number(_GUICtrlListView_GetSelectedIndices($ListView1)));选中行的第一列
If $n_id = 0 Then
MsgBox(48, '提示:', '请选择要修改的条目!')
Else
_editRecoder($n_id)
EndIf
Case $RC2
_delRecod()
EndSwitch
WEnd
_GUICtrlListView_UnRegisterSortCallBack($ListView1)
GUIDelete()
EndFunc ;==>_Main
Func _init_db()
_accessCreateDB($adSource)
Local $adCol = "序号 counter primary key|机柜号 text(255)|设备型号 text(255)|设备类型 text(255)|序列号 text(255)|IP地址 text(255)|所属应用 text(255)|信息点 text(255)|操作系统 text(255)|MAC地址 text(255)|机房区域 text(255)|归属 text(255) |备注 memo | QRcode text(255) "
_accessCreateTable($adSource, $adTable, $adCol)
$adCol = "字段 text(255) | 值 text(255) "
_accessCreateTable($adSource, $csTable, $adCol)
_accessAddRecord($adSource, $csTable, "操作系统|AIX", 0)
_accessAddRecord($adSource, $csTable, "机房区域|开发机房", 0)
_accessAddRecord($adSource, $csTable, "机房区域|测试机房", 0)
_accessAddRecord($adSource, $csTable, "归属|自购", 0)
_accessAddRecord($adSource, $csTable, "归属|租用", 0)
_accessAddRecord($adSource, $csTable, "设备类型|服务器", 0)
MsgBox(64, '提示', '数据库创建成功!', 0)
EndFunc ;==>_init_db
Func _about()
Dim $Form_about, $GroupBox1, $Image1, $Label1, $Label2, $Label4, $Label3, $Button_about_OK
#region ### START Koda GUI section ### Form=c:\users\mason-home\desktop\form_about.kxf
$Form_about = GUICreate("关于", 350, 250, 302, 218)
$GroupBox1 = GUICtrlCreateGroup("", 8, 8, 305, 185)
$Image1 = GUICtrlCreatePic(@ScriptDir & "\eee.bmp", 16, 24, 105, 97)
$Label1 = GUICtrlCreateLabel($Program_name, 152, 24, 148, 34)
$Label2 = GUICtrlCreateLabel($Program_ver, 152, 48, 148, 17)
$Label4 = GUICtrlCreateLabel($Program_desc, 16, 160, 268, 17)
$Label3 = GUICtrlCreateLabel($Program_copyright, 16, 136, 268, 17)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Button_about_OK = GUICtrlCreateButton("确定(&O)", 124, 208, 75, 25, 0)
GUISetState(@SW_SHOW)
#endregion ### END Koda GUI section ###
Dim $nMsg
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
GUIDelete($Form_about)
Return
Case $Button_about_OK
GUIDelete($Form_about)
Return
EndSwitch
WEnd
GUIDelete()
EndFunc ;==>_about
Func _readTable() ;查看数据库中所有的记录
GUICtrlSendMsg($ListView1, $LVM_DELETEALLITEMS, 0, 0)
GUISetState(@SW_LOCK, $Main)
Dim $addfld, $RS
$addfld = ObjCreate("ADODB.Connection")
$addfld.Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & $adSource) ;这句话和上一句一起是连接到数据库
$RS = ObjCreate("ADODB.Recordset")
$RS.ActiveConnection = $addfld
ConsoleWrite("Select " & $T & " From " & $adTable & $Key_Word & @CRLF)
$RS.Open("Select " & $T & " From " & $adTable & $Key_Word) ;$t : 表示一个连接$adtable :表示的是这个表的名称 这句话是:从$adtable表的第一条开始读取,直到结束$t的星号是所有的意思
While Not $RS.eof And Not $RS.bof
If @error = 1 Then ExitLoop
GUICtrlCreateListViewItem( _
$RS.Fields(0).value & "|" & _
$RS.Fields(1).value & "|" & _
$RS.Fields(2).value & "|" & _
$RS.Fields(3).value & "|" & _
$RS.Fields(4).value & "|" & _
$RS.Fields(5).value & "|" & _
$RS.Fields(6).value & "|" & _
$RS.Fields(7).value & "|" & _
$RS.Fields(8).value & "|" & _
$RS.Fields(9).value & "|" & _
$RS.Fields(10).value & "|" & _
$RS.Fields(11).value & "|" & _
$RS.Fields(12).value, $ListView1)
; $RS.Fields (0).value;表示的是第一个数据,往后依此类推,第二个第三个···把读取到的值用 | 隔开,并把他们放到对应的ListView下面。
$RS.movenext ;
WEnd
$RS.close
$addfld.Close ;读取结束后结束所有的连接,并把缓存的数据清空。
_GUICtrlListView_RegisterSortCallBack($ListView1)
_update_Statusbar()
GUISetState(@SW_UNLOCK, $Main)
EndFunc ;==>_readTable
;更新状态栏记录数
Func _update_Statusbar()
Local $num = 0
$num = _GUICtrlListView_GetItemCount($ListView1)
_GUICtrlStatusBar_SetText($StatusBar1, @TAB & "共有 " & $num & " 条记录", 1)
EndFunc ;==>_update_Statusbar
Func _delRecod()
Dim $Strn, $Strnspin
$Strn = GUICtrlRead(GUICtrlRead($ListView1))
$Strnspin = StringSplit($Strn, "|")
If $Strnspin = "0" Then
MsgBox(48, '提示:', '请选择要删除的条目!')
Else
Dim $iResult
$iResult = MsgBox(289, "警告", "确定要删除序号为[" & $Strnspin & "]的记录?", 0)
ConsoleWrite("iResult=" & $iResult & @CRLF)
If $iResult = 1 Then
;删除数据库记录
_accessDeleteRecord($adSource, $adTable, "序号", $Strnspin, 1)
;删除列表项
_GUICtrlListView_DeleteItemsSelected($ListView1)
;删除QRcode图片文件
_update_Statusbar()
EndIf
EndIf
;删除列表项
EndFunc ;==>_delRecod
Func _getQRcode($string)
ConsoleWrite("QR编码文本:" & $string & @CRLF)
Local $picName = _random_key()
If _QRcode($dirImage & "\" & $picName & ".png", 512, $string) Then
Dim $hImage, $sCLSID, $tData, $tParams
;png2jpg
_GDIPlus_Startup()
; Load image
$hImage = _GDIPlus_ImageLoadFromFile($dirImage & "\" & $picName & ".png")
; Get JPEG encoder CLSID
$sCLSID = _GDIPlus_EncodersGetCLSID("JPG")
; Set up parameters for 90 degree rotation
$tData = DllStructCreate("int Data")
DllStructSetData($tData, "Data", $GDIP_EVTTRANSFORMROTATE90)
$tParams = _GDIPlus_ParamInit(1)
_GDIPlus_ParamAdd($tParams, $GDIP_EPGTRANSFORMATION, 1, $GDIP_EPTLONG, DllStructGetPtr($tData, "Data"))
; Save image with rotation
_GDIPlus_ImageSaveToFileEx($hImage, $dirImage & "\" & $picName & ".jpg", $sCLSID, DllStructGetPtr($tParams))
; Shut down GDI+ library
_GDIPlus_Shutdown()
Local $opt = FileDelete($dirImage & "\" & $picName & ".png")
ConsoleWrite("删除文件:" & $dirImage & "\" & $picName & ".png" & "[" & $opt & "],1成功 0失败")
Return $picName & ".jpg"
Else
Return ""
MsgBox(48, '提示', 'QRcode 图片生成失败!' & @CRLF & "需要连接互联网!")
EndIf
EndFunc ;==>_getQRcode
;$sPath:生成文件的带路径的文件名,应为png文件
;$WH:生成文件的宽高
;$sString:用于生成文件的字符串
Func _QRcode($sPath, $sWH, $sString)
#CS
If StringRegExp($sString, '[^\x00-\xff]') Then
SetError(1)
Return False
EndIf ;检测是否为全单字节
;这个过滤了中文,所以注释掉
#CE
$sString = StringReplace($sString, '+', '%2B')
$sString = StringReplace($sString, '%', '%25')
$sString = StringReplace($sString, @CR, '%0D')
$sString = StringReplace($sString, @LF, '%0A')
$sString = StringReplace($sString, ' ', '+')
If StringLen($sString) + StringLen("http://chart.apis.google.com/chart?cht=qr&chs=" & $sWH & 'x' & $sWH & "&chl=") > 2048 Then
SetError(2)
Return False
EndIf;字符超限
FileDelete($sPath)
;开始发送数据,并获取二维码了
InetGet("http://chart.apis.google.com/chart?cht=qr&chs=" & $sWH & 'x' & $sWH & "&chl=" & _encodeURIComponent($sString), $sPath, 1, 0)
If @error Then
SetError(3)
Return False
EndIf;网络故障
Return True
EndFunc ;==>_QRcode
#CS
Unicode 编码、解码函数
#CE
Func _encodeURIComponent($sStr)
Local $oSC, $sResult
$oSC = ObjCreate('MSScriptControl.ScriptControl.1')
$oSC.Language = 'JavaScript'
$sResult = $oSC.Eval('encodeURIComponent(''' & $sStr & ''')')
Return $sResult
EndFunc ;==>_encodeURIComponent
Func _decodeURIComponent($sStr)
Local $oSC, $sResult
$oSC = ObjCreate('MSScriptControl.ScriptControl.1')
$oSC.Language = 'JavaScript'
$sResult = $oSC.Eval('decodeURIComponent(''' & $sStr & ''')')
Return $sResult
EndFunc ;==>_decodeURIComponent
Func _initCombox()
GUICtrlSetData($Combo_3, "")
GUICtrlSetData($Combo_8, "")
GUICtrlSetData($Combo_10, "")
GUICtrlSetData($Combo_11, "")
GUICtrlSetData($Combo_3, _Array2String(_SelectDB("Select 值 From " & $csTable & " where 字段='设备类型'")), "")
GUICtrlSetData($Combo_8, _Array2String(_SelectDB("Select 值 From " & $csTable & "where 字段='操作系统'")), "")
GUICtrlSetData($Combo_10, _Array2String(_SelectDB("Select 值 From " & $csTable & "where 字段='机房区域'")), "")
GUICtrlSetData($Combo_11, _Array2String(_SelectDB("Select 值 From " & $csTable & "where 字段='归属'")), "")
EndFunc ;==>_initCombox
Func _editRecoder($strID)
Dim $Group_Input, $Group1
Dim $Label1, $Label2, $Label3, $Label4, $Label5, $Label6, $Label7, $Label8, $Label9, $Label10, $Label11, $Label12
Dim $Button_save, $Button_cancel
#region ### START Koda GUI section ### Form=d:\通用资产管理工具\form_add.kxf
$Form_ADD = GUICreate("设备新增和预览", 762, 543, 293, 88)
$Group_Input = GUICtrlCreateGroup("设备信息录入", 16, 16, 449, 497)
$Label1 = GUICtrlCreateLabel("机柜号:", 48, 48, 52, 17)
$Label2 = GUICtrlCreateLabel("设备型号:", 36, 80, 64, 17)
GUICtrlSetColor(-1, 0xFF0000)
$Label3 = GUICtrlCreateLabel("序列号:", 48, 144, 52, 17)
GUICtrlSetColor(-1, 0xFF0000)
$Label4 = GUICtrlCreateLabel("IP地址:", 50, 176, 50, 17)
$Label5 = GUICtrlCreateLabel("所属应用:", 36, 208, 64, 17)
$Label6 = GUICtrlCreateLabel("信息点:", 48, 240, 52, 17)
$Label7 = GUICtrlCreateLabel("操作系统:", 36, 272, 64, 17)
GUICtrlSetColor(-1, 0xFF0000)
$Label8 = GUICtrlCreateLabel("信息点2:", 42, 304, 58, 17)
$Label9 = GUICtrlCreateLabel("机房区域:", 36, 336, 64, 17)
GUICtrlSetColor(-1, 0xFF0000)
$Label10 = GUICtrlCreateLabel("归属:", 60, 368, 40, 17)
GUICtrlSetColor(-1, 0xFF0000)
$Label11 = GUICtrlCreateLabel("备注:", 60, 400, 40, 17)
$Input1 = GUICtrlCreateInput("", 120, 40, 121, 21)
$Input2 = GUICtrlCreateInput("", 120, 72, 121, 21)
$Input4 = GUICtrlCreateInput("", 120, 136, 121, 21)
$Input5 = GUICtrlCreateInput("", 120, 168, 121, 21)
$Input6 = GUICtrlCreateInput("", 120, 200, 321, 21)
$Input7 = GUICtrlCreateInput("", 120, 232, 121, 21)
$Input8 = GUICtrlCreateInput("", 120, 264, 121, 21)
GUICtrlSetState(-1, $GUI_DISABLE)
$Input9 = GUICtrlCreateInput("", 120, 296, 121, 21)
$Input10 = GUICtrlCreateInput("", 120, 328, 121, 21)
GUICtrlSetState(-1, $GUI_DISABLE)
$Input12 = GUICtrlCreateInput("", 120, 392, 329, 21)
$Button_save = GUICtrlCreateButton("保存", 72, 464, 75, 25)
$Button_cancel = GUICtrlCreateButton("取消", 320, 464, 75, 25)
$Combo_11 = GUICtrlCreateCombo("", 280, 360, 145, 25, BitOR($CBS_DROPDOWN, $CBS_AUTOHSCROLL))
$Input11 = GUICtrlCreateInput("", 120, 360, 121, 21)
GUICtrlSetState(-1, $GUI_DISABLE)
$Combo_10 = GUICtrlCreateCombo("", 280, 328, 145, 25, BitOR($CBS_DROPDOWN, $CBS_AUTOHSCROLL))
$Combo_8 = GUICtrlCreateCombo("", 280, 264, 145, 25, BitOR($CBS_DROPDOWN, $CBS_AUTOHSCROLL))
$Label12 = GUICtrlCreateLabel("设备类型:", 36, 112, 64, 17)
GUICtrlSetColor(-1, 0xFF0000)
$Input3 = GUICtrlCreateInput("", 120, 104, 121, 21)
GUICtrlSetState(-1, $GUI_DISABLE)
$Combo_3 = GUICtrlCreateCombo("", 272, 104, 145, 25, BitOR($CBS_DROPDOWN, $CBS_AUTOHSCROLL))
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Group1 = GUICtrlCreateGroup("QRcode", 496, 16, 220, 228)
$pic = GUICtrlCreatePic("", 504, 32, 200, 200)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Button_SAV = GUICtrlCreateButton("生成", 520, 255, 72, 32)
GUICtrlSetCursor(-1, 0)
$Button_PRT = GUICtrlCreateButton("打印", 624, 255, 80, 32)
GUICtrlSetCursor(-1, 0)
GUISetState(@SW_SHOW)
#endregion ### END Koda GUI section ###
;初始化下拉列表
_initCombox()
If $strID = "" Then
_clearInput()
Else
;$strID不为空,则为编辑状态;获取数据,填充到表
_setInput($strID)
EndIf
Dim $nMsg
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
GUIDelete($Form_ADD)
Return
Case $Button_save
If _writeInput($strID) Then;保存数据
GUIDelete($Form_ADD)
Return
EndIf
Case $Button_cancel
GUIDelete($Form_ADD)
Return
Case $Combo_3
GUICtrlSetData($Input3, GUICtrlRead($Combo_3))
Case $Combo_8
GUICtrlSetData($Input8, GUICtrlRead($Combo_8))
Case $Combo_10
GUICtrlSetData($Input10, GUICtrlRead($Combo_10))
Case $Combo_11
GUICtrlSetData($Input11, GUICtrlRead($Combo_11))
;QRcode区操作按钮
Case $Button_SAV
Local $iResult = 0
If $input13 <> '' And FileExists($dirImage & "\" & $input13) Then
$iResult = MsgBox(289, "'提示", $input13 & " 图片文件已经存在!是否重新生成?", 0)
Else
$iResult = 1
EndIf
If $iResult = 1 Then
Local $string = "机柜号=" & GUICtrlRead($Input1) _
& ",设备型号=" & GUICtrlRead($Input2) _
& ",设备类型=" & GUICtrlRead($Input3) _
& ",序列号=" & GUICtrlRead($Input4) _
& ",IP地址=" & GUICtrlRead($Input5) _
& ",所属应用=" & GUICtrlRead($Input6) _
& ",信息点=" & GUICtrlRead($Input7) _
& ",操作系统=" & GUICtrlRead($Input8) _
& ",MAC地址=" & GUICtrlRead($Input9) _
& ",机房区域=" & GUICtrlRead($Input10) _
& ",归属=" & GUICtrlRead($Input11)
$input13 = _getQRcode($string)
GUICtrlSetImage($pic, $dirImage & "\" & $input13)
EndIf
Case $Button_PRT
Local $iResult = 0
If $input13 <> '' And FileExists($dirImage & "\" & $input13) Then
$iResult = MsgBox(289, "'提示", '是否打印图片文件:' & $input13 & "?", 0)
Else
MsgBox(48, '提示:', '没有二维码可以打印!')
EndIf
If $iResult = 1 Then
;打印
RunWait('Rundll32.exe "' & @SystemDir & '\mshtml.dll",PrintHTML "' & $dirImage & "\" & $input13 & '"', @SystemDir)
EndIf
EndSwitch
WEnd
GUIDelete()
EndFunc ;==>_editRecoder
;按照字段搜索数据。
;如果录入框有输入,则做关键字查询。
;如果录入框无数据,则列出本分类所有数据。
Func _searchRecord()
Local $key1 = GUICtrlRead($Combo_key)
Local $key2 = GUICtrlRead($Input_key)
If $key1 = '' Or $key2 = '' Then
MsgBox(48, '提示:', '请选择查询条件!')
Else
$Key_Word = " where " & $key1 & " like '%" & $key2 & "%'"
_readTable()
EndIf
EndFunc ;==>_searchRecord
Func _to_EXCEL() ;扩展功能,把选中的记录读取并写到excel里面,
Local $excnum = _GUICtrlListView_GetItemCount($ListView1)
ConsoleWrite($excnum & @CRLF)
If $excnum = 0 Then
MsgBox(64, "提示", "没有记录可被输出.")
Else
Local $array
_ArrayAdd($array, $Mdb_Table_Fields)
For $I = 0 To $excnum
_ArrayAdd($array, _GUICtrlListView_GetItemTextString($ListView1, $I))
Next
Dim $excel, $htest
$excel = _ExcelBookNew(1) ;创建一个新的工作表并设置显示,(0=不可见, 1=可见)
For $I = 0 To UBound($array) - 1 ;返回数组维度的大小
$htest = StringSplit($array[$I], '|') ;以指定分隔符把字符串拆分成若干子串
For $ii = 0 To $htest
_ExcelWriteCell($excel, $htest[$ii], $I, $ii) ;添加一个数据到打开的EXCEL(打开的excel,要添加的数据,要写入的行,要写入的列)
Next
Next
EndIf
EndFunc ;==>_to_EXCEL
Func _clearInput() ;清空输入
GUICtrlSetData($Input1, '')
GUICtrlSetData($Input2, '')
GUICtrlSetData($Input3, '')
GUICtrlSetData($Input4, '')
GUICtrlSetData($Input5, '')
GUICtrlSetData($Input6, '')
GUICtrlSetData($Input7, '')
GUICtrlSetData($Input8, '')
GUICtrlSetData($Input9, '')
GUICtrlSetData($Input10, '')
GUICtrlSetData($Input11, '')
GUICtrlSetData($Input12, '')
$input13 = ''
GUICtrlSetState($Input1, $GUI_FOCUS)
EndFunc ;==>_clearInput
Func _setInput($strID)
Dim $addfld, $RS
$addfld = ObjCreate("ADODB.Connection")
$addfld.Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & $adSource)
$RS = ObjCreate("ADODB.Recordset")
$RS.ActiveConnection = $addfld
$RS.Open("Select " & $T & " From " & $adTable & " WHERE 序号" & " = " & $strID);$Strnspin) ;这里和上面的是一个意思,以第一个关键字读取数据
; $id = $RS.Fields(0).value
;更新数据
GUICtrlSetData($Input1, $RS.Fields(1).value)
GUICtrlSetData($Input2, $RS.Fields(2).value)
GUICtrlSetData($Input3, $RS.Fields(3).value)
GUICtrlSetData($Input4, $RS.Fields(4).value)
GUICtrlSetData($Input5, $RS.Fields(5).value)
GUICtrlSetData($Input6, $RS.Fields(6).value)
GUICtrlSetData($Input7, $RS.Fields(7).value)
GUICtrlSetData($Input8, $RS.Fields(8).value)
GUICtrlSetData($Input9, $RS.Fields(9).value)
GUICtrlSetData($Input10, $RS.Fields(10).value)
GUICtrlSetData($Input11, $RS.Fields(11).value)
GUICtrlSetData($Input12, $RS.Fields(12).value);这里就是把所要修改的数据写到修改窗口对应的编辑框。
$input13 = $RS.Fields(13).value
ConsoleWrite("QRcode = " & $input13 & @CRLF)
If $input13 <> '' Then
GUICtrlSetImage($pic, $dirImage & "\" & $input13)
EndIf
$RS.movenext
$RS.close
$addfld.Close
EndFunc ;==>_setInput
Func _writeInput($idno) ;往数据库中添加新的条目
; If GUICtrlRead($Input1)<>'' And GUICtrlRead($Input3)<>'' And GUICtrlRead($Input4)<>'' And GUICtrlRead($Input5)<>'' And GUICtrlRead($Input6)<>'' And GUICtrlRead($Input7)<>'' And GUICtrlRead($Input8)<>'' And GUICtrlRead($Input9)<>'' And GUICtrlRead($Input10)<>''And GUICtrlRead($Input11)<>'' Then
Dim $read1, $read2, $read3, $read4, $read5, $read6, $read7, $read8, $read9, $read10, $read11, $read12, $read13
Dim $addfld, $sQuery
If GUICtrlRead($Input2) <> '' And GUICtrlRead($Input3) <> '' And GUICtrlRead($Input4) <> '' And GUICtrlRead($Input8) <> '' And GUICtrlRead($Input10) <> '' And GUICtrlRead($Input11) <> '' Then
$read1 = GUICtrlRead($Input1)
$read2 = GUICtrlRead($Input2)
$read3 = GUICtrlRead($Input3)
$read4 = GUICtrlRead($Input4)
$read5 = GUICtrlRead($Input5)
$read6 = GUICtrlRead($Input6)
$read7 = GUICtrlRead($Input7)
$read8 = GUICtrlRead($Input8)
$read9 = GUICtrlRead($Input9)
$read10 = GUICtrlRead($Input10)
$read11 = GUICtrlRead($Input11)
$read12 = GUICtrlRead($Input12)
If $input13 = '' Then
;如果没有QRcode,自动生成
Local $string = "机柜号=" & $read1 _
& ",设备型号=" & $read2 _
& ",设备类型=" & $read3 _
& ",序列号=" & $read4 _
& ",IP地址=" & $read5 _
& ",所属应用=" & $read6 _
& ",信息点=" & $read7 _
& ",操作系统=" & $read8 _
& ",MAC地址=" & $read9 _
& ",机房区域=" & $read10 _
& ",归属=" & $read11
$read13 = _getQRcode($string)
Else
$read13 = $input13
EndIf
$addfld = ObjCreate("ADODB.Connection")
$addfld.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & $adSource) ;要写数据,首先要连接到数据库
If $idno = '' Then
$sQuery = "insert into " & $adTable & " ( " & StringReplace($Combo_Fields, "|", ",") & ",QRcode" & ") values('" & $read1 & "','" & $read2 & "','" & $read3 & "','" & $read4 & "','" & $read5 & "','" & $read6 & "','" & $read7 & "','" & $read8 & "','" & $read9 & "','" & $read10 & "','" & $read11 & "','" & $read12 & "','" & $read13 & "')" ;这里是告诉数据库要写到什么位置。要写什么值,
ConsoleWrite("$sQuery= " & $sQuery & @CRLF)
$addfld.Execute($sQuery)
Else
$sQuery = "update " & $adTable & " set " & "机柜号='" & $read1 & "',设备型号='" & $read2 & "',设备类型='" & $read3 & "',序列号='" & $read4 & "',IP地址='" & $read5 & "',所属应用='" & $read6 & "',信息点='" & $read7 & "',操作系统='" & $read8 & "',MAC地址='" & $read9 & "',机房区域='" & $read10 & "',归属='" & $read11 & "',备注='" & $read12 & "',QRcode='" & $read13 & "' WHERE 序号" & " = " & $idno
ConsoleWrite($sQuery & @CRLF)
$addfld.execute($sQuery) ;执行刚才输入的更新数据。
EndIf
$addfld.close ;写完结束所有的连接
;刷新列表
_readTable()
;清楚数据
_clearInput()
Return True
Else
MsgBox(48, "提示", "红色项目为必输项!")
GUICtrlSetState($Input1, $GUI_FOCUS)
Return False
EndIf
EndFunc ;==>_writeInput
Func _SelectDB($sqlstr) ;查询数据库 ,返回数组,宏保存记录个数
Local $ret = "null"
Dim $addfld, $RS
$addfld = ObjCreate("ADODB.Connection")
$addfld.Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & $adSource)
$RS = ObjCreate("ADODB.Recordset")
$RS.ActiveConnection = $addfld
ConsoleWrite($sqlstr & @CRLF)
$RS.Open($sqlstr)
If $RS.bof And $RS.eof Then
SetError(1)
Else
SetError(0)
$ret = $RS.GetRows
EndIf
$RS.close
$addfld.Close ;读取结束后结束所有的连接,并把缓存的数据清空。
ConsoleWrite($ret & @CRLF)
Return $ret
EndFunc ;==>_SelectDB
Func _Array2String($ary, $col = 0) ;数组转换成字符串,查询用
If Not IsArray($ary) Then Return ""
Local $Str = ""
For $I = 0 To UBound($ary) - 1
$Str &= "|" & $ary[$I][$col]
Next
ConsoleWrite($Str & @CRLF)
Return $Str
EndFunc ;==>_Array2String
Func _config()
Dim $table, $field
Dim $nMsg
Dim $Form_Config, $Group1, $Label_table, $List_field, $Combo_table, $Button_delTable, $Input_Field, $Button_addField
Dim $Group2, $Button_QR_all
Dim $addfld, $RS, $sQuery
#region ### START Koda GUI section ### Form=c:\users\mason\desktop\开发测试设备管理工具\form_config.kxf
$Form_Config = GUICreate("设置参数表字段", 388, 416, 439, 156)
$Group1 = GUICtrlCreateGroup("参数表", 16, 8, 353, 257)
$Label_table = GUICtrlCreateLabel("表名:", 56, 32, 40, 17)
$List_field = GUICtrlCreateList("", 55, 64, 193, 149)
$Combo_table = GUICtrlCreateCombo("", 112, 32, 121, 25, BitOR($CBS_DROPDOWN, $CBS_AUTOHSCROLL))
$Button_delTable = GUICtrlCreateButton("删除", 255, 184, 75, 25)
$Input_Field = GUICtrlCreateInput("", 55, 224, 193, 21)
$Button_addField = GUICtrlCreateButton("添加", 255, 224, 75, 25)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Group2 = GUICtrlCreateGroup("其他", 24, 288, 345, 105)
$Button_QR_all = GUICtrlCreateButton("生成QRcode", 48, 336, 75, 25)
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUISetState(@SW_SHOW)
#endregion ### END Koda GUI section ###
GUICtrlSetData($Combo_table, _Array2String(_SelectDB("Select distinct(字段) From " & $csTable & " where 1= 1")))
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
GUIDelete($Form_Config)
Return
Case $Combo_table
$table = GUICtrlRead($Combo_table)
If $table <> "" Then
GUICtrlSetData($List_field, _Array2String(_SelectDB("Select 值 From " & $csTable & " where 字段= '" & $table & "'")))
EndIf
Case $Button_delTable
$table = GUICtrlRead($Combo_table)
$field = GUICtrlRead($List_field)
If $table = "" Or $field = "" Then
MsgBox(48, '提示:', '请选择要删除表和字段!')
Else
$addfld = ObjCreate("ADODB.Connection")
$addfld.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & $adSource) ;要写数据,首先要连接到数据库
$sQuery = "DELETE FROM " & $csTable & " IN '" & $adSource & "' WHERE 字段" & " = '" & $table & "' and 值 = '" & $field & "'" ;以选中的数据分割的第一个作为关键字,全数据库查找并删除。
ConsoleWrite($sQuery & @CRLF)
$addfld.execute($sQuery)
$addfld.close
;刷新列表
GUICtrlSetData($Input_Field, '')
GUICtrlSetData($List_field, _Array2String(_SelectDB("Select 值 From " & $csTable & " where 字段= '" & $table & "'")))
EndIf
Case $Button_addField
$table = GUICtrlRead($Combo_table)
$field = GUICtrlRead($Input_Field)
If $table = "" Or $field = "" Then
MsgBox(48, '提示:', '请选择要添加表和录入字段!')
Else
_accessAddRecord($adSource, $csTable, $table & "|" & $field, 0)
;刷新列表
GUICtrlSetData($Input_Field, '')
GUICtrlSetData($List_field, _Array2String(_SelectDB("Select 值 From " & $csTable & " where 字段= '" & $table & "'")))
EndIf
Case $Button_QR_all
MsgBox(48, '提示:', '你觉得有必要这么干么?')
EndSwitch
WEnd
GUIDelete()
EndFunc ;==>_config
Func _random_key()
Local $Str = "0123456789abcdefghijklmnopqrstuvwxyz"
Local $max = Random(6, 10, 1)
Local $key = ""
Local $I
For $I = 1 To $max
$key = $key & StringMid($Str, Random(1, 26, 1), 1)
Next
ConsoleWrite("Random key = " & $key & @CRLF)
Return $key
EndFunc ;==>_random_key
;根据窗口大小,调整状态栏
Func _WM_SIZE($hWnd, $iMsg, $iwParam, $ilParam)
#forceref $hWnd, $iMsg, $iwParam, $ilParam
_GUICtrlStatusBar_Resize($StatusBar1)
Return $GUI_RUNDEFMSG
EndFunc ;==>_WM_SIZE
Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
#forceref $hWnd, $iMsg, $iwParam
Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView, $tInfo
$hWndListView = $ListView1
If Not IsHWnd($ListView1) Then $hWndListView = GUICtrlGetHandle($ListView1)
$tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
$hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
$iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
$iCode = DllStructGetData($tNMHDR, "Code")
Switch $hWndFrom
Case $hWndListView
Switch $iCode
Case $NM_DBLCLK ; Sent by a list-view control when the user double-clicks an item with the left mouse button
$tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
_DebugPrint("$NM_DBLCLK" & @LF & "--> hWndFrom:" & @TAB & $hWndFrom & @LF & _
"-->IDFrom:" & @TAB & $iIDFrom & @LF & _
"-->Code:" & @TAB & $iCode & @LF & _
"-->Index:" & @TAB & DllStructGetData($tInfo, "Index") & @LF & _
"-->SubItem:" & @TAB & DllStructGetData($tInfo, "SubItem") & @LF & _
"-->NewState:" & @TAB & DllStructGetData($tInfo, "NewState") & @LF & _
"-->OldState:" & @TAB & DllStructGetData($tInfo, "OldState") & @LF & _
"-->Changed:" & @TAB & DllStructGetData($tInfo, "Changed") & @LF & _
"-->ActionX:" & @TAB & DllStructGetData($tInfo, "ActionX") & @LF & _
"-->ActionY:" & @TAB & DllStructGetData($tInfo, "ActionY") & @LF & _
"-->lParam:" & @TAB & DllStructGetData($tInfo, "lParam") & @LF & _
"-->KeyFlags:" & @TAB & DllStructGetData($tInfo, "KeyFlags"))
; No return value
;ConsoleWrite("myIndex = " & Number(_GUICtrlListView_GetSelectedIndices($ListView1)) & @LF);选中行的第一列
Local $myIndex = _GUICtrlListView_GetItemText($ListView1, DllStructGetData($tInfo, "Index"))
ConsoleWrite("myIndex = " & $myIndex & @LF)
;_editRecoder($myIndex)
EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc ;==>WM_NOTIFY
Func _DebugPrint($s_text, $line = @ScriptLineNumber)
ConsoleWrite( _
"!===========================================================" & @LF & _
"+======================================================" & @LF & _
"-->Line(" & StringFormat("%04d", $line) & "):" & @TAB & $s_text & @LF & _
"+======================================================" & @LF)
EndFunc ;==>_DebugPrint
本帖最后由 netegg 于 2012-2-17 15:06 编辑
第一个问题,用*dispose*先把资源释放了
第二个问题,建议楼主gui一次建立,用显示状态切换
第三个问题,用setfocus试试,没试过大数据量,不清楚会不会滚动到当前项
第四个问题,png2jpg,是gdi的活,好像有专门的函数可以完成
第五个问题,先把图片转成16进制数据,用_WinAPI_CreateStreamOnHGlobal加载 第一个问题,用*dispose*先把资源释放了
第二个问题,建议楼主gui一次建立,用显示状态切换
第三个问题, ...
netegg 发表于 2012-2-17 14:35 http://www.autoitx.com/images/common/back.gif
针对第二个问题,我想知道,为啥这样不行呢? 函数内部建立gui,要重新定义消息队列,大概意思吧 函数内部建立gui,要重新定义消息队列,大概意思吧
netegg 发表于 2012-2-17 18:41 http://www.autoitx.com/images/common/back.gif
能说说怎么做么?
关于问题3我的解决方案是:
Case $Button_add
_editRecoder("")
Local $num = _GUICtrlListView_GetItemCount($ListView1)
_GUICtrlListView_Scroll($ListView1, 0, $num * 18);估计默认行高为18
_GUICtrlListView_SetItemSelected($ListView1, $num - 1);索引从0开始,所以减1
Case $Button_edit
;修改
Dim $Index, $n_id
$Index = Number(_GUICtrlListView_GetSelectedIndices($ListView1))
$n_id = _GUICtrlListView_GetItemText($ListView1, $Index);选中行的第一列
If $n_id = 0 Then
MsgBox(48, '提示:', '请选择要修改的条目!')
Else
_editRecoder($n_id)
_GUICtrlListView_Scroll($ListView1, 0, $Index * 18);估计默认行高为18
_GUICtrlListView_SetItemSelected($ListView1, $Index)
EndIf
这样,新增就滚动到最后,编辑就将修改后的滚动到窗口里。
但是,好像默认行高是16,我设为16总是差一点,18差不多。呵呵呵。 回复 5# zerozha
16是行距----是两条线之间的距离,算上两条分割线,是18 顶起来。希望有人关注。 本帖最后由 netegg 于 2012-2-19 00:03 编辑
回复 7# zerozha
你这样,参考子窗体的消息控制试试看$nMsg = guigetmsg(1) 为什么别的窗口:config about都没有问题呢? 回复 9# zerozha
脚本是顺序执行的,后面的窗口是在config about建立之后才建立的,自然不会有问题 谢谢~从第五个问题学到了新东西~{:face (316):}
页:
[1]