au3制作透明窗体可移动调节大小并记忆位置

利用au3制作的一个小工具,都是借鉴其他人的劳动成果改编的,再次感谢他们的无私奉献!

功能:利用au3的gui绘图功能?实现的透明红色边框显示,最初做这个是给led拼接屏框定播放范围的,可以调节框的大小,以及移动位置,可以生成配置文件set_windows.ini。其中设置state=1,会影藏调节按钮,只显示红色边框,并带抖动。

au3透明框体

源码附上(代码冗余请勿见笑哈):

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <WinAPI.au3>
#include <WinAPIFiles.au3>
#NoTrayIcon
#Region ### START Koda GUI section ### Form=
;宽度,高度,x,y
;$Form1 = GUICreate("Form1", 800, 600, 20, 200,$WS_POPUP, $WS_EX_LAYERED,WinGetHandle("Program Manager"))
;$x=50
;$y=50

;$width=400
;$hight=400

$width = IniRead("set_windows.ini", "windows", "w", -123719)
$hight = IniRead("set_windows.ini", "windows", "h", -123719)
$x = IniRead("set_windows.ini", "windows", "l", -123719)
$y = IniRead("set_windows.ini", "windows", "t", -123719)
$state = IniRead("set_windows.ini", "state", "state", -123719)
If $width = -123719 Or $hight = -123719 Or $x = -123719 Or $y = -123719 Or $state = -123719 Then
	_write_ini(400, 400, 50, 50, 1)
	;_write_ini(400,400,50,50,-123719)
	$width = IniRead("set_windows.ini", "windows", "w", -123719)
	$hight = IniRead("set_windows.ini", "windows", "h", -123719)
	$x = IniRead("set_windows.ini", "windows", "l", -123719)
	$y = IniRead("set_windows.ini", "windows", "t", -123719)
	$state = IniRead("set_windows.ini", "state", "state", -123719)
EndIf

;$Form1 = GUICreate("Form1", $width, $hight, $x, $y,$WS_POPUP, $WS_EX_LAYERED,WinGetHandle("Program Manager"))

$Form1 = GUICreate("红色范围框", $width + 200, $hight + 200, $x, $y, $WS_POPUP, $WS_EX_LAYERED)
;$Form1 = GUICreate("Form1", $width+200, $hight+200,$x, $y)
GUISetBkColor(0x010101)

If $state <> 0 Then
	$left = GUICtrlCreateButton("左", $width / 2 - 200, $y + 100, 50, 50)
	GUICtrlSetBkColor(-1,0xFF69B4)
	$right = GUICtrlCreateButton("右", $width / 2 + 100 - 200, $y + 100, 50, 50)
	GUICtrlSetBkColor(-1,0xFF69B4)
	$up = GUICtrlCreateButton("上", $width / 2 + 50 - 200, $y + 50, 50, 50)
	GUICtrlSetBkColor(-1,0xFF69B4)
	$down = GUICtrlCreateButton("下", $width / 2 + 50 - 200, $y + 150, 50, 50)
    GUICtrlSetBkColor(-1,0xFF69B4)
	$width_m = GUICtrlCreateButton("宽", $width / 2, $y + 100, 50, 50)
	GUICtrlSetBkColor(-1,0xFF69B4)
	$width_l = GUICtrlCreateButton("窄", $width / 2 + 100, $y + 100, 50, 50)
	GUICtrlSetBkColor(-1,0xFF69B4)
	$hight_m = GUICtrlCreateButton("高", $width / 2 + 50, $y + 50, 50, 50)
	GUICtrlSetBkColor(-1,0xFF69B4)
	$hight_l = GUICtrlCreateButton("低", $width / 2 + 50, $y + 150, 50, 50)
	GUICtrlSetBkColor(-1,0xFF69B4)

Else
	;$left=$right=$up=$down=$width_m=$width_l=$hight_m=$hight_l=0
	$left = GUICtrlCreateButton("左", $width / 2 - 200, $y + 100, 1, 1)
	$right = GUICtrlCreateButton("右", $width / 2 + 100 - 200, $y + 100, 1, 1)
	$up = GUICtrlCreateButton("上", $width / 2 + 50 - 200, $y + 50, 1, 1)
	$down = GUICtrlCreateButton("下", $width / 2 + 50 - 200, $y + 150, 1, 1)

	$width_m = GUICtrlCreateButton("宽", $width / 2, $y + 100, 1, 1)
	$width_l = GUICtrlCreateButton("窄", $width / 2 + 100, $y + 100, 1, 1)
	$hight_m = GUICtrlCreateButton("高", $width / 2 + 50, $y + 50, 1, 1)
	$hight_l = GUICtrlCreateButton("低", $width / 2 + 50, $y + 150, 1, 1)
EndIf

;$group1=GUICtrlCreateGroup("",50,50,$width-222,$hight-222)
;GUICtrlSetDefBkColor(0xFF0000)
;GUICtrlSetBkColor(-1, 0x0000ff)

