试题随机组合

;-----------------------------------------
/*
[config]
ahk_version=Unicode
icon=word.ico
bin=
*/

;@Ahk2Exe-SetDescription 试题随机组合
;@Ahk2Exe-SetVersion 1.0.1.0
;-----------------------------------------

#NoEnv
#SingleInstance Force
#NoTrayIcon
SetWorkingDir %A_ScriptDir%
SetBatchLines -1
ListLines Off
OnExit, ExitSub

gui_title := "试题随机组合 v1.01"

; =================================
;		读取 ini
; =================================
ini_file := SubStr(A_ScriptName, 1, -4) ".ini"
IniRead, Out_Dir, %ini_file%, Settings, Out_Dir, %A_ScriptDir%

; =================================
;		界面
; =================================
Gui, Color, FCFCFC
Gui_Add_BoldGroupBox("w355 h410 cFF7917 vgbox1", "题源",, "s15")
	Gui, Add, Custom, ClassToolbarWindow32 hwndhToolbar1 vToolbar1 xp+10 yp+25 wp-20 h22 Section 0x1000 0x0800 0x0100 0x20 0x0040 0x0008 0x0004
		himl := __CreateIL("add.png|remove.png|up.png|down.png|clear.png", 16, 16)

		TB1 := New Toolbar(hToolbar1)
		TB1.SetImageList(himl)

		BtnsArray := []
		DefArray1 := [ "src_add=添加:1", "src_del=删除:2(Enabled0)", "", "src_up=上移:3(Enabled0)", "src_down=下移:4(Enabled0)", "", "src_clear=清空:5(Enabled0)"]
		BtnsArray := DefArray1

		TB1.Add("Enabled AutoSize", BtnsArray*)
		TB1.SetDefault("", DefArray1*)

	Gui, Font
	Gui, Add, ListView, xs wp h343 Grid NoSort hwndHLV gLV vLV AltSubmit, 序号|路径
		LV_SetRowHeight(20)
		LV_SetWidth("40|270")
		; Loop, D:zhubajiedoc随机合并题源*.docx
		; 	LV_Add("", A_Index, A_LoopFileFullPath)

; Gui_Add_Picture("arrow_right.png", "x+10 yp+90 w128 h128")
Gui, Add, Button, x+10 yp+90 w128 h128 hwndhBtn vbtn_convert gbtn_convert
	ILButton(hBtn, "convert.png|convert_Hot.png|convert.png|convert_disabled.png|convert.png", 128, 128, "Left", "-3")

GuiControlGet, gbox1, Pos
Gui_Add_BoldGroupBox("x+5 w355 h410 cFF7917 y" gbox1Y, "输出",, "s15")
	Gui, Add, Text, xp+10 yp+30 Section, 格式:
	Gui, Add, Radio, x+10 Checked vOut_Ext_Doc, DOC
	Gui, Add, Radio, x+10 vOut_Ext_Txt, TXT

	Gui, Add, Text, xs, 路径: (可选)
		Gui, Add, Edit, xs+10 w280 r1 vOut_Dir, %Out_Dir%
		Gui, Add, Button, x+5 hp gbtn_browse, ...

	Gui, Add, Text, xs, 文件名: (每行一个)
		Gui, Add, Edit, xs+10 w325 h255 vOut_NameList, 

Gui, Add, StatusBar, -Theme BackgroundDDE1E5, %A_Space%
	SB_SetText("", 1, 1)

Gui, Show,, %gui_title%

OnMessage("0x111", "TB_Messages")

LV_AltColoredRows.OnMessage()
LV_AltColoredRows.Attach(HLV)
Return

; =================================
;		添加
; =================================
src_add:
	; 选择文件
	Gui, +OwnDialogs
	FileSelectFile, SelectedFiles, M1, %A_ScriptDir%, 请选择 Word 文件, Word文件(*.docx;*.doc)
	if !SelectedFiles
		Return

	; 添加到列表
	LVTotal       := LV_GetCount()
	SelectedFiles := StrSplit(SelectedFiles, "`n")
	Loop, % SelectedFiles.MaxIndex() - 1
		LV_Add( "", ++LVTotal, SelectedFiles[1] "" SelectedFiles[A_Index+1] )

	; 启用【清空】按钮
	TB1.ModifyButton(7, "Enable", True)
Return

; =================================
;		删除
; =================================
src_del:
	LV_Rows.Delete()
	Goto, ModifyIndex
Return

; =================================
;		上移、下移
; =================================
src_up:
src_down:
	LV_Rows.Move( (A_ThisLabel = "src_up") ? True : False )

