I need help with this code because I am not a programmer, can anyone add F4 to change the color Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Sarhad
Posts: 7
Joined: 05 May 2024, 13:58

I need help with this code because I am not a programmer, can anyone add F4 to change the color

05 May 2024, 14:11

Code: Select all

;===Auto-execute========================================================================
/*
How to use:
- Click, drag, release F1 to draw new line (multiple lines supported)
- Drag line start or end point to move it
- Press F2 to delete last line
- Press F3 to delete all lines
- Press Esc to exit
*/
CoordMode, mouse,Screen
MyLines := new c_DrawLinesOnScreen()
;MyLines := new c_DrawLinesOnScreen({PenColor: "88ff0000", PenWidth: 8})   ; example how to override defaults ...
return


;===Hotkeys=============================================================================
F1::MyLines.DrawLine() ; click, drag, release
F2::MyLines.DeleteLine() ; if LineNum is blank, last line will be deleted
F3::MyLines.DeleteAllLines()

Esc::ExitApp
#If (MyLines.IsClickOnLineEnd() = 1)
LButton::MyLines.MoveLine()
#If


;===Functions===========================================================================
#Include Gdip.ahk				; by Tic	www.autohotkey.com/community/viewtopic.php?f=2&t=32238


;===Classes=============================================================================
Class c_DrawLinesOnScreen {      ; demo by Learning one. http://www.autohotkey.com/community/viewtopic.php?p=572041#p572041
	Lines := []
	__New(o="") {
		Gui, New, +Hwndhwnd
		Gui %hwnd%: -Caption +E0x80000 +ToolWindow +AlwaysOnTop +OwnDialogs +Hwndhwnd
		Gui %hwnd%: Show, NA
		hbm := CreateDIBSection(A_ScreenWidth, A_ScreenHeight), hdc := CreateCompatibleDC(), obm := SelectObject(hdc, hbm)      
		G := Gdip_GraphicsFromHDC(hdc)
		if (G < 1)
			pToken := Gdip_Startup(), G := Gdip_GraphicsFromHDC(hdc), this.pToken := pToken
		Gdip_SetSmoothingMode(G, 4), Gdip_SetInterpolationMode(G, 7)
		PenColor := (o.PenColor) ? o.PenColor : "ff0000ff", PenWidth := (o.PenWidth) ? o.PenWidth : 3
		pPen := Gdip_CreatePen("0x" PenColor, PenWidth)
		UpdateLayeredWindow(hwnd, hdc, 0, 0, A_ScreenWidth, A_ScreenHeight)
		this.hwnd := hwnd, this.hbm := hbm, this.hdc := hdc, this.obm := obm, this.G := G, this.pPen := pPen
	}
	DrawLine() {
		Hotkey := RegExReplace(A_ThisHotkey, (A_IsUnicode = 1) ? "(*UCP)^(\w* & |\W*)" : "^(\w* & |\W*)")
		MouseGetPos, StartX, StartY
		While (GetKeyState(Hotkey, "p") = 1) {
			Sleep, 20
			MouseGetPos, EndX, EndY
			Gdip_GraphicsClear(this.G)
			For LineNum,pLine in this.Lines   ; draw all lines in collection
				Gdip_DrawLine(this.G, this.pPen, pLine.1, pLine.2, pLine.3, pLine.4)
			Gdip_DrawLine(this.G, this.pPen, StartX, StartY, EndX, EndY)   ; draw new line
			UpdateLayeredWindow(this.hwnd, this.hdc)
		}
		this.Lines.Insert([ StartX, StartY, EndX, EndY])   ; insert new line in collection
	}
	MoveLine() {
		Hotkey := RegExReplace(A_ThisHotkey, (A_IsUnicode = 1) ? "(*UCP)^(\w* & |\W*)" : "^(\w* & |\W*)")
		StartX := this.LineToMove.2, StartY := this.LineToMove.3
		this.Lines.Remove(this.LineToMove.1)   ; remove LineToMove from collection and treat is as new line
		While (GetKeyState(Hotkey, "p") = 1) {
			Sleep, 20
			MouseGetPos, EndX, EndY
			Gdip_GraphicsClear(this.G)
			For LineNum,pLine in this.Lines   ; draw all lines in collection
				Gdip_DrawLine(this.G, this.pPen, pLine.1, pLine.2, pLine.3, pLine.4)
			Gdip_DrawLine(this.G, this.pPen, StartX, StartY, EndX, EndY)   ; draw new line
			UpdateLayeredWindow(this.hwnd, this.hdc)
		}
		this.Lines.Insert(this.LineToMove.1, [ StartX, StartY, EndX, EndY]), this.LineToMove := ""   ; insert new line in collection, delete LineToMove info
	}
	DeleteAllLines() {
		this.Lines := [], this.LineToMove := "", Gdip_GraphicsClear(this.G), UpdateLayeredWindow(this.hwnd, this.hdc)
	}
	DeleteLine(LineNum="") {	; if LineNum is blank, last line will be deleted
		if (this.Lines.MaxIndex() = "")   ; no lines in collection
			return
		LineNum := (LineNum = "") ? this.Lines.MaxIndex() : LineNum	; last or specified
		this.Lines.Remove(LineNum)   ; remove from collection
		Gdip_GraphicsClear(this.G)
		For LineNum,pLine in this.Lines   ; draw all lines in collection
			Gdip_DrawLine(this.G, this.pPen, pLine.1, pLine.2, pLine.3, pLine.4)
		UpdateLayeredWindow(this.hwnd, this.hdc)
	}
	IsClickOnLineEnd(radius=6) {
		if (this.Lines.MaxIndex() = "")   ; no lines in collection
			return
		MouseGetPos, x, y
		TotalLines := this.Lines.MaxIndex()
		Loop % TotalLines   ; Z-order.   Reverse Z-order: For LineNum,pLine in this.Lines
		{
			LineNum := TotalLines-A_Index+1, pLine := this.Lines[LineNum]
			if (this.IsInCircle(pLine.1, pLine.2, x, y, radius) = 1) {
				this.LineToMove := [LineNum, pLine.3, pLine.4]   ; LineNum | Coord3 | Coord4
				return 1
			}
			else if (this.IsInCircle(pLine.3, pLine.4, x, y, radius) = 1) {
				this.LineToMove := [LineNum, pLine.1, pLine.2]   ; LineNum | Coord1 | Coord2
				return 1
			}
		}
		this.LineToMove := ""
	}
	IsInCircle(Xstart, Ystart, Xend, Yend, radius) {
		a := Abs(Xend-Xstart), b := Abs(Yend-Ystart), c := Sqrt(a*a+b*b)
		Return (c<radius) ? 1:0   ; if in circle returns 1, else 0
	}
	__Delete() {
		Gdip_DeletePen(this.pPen)
		Gdip_DeleteGraphics(this.G)
		SelectObject(this.hdc, this.obm), DeleteObject(this.hbm), DeleteDC(this.hdc)
		if (this.pToken != "")
			Gdip_Shutdown(this.pToken)
	}
}
[Mod edit: Fixed codebox. The tags should go around the code.]
Sarhad
Posts: 7
Joined: 05 May 2024, 13:58