#cs   图片做边框
	;   left,top,width,higt
	$Pic1 = GUICtrlCreatePic("C:\1.jpg", 1, 1, $width-1, 2,BitOR($SS_NOTIFY,$WS_GROUP,$WS_CLIPSIBLINGS)) ;上方横线
	$Pic2 = GUICtrlCreatePic("C:\1.jpg", 1, $hight-2, $width-1, 2,BitOR($SS_NOTIFY,$WS_GROUP,$WS_CLIPSIBLINGS)) ;下方横线

	$Pic3 = GUICtrlCreatePic("C:\1.jpg", 1, 1, 2,$hight-1,BitOR($SS_NOTIFY,$WS_GROUP,$WS_CLIPSIBLINGS)) ;左方竖线
	$Pic4 = GUICtrlCreatePic("C:\1.jpg", $width-2, 1, 2, $hight-1,BitOR($SS_NOTIFY,$WS_GROUP,$WS_CLIPSIBLINGS)) ;右方竖线
#ce

;$Label1 = GUICtrlCreateLabel("██████████", 8, 120, 164, 17)
;GUICtrlSetFont(-1, 9, 400, 4)
;GUICtrlSetColor(-1, 0x0000ff)


#cs
	;                       x,   y,  w  ,h
	GUICtrlCreateLabel("", 1, 1, 100, $hight, $SS_ETCHEDVERT);竖线
	GUICtrlSetColor(-1, 0x0000ff)
	GUICtrlCreateLabel("", $width-1, 1, 10,$hight, $SS_ETCHEDVERT);竖线2
	GUICtrlSetColor(-1, 0x0000ff)
	GUICtrlCreateLabel("", 1, 1, $width, 10, $SS_ETCHEDHORZ);横线
	GUICtrlSetColor(-1, 0x0000ff)
	GUICtrlCreateLabel("", 1, $hight-1, 10, 1, $SS_ETCHEDHORZ);横线
	GUICtrlSetColor(-1, 0x0000ff)
#ce


#cs
	$hDC = _WinAPI_GetWindowDC($form1) ;
	$hPen = _WinAPI_CreatePen($PS_SOLID, 1, 0xFF)
	$obj_orig = _WinAPI_SelectObject($hDC, $hPen)
	$111=_WinAPI_DrawLine ( $hDC, 10, 10, $width, $hight )

	;MsgBox(0,0,$111)
#ce


GUICtrlSetCursor(-1, 0)


_API_SetLayeredWindowAttributes($Form1, 0x010101)
WinSetOnTop($Form1, "Form1", 1)

;_WinToTop($form1)
GUISetState(@SW_SHOW)
ShowCross($width, $HIGHT, $hight, 2, 0xFF) ;, 3000)

#EndRegion ### END Koda GUI section ###


;WinMove

;$y1=$y
;$x1=$x
$t = 1
While 1
	$nMsg = GUIGetMsg()
	
	If $state <> 0 Then
		Switch $nMsg
			Case $GUI_EVENT_CLOSE
				Exit
				
			Case $left
				$x = $x - 1
				WinMove($Form1, "", $x, $y)
				_write_ini($width, $hight, $x, $y, -123719)
			Case $right
				$x = $x + 10
				WinMove($Form1, "", $x, $y)
				_write_ini($width, $hight, $x, $y, -123719)
			Case $up
				$y = $y - 1
				WinMove($Form1, "", $x, $y)
				_write_ini($width, $hight, $x, $y, -123719)
			Case $down
				$y = $y + 10
				WinMove($Form1, "", $x, $y)
				_write_ini($width, $hight, $x, $y, -123719)
			Case $width_m
				$width = $width + 10
				WinMove($Form1, "", $x, $y, $width + 200, $hight + 200)
				_write_ini($width, $hight, $x, $y, -123719)
				ShowCross($width, $HIGHT, $hight, 2, 0xFF) ;, 3000)
			Case $width_l
				$width = $width - 1
				WinMove($Form1, "", $x, $y, $width + 200, $hight + 200)
				_write_ini($width, $hight, $x, $y, -123719)
				ShowCross($width, $HIGHT, $hight, 2, 0xFF) ;, 3000)
			Case $hight_m
				$hight = $hight + 10
				WinMove($Form1, "", $x, $y, $width + 200, $hight + 200)
				_write_ini($width, $hight, $x, $y, -123719)
				ShowCross($width, $HIGHT, $hight, 2, 0xFF) ;, 3000)
			Case $hight_l
				$hight = $hight - 1
				WinMove($Form1, "", $x, $y, $width + 200, $hight + 200)
				_write_ini($width, $hight, $x, $y, -123719)
				ShowCross($width, $HIGHT, $hight, 2, 0xFF) ;, 3000)
				
				#cs
					Case Else
					Sleep(100)
					If $t=1 Then

					WinMove($form1,"",$x,$y)
					$t=0
					Else

					WinMove($form1,"",$x+1,$y+1)
					$t=1
					EndIf
				#ce
		EndSwitch
		
	Else
		
		Switch $nMsg
			Case $GUI_EVENT_CLOSE
				Exit

			Case Else
				Sleep(100)
				If $t = 1 Then
					
					WinMove($Form1, "", $x, $y)
					$t = 0
				Else
					
					WinMove($Form1, "", $x + 1, $y + 1)
					$t = 1
				EndIf
		EndSwitch
	EndIf