; 纠正序号
ModifyIndex:
	Loop, % LV_GetCount()
		LV_Modify(A_Index, "Col1", A_Index)
Return

; =================================
;		清空
; =================================
src_clear:
	if !LV_GetCount()
		Return

	MsgBox2("确定要清空列表吗?", "确认清空", 36)
	IfMsgBox, No
		Return

	LV_Delete()
	SB_SetText("")
	Loop, 6
		TB1.ModifyButton(A_Index+1, "Enable", False)
Return

; =================================
;    ...
; =================================
;
btn_browse:
	FileSelectFolder, Out_Dir, *%A_ScriptDir%, 1, 选择保存目录
	if Out_Dir
		GuiControl,, Out_Dir, % Out_Dir
return

; =================================
;		生成
; =================================
btn_convert:
	Gui, Submit, NoHide

	if !LV_GetCount()
	{
		MsgBox2("请先导入题源", "提示", 48)
		Return
	}

	GuiControl, Disable, btn_convert

	; 格式化文件名列表
	  Out_NameList := RegExReplace(Out_NameList, "[\/:*?x22|]", "_")	; 替换掉不能作为文件名的字符
	, Out_NameList := RegExReplace(Out_NameList, "[rn]+", "`n")			; 多个换行符替换为一个
	, Out_NameList := RegExReplace(Out_NameList, "m`a)^[ t]+|[ t]$")		; 去掉每行开头和结尾的空格
	, Out_NameList := Trim(Out_NameList, "`n")								; 去掉首尾的换行符
	, Out_NameList := (Out_NameList = "") ? A_Now : Out_NameList 			; 为空则用当前时间
	, Out_NameList := RegExReplace(Out_NameList, "m`ai).(txt|doc|docx)$")	; 去掉每行结尾的后缀
	, Out_NameList := StrSplit(Out_NameList, "`n")							; 转为 array

	; 判断输出文件夹
	  Out_Dir := Trim(Out_Dir)
	, Out_Dir := (Out_Dir = "") ? A_ScriptDir : Out_Dir
	IfNotExist, %Out_Dir%
		FileCreateDir, %Out_Dir%

	; 转换为 html
	ConvertedList := {} ; 结构为 [ {dir:xx, filepath:xx}, {dir:xx, filepath:xx}, ... ]
	Loop, % ( LVTotal := LV_GetCount() )
	{
		SB_SetText("[" A_Index "/" LVTotal "] 转换为 html...")
		LV_GetText(DocPath, A_Index, 2)
		obj := doc2html(DocPath)
		ConvertedList.Insert(obj)
	}

	; 提取内容
	htmlContents := {} ; 结构为 [ {head:xx, content:[xx, xx, ...]}, {head:xx, content:[xx, xx, ...]}, ... ]
	Loop, % ConvertedList.MaxIndex()
	{
		SB_SetText("[" A_Index "/" LVTotal "] 提取内容...")

		; 读取整个 html
		FileRead, ThisHtml, % ConvertedList[A_Index]["filepath"]

		; 把图片路径替换为完整路径
		ThisHtml := RegExReplace(ThisHtml, "i)(src=x22)([^/]+.files)/", "$1" ConvertedList[A_Index]["dir"] "$2")

		; head
		head := StrX(ThisHtml, "", "", 0, "", 1, 0, "")
		ConvertedList[A_Index]["head"] := head

		; 内容
		N := "", FileNum := A_Index, oContent := []
		While, content := StrX(ThisHtml, "【【", N, 0, "】】", 1, 0, N)
		{
			StringReplace, content, content, 【【
			StringReplace, content, content, 】】
			oContent.Insert(content)
		}
		ConvertedList[A_Index]["content"] := oContent
	}

	; 随机组合
	Loop, % ( OutTotal := Out_NameList.MaxIndex() )
	{
		SB_SetText("[" A_Index "/" OutTotal "] 随机组合...")

		; 创建一个 Word 文档
		oWord    := ComObjCreate("Word.Application")
		Document := oWord.Documents.Add

		Loop, % ConvertedList.MaxIndex()
		{
			; 随机选一个
			Random, rNum, 1, % ConvertedList[A_Index]["content"].MaxIndex()

			; 构建 HTML 剪贴板
			ThisHtml := ConvertedList[A_Index]["head"] "`n" ConvertedList[A_Index]["content"][rNum]
			WinClip.Clear()
			WinClip.SetHTML( ThisHtml )

			; 粘贴到 Word
			oWord.Selection.Paste
		}

		; oWord.Visible := 1, oWord.Activate

		; 保存
		; wdFormatDocumentDefault := 16, wdFormatEncodedText := 7
		if Out_Ext_Doc
			Document.SaveAs(Out_Dir "" Out_NameList[A_Index] ".docx", 16)
		else
			Document.SaveAs(Out_Dir "" Out_NameList[A_Index] ".txt", 7)
		Document.Close()
		oWord.Quit
	}

	GuiControl, Enable, btn_convert
	SB_SetText("生成完毕")
	MsgBox2("已生成 " OutTotal " 个文件", "完毕")