Re: I need help with this code because I am not a programmer, can anyone add F4 to change the color

05 May 2024, 14:29

ive been using this tool for a long time to prepare for my IELTS exam, as well as annotation tool for my PDF files. It is easy to use, but it lacks changing the line color for me. Is it possible for an expert or someone with the ability to add this function? I would be very grateful.
User avatar
boiler
Posts: 17706
Joined: 21 Dec 2014, 02:44

Re: I need help with this code because I am not a programmer, can anyone add F4 to change the color  Topic is solved

05 May 2024, 17:42

See how you like this. Press F4 and it will flash up the current color as you cycle through them. You can only drag the ends and use the other hotkeys for the color that you're currently in because they're each their own "layer". You can add or subtract as many colors as you'd like by editing the LineColors array assignment line.

Code: Select all

;===Auto-execute========================================================================
/*
How to use:
- Click, drag, release F1 to draw new line (multiple lines supported)
- Drag line start or end point to move it
- Press F2 to delete last line
- Press F3 to delete all lines
- Press Esc to exit
*/

ColorIndex := 1
LineColors := ["0000ff","ff0000", "00ff00","ffff00","00ffff","ff00ff"]
CoordMode, mouse,Screen
for Each, LineColor in LineColors
	MyLines%A_Index% := new c_DrawLinesOnScreen({PenColor: "ff" LineColor, PenWidth: 3})