WEnd

Func _API_SetLayeredWindowAttributes($hwnd, $i_transcolor, $Transparency = 255, $isColorRef = False)

	Local Const $AC_SRC_ALPHA = 1
	Local Const $ULW_ALPHA = 2
	Local Const $LWA_ALPHA = 0x2
	Local Const $LWA_COLORKEY = 0x1
	If Not $isColorRef Then
		$i_transcolor = Hex(String($i_transcolor), 6)
		$i_transcolor = Execute('0x00' & StringMid($i_transcolor, 5, 2) & StringMid($i_transcolor, 3, 2) & StringMid($i_transcolor, 1, 2))
	EndIf
	Local $Ret = DllCall("user32.dll", "int", "SetLayeredWindowAttributes", "hwnd", $hwnd, "long", $i_transcolor, "byte", $Transparency, "long", $LWA_COLORKEY + $LWA_ALPHA)
	Select
		Case @error
			Return SetError(@error, 0, 0)
		Case $Ret[0] = 0
			Return SetError(4, 0, 0)
		Case Else
			Return 1
	EndSelect
EndFunc   ;==>_API_SetLayeredWindowAttributes


Func _WinToTop($Whd)
	
	Local $XYPos
	If Not WinExists($Whd) Then Return
	$XYPos = WinGetPos($Whd)
	DllCall("user32.dll", "long", "SetWindowPos", "hwnd", $Whd, "long", -1, "long", $XYPos[0], "long", $XYPos[1], "long", $XYPos[2], "long", $XYPos[3], "long", 0x40)
EndFunc   ;==>_WinToTop

Func ShowCross($start_x, $start_y, $length, $width, $color)
	Local $hDC, $hPen, $obj_orig

	$hDC = _WinAPI_GetWindowDC($Form1) ; 全部屏幕的场景(桌面)
	$hPen = _WinAPI_CreatePen($PS_SOLID, $width, $color)
	$obj_orig = _WinAPI_SelectObject($hDC, $hPen)

	;  _WinAPI_DrawLine($hDC, $start_x - $length, $start_y, $start_x - 5, $start_y) ; 水平左
	_WinAPI_DrawLine($hDC, 0, 0, $start_x, 1) ;上横线
	_WinAPI_DrawLine($hDC, 1, $hight, $start_x, $hight) ;下横线
	_WinAPI_DrawLine($hDC, 0, 0, 1, $start_y) ;左竖线
	_WinAPI_DrawLine($hDC, $start_x, 1, $start_x, $start_y) ;右竖线
	; _WinAPI_LineTo ( $hDC, $WIDTH, $HIGHT )

	;   _WinAPI_DrawLine($hDC, $start_x + $length, $start_y, $start_x + 5, $start_y) ; 水平右
	;  _WinAPI_DrawLine($hDC, $start_x, $start_y - $length, $start_x, $start_y - 5) ; 垂直上
	;  _WinAPI_DrawLine($hDC, $start_x, $start_y + $length, $start_x, $start_y + 5) ; 垂直下
	;  _WinAPI_MoveTo($hDC, $start_x, $start_y + $length)
	;  _WinAPI_LineTo($hDC, $start_x, $start_y + 5)

	;   Sleep($time) ; 穿过屏幕几秒

	; 刷新桌面(清扫穿过)
	_WinAPI_RedrawWindow(_WinAPI_GetDesktopWindow(), 0, 0, $RDW_INVALIDATE + $RDW_ALLCHILDREN)

	; 清除资源
	_WinAPI_SelectObject($hDC, $obj_orig)
	_WinAPI_DeleteObject($hPen)
	_WinAPI_ReleaseDC(0, $hDC)
EndFunc   ;==>ShowCross

Func _write_ini($w, $h, $x, $y, $state)



	IniWrite("set_windows.ini", "windows", "w", $w)
	IniWrite("set_windows.ini", "windows", "h", $h)

	IniWrite("set_windows.ini", "windows", "l", $x)
	IniWrite("set_windows.ini", "windows", "t", $y)

	If $state <> -123719 Then IniWrite("set_windows.ini", "state", "state", $state)
IniWrite("set_windows.ini","Readme","说明"," state值设置为0可以隐藏调节按钮")
EndFunc   ;==>_write_ini

 

基于互联网精神,在注明出处的前提下本站文章可自由转载!

本文链接:https://ranjuan.cn/au3制作透明窗体可移动调节大小并记忆位置/

赞赏

微信赞赏支付宝赞赏

poeam20171226
get-aliyun-oss-appkeysecret
php中odbc方式连接sqlserver数据库