Return

; =================================
;		列表动作
; =================================
LV:
	if (A_GuiEvent = "D")
	{
		LV_Rows.Drag()
		Gosub, ModifyIndex
		GuiControl, +Redraw, LV
	}
	else if (A_GuiEvent = "I")
	{
		_Enable := LV_GetNext() ? True : False
		Loop, 4
			TB1.ModifyButton(A_Index+1, "Enable", _Enable)
	}
Return

; =================================
;		拖拽文件到界面
; =================================
GuiDropFiles:
	LVTotal := LV_GetCount()
	Loop, Parse, A_GuiEvent, `n, `r
	{
		SplitPath, A_LoopField,,, ThisExt
		if ThisExt in doc,docx
			LV_Add("", ++LVTotal, A_LoopField)
	}

	; 启用【清空】按钮
	if LVTotal
		TB1.ModifyButton(7, "Enable", True)
Return

; =================================
;		退出
; =================================
GuiClose:
ExitApp

ExitSub:
	IniWrite, % Out_Dir, %ini_file%, Settings, Out_Dir
	FileRemoveDir, %A_Temp%.doc2html, 1
ExitApp

; =========================================== 以下是函数 ===========================================
TB_Messages(wParam, lParam)
{
	Global ; Function (or at least the Handles) must be global.
	TB1.OnMessage(wParam) ; Handles messages from TB1
}

Gui_Add_BoldGroupBox(Options = "", Text = "", GuiName = 1, NewFont="s12", OldFont="s12")
{
	global

	Gui, %GuiName%:Font, bold %NewFont%
	Gui, %GuiName%:Add, GroupBox, %Options%, %Text%
	Gui, %GuiName%:Font
	Gui, %GuiName%:Font, %OldFont%
}

LV_SetWidth(WidthList)
{
	WidthList := RegExReplace(WidthList, "D+", "#")
	WidthList := Trim(WidthList, "#")
	loop, parse, WidthList, #
		LV_ModifyCol(A_Index, A_LoopField)
}

LV_SetRowHeight(Height)
{
	LV_SetImageList( DllCall( "ImageList_Create", Int,2, Int, Height, Int,0x18, Int,1, Int,1 ), 1 )
}

__CreateIL(images, cx, cy)
{
    himl := DllCall("ImageList_Create", "Int",cx, "Int",cy, "UInt",0x20, "Int",1, "Int",5, "UPtr")

    Loop, Parse, images, |
    {
        StringReplace, name, A_LoopField, ., _, a
        hicon := Create_%name%()
        DllCall("ImageList_AddIcon", "Ptr",himl, "Ptr",hicon)
        DllCall("DestroyIcon", "Ptr", hicon)
    }

    Return, himl
}

Gui_Add_Picture(file, Options, hWnd = "hPic")
{
	global
	StringReplace, name, file, ., _, a
	Gui, Add, Picture, 0xE %Options% hWnd%hWnd%
	SendMessage, 0x172, 0x0, % ( Create_%name%() ), , % "ahk_id " %hWnd%
}

MsgBox2(Text, Title = "", Options = "")
{
	gui, +OwnDialogs
	MsgBox, % Options, %Title%, %Text%
}

StrX(ByRef H,  BS="",BO=0,BT=1,   ES="",EO=0,ET=1,  ByRef N="" ) { ;    | by Skan | 19-Nov-2009
Return SubStr(H,P:=(((Z:=StrLen(ES))+(X:=StrLen(H))+StrLen(BS)-Z-X)?((T:=InStr(H,BS,0,((BO
 
#Include 
#Include 
#Include 
#Include 
#Include 

#Include 
#Include 

#Include 
#Include 
#Include 
#Include 

#Include 
#Include 

给TA捐赠
共{{data.count}}人
人已捐赠
其他

设置色温

2020-3-9 5:34:44

其他

说说采集

2020-3-9 5:36:44

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索