Tooltip () -> schnell und einfach ein Tooltip anzeigen

Hier könnt Ihr gute, von Euch geschriebene Codes posten. Sie müssen auf jeden Fall funktionieren und sollten möglichst effizient, elegant und beispielhaft oder einfach nur cool sein.
SBond
Beiträge: 266
Registriert: 22.05.2013 20:35
Computerausstattung: armseliger Laptop, mit wenig RAM und noch weniger CPU-Leistung. ...und die Grafikkarte.... ....naja.. da male ich doch lieber selber.
Wohnort: nahe Wolfsburg

Re: Tooltip () -> schnell und einfach ein Tooltip anzeigen

Beitrag von SBond »

Version 1.02: *only Windows*

das Tooltip ist nun immer innerhalb des Desktops sichtbar

Code: Alles auswählen

Procedure Tooltip (sText.s , sTitel.s = "", sSymbol.s = "kein", iX_Pos.i = #PB_Default, iY_Pos.i = #PB_Default, iMax_Breite.i = #PB_Default, bBallon.i = #False, bSchliessen.i = #False)
	
	#TTF_ABSOLUTE		= $0080
	#TTF_TRACK			= $0020
	#TTS_CLOSE			= $80
	#TTS_NOFADE			= $20
	
	Static		iTooltip_ID.i		= 0
	Static		iStyle_aktuell.i	= 0
	Static		iX_Pos_aktuell.i	= 0
	Static		iY_Pos_aktuell.i	= 0	
	Static		sText_aktuell.s		= ""
	Static		sTitel_aktuell.s	= ""
	Static		sSymbol_aktuell.s	= ""

	Protected 	iUpdate_Inhalt.i	= #False
	Protected 	iUpdate_Position.i	= #False
	Protected	iX_Pos_Offset.i		= 0
	Protected	iY_Pos_Offset.i		= 0
	
	Protected	iSymbol.i			= 0
	Protected	iStyle.i			= #WS_POPUP | #TTS_NOPREFIX | #TTS_ALWAYSTIP | #TTS_NOFADE
	Protected	iExStyle.i			= #WS_EX_TOPMOST
	Protected 	lWindowID.l			= 0	; experimentell: WindowID angeben, falls es Probleme mit der Darstellung gibt
	Protected	iInstanz.i			= GetModuleHandle_(0)
	Protected	lPosition.l			= 0

	Protected	stParameter.TOOLINFO
	Protected	stAbmessungen.RECT
	Static temp = 0

	; Tooltip löschen, wenn kein Text angegeben wurde
	If sText = "" And iTooltip_ID <> 0
		DestroyWindow_(iTooltip_ID)
		iTooltip_ID 	= 0
		iStyle_aktuell 	= 0
		iX_Pos_aktuell	= 0
		iY_Pos_aktuell	= 0
		sText_aktuell 	= ""
		sTitel_aktuell 	= ""
		sSymbol_aktuell	= ""
		ProcedureReturn 1
		
	ElseIf sText = "" And iTooltip_ID = 0
		iTooltip_ID 	= 0
		iStyle_aktuell 	= 0
		iX_Pos_aktuell	= 0
		iY_Pos_aktuell	= 0
		sText_aktuell 	= ""
		sTitel_aktuell 	= ""
		sSymbol_aktuell	= ""
		ProcedureReturn -1
		
	EndIf
	
	
	; darzustellendes Symbol
	If 		sSymbol = "kein":			iSymbol = #TTI_NONE
	ElseIf 	sSymbol = "Info":			iSymbol = #TTI_INFO
	ElseIf 	sSymbol = "Warnung":		iSymbol = #TTI_WARNING
	ElseIf 	sSymbol = "Fehler":			iSymbol = #TTI_ERROR
	ElseIf 	sSymbol = "Info_groß":		iSymbol = #TTI_INFO_LARGE
	ElseIf 	sSymbol = "Warnung_groß":	iSymbol = #TTI_WARNING_LARGE
	ElseIf 	sSymbol = "Fehler_groß":	iSymbol = #TTI_ERROR_LARGE
	Else: 								iSymbol = #TTI_NONE
	EndIf
	
	
	; X-Position bestimmen (Standard: aktuelle Mausposition)
	If (iX_Pos = #PB_Default) And (bBallon = #True)
		iX_Pos = DesktopMouseX()
		iX_Pos_Offset = 0
		
	ElseIf (iX_Pos = #PB_Default) And (bBallon = #False)
		iX_Pos 	= DesktopMouseX()
		iX_Pos_Offset = 16
	EndIf
	
	
	; Y-Position bestimmen (Standard: aktuelle Mausposition)
	If iY_Pos = #PB_Default
		iY_Pos = DesktopMouseY()
		iY_Pos_Offset = 0
		
	EndIf   
	
	
	; maximale Breite festlegen
	If (iMax_Breite = #PB_Default) Or (iMax_Breite < 10)
		iMax_Breite = 400
		
	EndIf   
	
	
	; ggf. Ballonform aktivieren
	If bBallon = #True
		iStyle | #TTS_BALLOON
	EndIf
	
	
	; ggf. den Schließen-Button anzeigen (Ballonform muss aktiviert sein)
	If bSchliessen = #True
		iStyle | #TTS_CLOSE
	EndIf
	
	
	; prüfen, ob schon ein Tooltip existiert
	If iTooltip_ID = 0
		iTooltip_ID = CreateWindowEx_(iExStyle, #TOOLTIPS_CLASS, #Null, iStyle, 0, 0, 0, 0, lWindowID, 0, iInstanz, 0)
	Else
		If iStyle_aktuell <> iStyle
			DestroyWindow_(iTooltip_ID)
			iTooltip_ID = CreateWindowEx_(iExStyle, #TOOLTIPS_CLASS, #Null, iStyle, 0, 0, 0, 0, lWindowID, 0, iInstanz, 0)
		EndIf
	EndIf

	
	; Eigenschaften übernehmen und anzeigen
	stParameter.TOOLINFO\cbSize	= SizeOf(TOOLINFO)
	stParameter\uFlags			= #TTF_IDISHWND | #TTF_ABSOLUTE | #TTF_TRACK
	stParameter\hWnd			= lWindowID
	stParameter\uId				= lWindowID
	stParameter\lpszText		= @sText
	stParameter\hInst			= iInstanz
	

	; prüfen, ob die Position oder der Inhalt geändert wurde
	If (sText <> sText_aktuell) Or (sTitel <> sTitel_aktuell) Or (sSymbol <> sSymbol_aktuell) Or (sText_aktuell = "")
		iUpdate_Inhalt		= #True
	Else
		iUpdate_Inhalt		= #False
	EndIf
	
	
	If (iX_Pos <> iX_Pos_aktuell) Or (iY_Pos <> iY_Pos_aktuell)
		iUpdate_Position	= #True
	Else
		iUpdate_Position	= #False
	EndIf

	
	; aktuelle Daten speichern
	iStyle_aktuell	= iStyle
	sText_aktuell 	= sText
	sTitel_aktuell 	= sTitel
	sSymbol_aktuell = sSymbol
	iX_Pos_aktuell	= iX_Pos
	iY_Pos_aktuell	= iY_Pos

	
	; Tooltip-Inhalt übernehmen und darstellen
	If iUpdate_Inhalt = #True
		
		SendMessage_	(iTooltip_ID,	#TTM_SETTIPTEXTCOLOR,	GetSysColor_(#COLOR_INFOTEXT),	0)
		SendMessage_	(iTooltip_ID,	#TTM_SETTIPBKCOLOR,		GetSysColor_(#COLOR_INFOBK),	0)
		SendMessage_	(iTooltip_ID,	#TTM_SETMAXTIPWIDTH,	0, iMax_Breite)	
		SendMessage_	(iTooltip_ID,	#TTM_SETTITLE, 			iSymbol, @sTitel)
		
		GetWindowRect_	(lWindowID, 	@stParameter\rect)
		SendMessage_	(iTooltip_ID, 	#TTM_ADDTOOL, 		0, @stParameter)
		SendMessage_	(iTooltip_ID, 	#TTM_TRACKACTIVATE, 1, @stParameter)
		SendMessage_	(iTooltip_ID,	#TTM_UPDATETIPTEXT, 0, @stParameter)		

	EndIf
	
	
	If iUpdate_Position = #True
		
		; die Position des Tooltips anpassen
		If (ExamineDesktops() <> 0)
			
			; die Abmessung des Tooltips ermitteln
			GetWindowRect_	(iTooltip_ID, @stAbmessungen)
			
			If (iX_Pos + iX_Pos_Offset +(stAbmessungen\right - stAbmessungen\left) > DesktopWidth(0))
				iX_Pos = iX_Pos - iX_Pos_Offset - (stAbmessungen\right - stAbmessungen\left)
			Else
				iX_Pos = iX_Pos + iX_Pos_Offset
			EndIf
			
			If (iY_Pos + iY_Pos_Offset +(stAbmessungen\bottom - stAbmessungen\top) > DesktopHeight(0))
				iY_Pos = iY_Pos - (stAbmessungen\bottom - stAbmessungen\top)
			Else
				iY_Pos = iY_Pos + iY_Pos_Offset
			EndIf
			
		EndIf
		
		lPosition = (iX_Pos & $FFFF) | ((iY_Pos & $FFFF) << 16)
		SendMessage_	(iTooltip_ID,	#TTM_TRACKPOSITION, 	0, lPosition)
		
	EndIf

	ProcedureReturn 0
	
EndProcedure


OpenWindow   (0, 0, 0, 40, 30, "Tooltip", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)


Repeat
	
	Text$ = "CPU-Name:" + #TAB$ + #TAB$ + CPUName() + #CRLF$
	Text$ + "Computer-Name:" + #TAB$ + #TAB$ + ComputerName() + #CRLF$
	Text$ + "RAM verfügbar:" + #TAB$ + #TAB$ + StrF(MemoryStatus(#PB_System_FreePhysical)/1048576,1) + " MB"
	
	Tooltip(Text$, "aktuelle Daten", "Info_groß", -1, -1 ,600)

Until WaitWindowEvent(15) =  #PB_Event_CloseWindow
41 6c 73 6f 20 77 65 6e 6e 20 64 75 20 73 6f 20 76 69 65 6c 20 4c 61 6e 67 65 77 65 69 6c 65 20 68 61 73 74 2c 20 64 61 6e 6e 20 6b 61 6e 6e 73 74 20 64 75 20 61 75 63 68 20 67 6c 65 69 63 68 20 7a 75 20 6d 69 72 20 6b 6f 6d 6d 65 6e 20 75 6e 64 20 61 62 77 61 73 63 68 65 6e 2e

:D
Antworten