;MyLines := new c_DrawLinesOnScreen({PenColor: "88ff0000", PenWidth: 8})   ; example how to override defaults ...

Gui, ColHwnd:+HWndColHwnd -Caption -Border +ToolWindow
Gui, ColHwnd:Color, % LineColors[ColorIndex]
return


;===Hotkeys=============================================================================
F1::MyLines%ColorIndex%.DrawLine() ; click, drag, release
F2::MyLines%ColorIndex%.DeleteLine() ; if LineNum is blank, last line will be deleted
F3::MyLines%ColorIndex%.DeleteAllLines()
F4::
	ColorIndex := ColorIndex = LineColors.Count() ? 1 : (ColorIndex + 1)
	Gui, ColHwnd:Color, % LineColors[ColorIndex]
	SoundBeep, 800
	Gui, ColHwnd:Show, w200 h150
	Sleep, 500
	Gui, ColHwnd:Cancel
return

Esc::ExitApp
#If (MyLines%ColorIndex%.IsClickOnLineEnd() = 1)
LButton::MyLines%ColorIndex%.MoveLine()
#If


;===Functions===========================================================================
#Include Gdip.ahk				; by Tic	www.autohotkey.com/community/viewtopic.php?f=2&t=32238


;===Classes=============================================================================
Class c_DrawLinesOnScreen {      ; demo by Learning one. http://www.autohotkey.com/community/viewtopic.php?p=572041#p572041
	Lines := []
	__New(o="") {
		Gui, New, +Hwndhwnd
		Gui %hwnd%: -Caption +E0x80000 +ToolWindow +AlwaysOnTop +OwnDialogs +Hwndhwnd
		Gui %hwnd%: Show, NA
		hbm := CreateDIBSection(A_ScreenWidth, A_ScreenHeight), hdc := CreateCompatibleDC(), obm := SelectObject(hdc, hbm)      
		G := Gdip_GraphicsFromHDC(hdc)
		if (G < 1)
			pToken := Gdip_Startup(), G := Gdip_GraphicsFromHDC(hdc), this.pToken := pToken
		Gdip_SetSmoothingMode(G, 4), Gdip_SetInterpolationMode(G, 7)
		PenColor := (o.PenColor) ? o.PenColor : "ff0000ff", PenWidth := (o.PenWidth) ? o.PenWidth : 3
		pPen := Gdip_CreatePen("0x" PenColor, PenWidth)
		UpdateLayeredWindow(hwnd, hdc, 0, 0, A_ScreenWidth, A_ScreenHeight)
		this.hwnd := hwnd, this.hbm := hbm, this.hdc := hdc, this.obm := obm, this.G := G, this.pPen := pPen
	}
	DrawLine() {
		Hotkey := RegExReplace(A_ThisHotkey, (A_IsUnicode = 1) ? "(*UCP)^(\w* & |\W*)" : "^(\w* & |\W*)")
		MouseGetPos, StartX, StartY
		While (GetKeyState(Hotkey, "p") = 1) {
			Sleep, 20
			MouseGetPos, EndX, EndY
			Gdip_GraphicsClear(this.G)
			For LineNum,pLine in this.Lines   ; draw all lines in collection
				Gdip_DrawLine(this.G, this.pPen, pLine.1, pLine.2, pLine.3, pLine.4)
			Gdip_DrawLine(this.G, this.pPen, StartX, StartY, EndX, EndY)   ; draw new line
			UpdateLayeredWindow(this.hwnd, this.hdc)
		}
		this.Lines.Insert([ StartX, StartY, EndX, EndY])   ; insert new line in collection
	}
	MoveLine() {
		Hotkey := RegExReplace(A_ThisHotkey, (A_IsUnicode = 1) ? "(*UCP)^(\w* & |\W*)" : "^(\w* & |\W*)")
		StartX := this.LineToMove.2, StartY := this.LineToMove.3
		this.Lines.Remove(this.LineToMove.1)   ; remove LineToMove from collection and treat is as new line
		While (GetKeyState(Hotkey, "p") = 1) {
			Sleep, 20
			MouseGetPos, EndX, EndY
			Gdip_GraphicsClear(this.G)
			For LineNum,pLine in this.Lines   ; draw all lines in collection
				Gdip_DrawLine(this.G, this.pPen, pLine.1, pLine.2, pLine.3, pLine.4)
			Gdip_DrawLine(this.G, this.pPen, StartX, StartY, EndX, EndY)   ; draw new line
			UpdateLayeredWindow(this.hwnd, this.hdc)
		}
		this.Lines.Insert(this.LineToMove.1, [ StartX, StartY, EndX, EndY]), this.LineToMove := ""   ; insert new line in collection, delete LineToMove info
	}
	DeleteAllLines() {
		this.Lines := [], this.LineToMove := "", Gdip_GraphicsClear(this.G), UpdateLayeredWindow(this.hwnd, this.hdc)
	}
	DeleteLine(LineNum="") {	; if LineNum is blank, last line will be deleted
		if (this.Lines.MaxIndex() = "")   ; no lines in collection
			return
		LineNum := (LineNum = "") ? this.Lines.MaxIndex() : LineNum	; last or specified
		this.Lines.Remove(LineNum)   ; remove from collection
		Gdip_GraphicsClear(this.G)
		For LineNum,pLine in this.Lines   ; draw all lines in collection
			Gdip_DrawLine(this.G, this.pPen, pLine.1, pLine.2, pLine.3, pLine.4)
		UpdateLayeredWindow(this.hwnd, this.hdc)
	}
	IsClickOnLineEnd(radius=6) {
		if (this.Lines.MaxIndex() = "")   ; no lines in collection
			return
		MouseGetPos, x, y
		TotalLines := this.Lines.MaxIndex()
		Loop % TotalLines   ; Z-order.   Reverse Z-order: For LineNum,pLine in this.Lines
		{
			LineNum := TotalLines-A_Index+1, pLine := this.Lines[LineNum]
			if (this.IsInCircle(pLine.1, pLine.2, x, y, radius) = 1) {
				this.LineToMove := [LineNum, pLine.3, pLine.4]   ; LineNum | Coord3 | Coord4
				return 1
			}
			else if (this.IsInCircle(pLine.3, pLine.4, x, y, radius) = 1) {
				this.LineToMove := [LineNum, pLine.1, pLine.2]   ; LineNum | Coord1 | Coord2
				return 1
			}
		}
		this.LineToMove := ""
	}
	IsInCircle(Xstart, Ystart, Xend, Yend, radius) {
		a := Abs(Xend-Xstart), b := Abs(Yend-Ystart), c := Sqrt(a*a+b*b)
		Return (c<radius) ? 1:0   ; if in circle returns 1, else 0
	}
	__Delete() {
		Gdip_DeletePen(this.pPen)
		Gdip_DeleteGraphics(this.G)
		SelectObject(this.hdc, this.obm), DeleteObject(this.hbm), DeleteDC(this.hdc)
		if (this.pToken != "")
			Gdip_Shutdown(this.pToken)
	}
}
Sarhad
Posts: 7
Joined: 05 May 2024, 13:58

Re: I need help with this code because I am not a programmer, can anyone add F4 to change the color

06 May 2024, 04:05

Thank you so much for your help I really needed this God bless you

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: AlFlo, Google [Bot] and 147 guests