GUI: Placeholder im Edit

Stelle Fragen zur Programmierung mit Autohotkey

Moderator: jNizM

User avatar
SAPlayer
Posts: 48
Joined: 30 Sep 2013, 13:38
Location: Germany
Contact:

GUI: Placeholder im Edit

03 Nov 2013, 04:23

Hallo,

ist es möglich, einem Edit-Feld einen Placeholder zu verpassen (Beispiel)?
Mir ist klar, dass das nicht so einfach geht, aber geht das evtl. über irgendwelche Tricks (DllCall o.Ä.)?

Vielen Dank
SAPlayer
Alibaba
Posts: 480
Joined: 29 Sep 2013, 16:15
Location: Germany

Re: GUI: Placeholder im Edit

03 Nov 2013, 05:38

Das erste was mir da in den Sinn kommt, wäre, dem Edit eine normale Voreinstellung zu geben und das Edit dann zu leeren, sobald es ein Klick Event empfängt.

Edit: Und vielleicht erst die Schriftfarbe auf Grau setzen und dann nach dem Klick auf Schwarz.
Allerdings müsste man auch erkennen, wenn aus dem Edit herausgeklickt wird (oder in ein anderes). Dann müsste man natürlich wieder Schriftfarbe und Inhalt ändern, falls es noch leer ist.
"Nothing is quieter than a loaded gun." - Heinrich Heine
User avatar
SAPlayer
Posts: 48
Joined: 30 Sep 2013, 13:38
Location: Germany
Contact:

Re: GUI: Placeholder im Edit

03 Nov 2013, 05:42

Das ist schonmal nicht so schlecht, jedoch müsste man dann bei Gui, Submit (bzw. GuiControlGet) unterscheiden können, ob der eingetragene Wert der Placeholder ist oder der echte Wert (es kann ja sein, dass jemand Name heißt ;) ).
Außerdem ist der Text nicht grau :D

Vielleicht findet ja jemand noch eine bessere Lösung.
Alibaba
Posts: 480
Joined: 29 Sep 2013, 16:15
Location: Germany

Re: GUI: Placeholder im Edit

03 Nov 2013, 06:22

SAPlayer wrote:Außerdem ist der Text nicht grau :D
Doch, theoretisch kann man ja auch die Schriffarbe im Edit mit GuiControl ändern, aber trotzdem stimme ich dir zu, dass die Lösung relativ unelegant ist.
"Nothing is quieter than a loaded gun." - Heinrich Heine
just me
Posts: 9458
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: GUI: Placeholder im Edit

03 Nov 2013, 09:47

EM_SETCUEBANNER

Beispiele gibt es im alten Forum. Läuft halbwegs gut mit XP, richtig gut ab Vista.
Alibaba
Posts: 480
Joined: 29 Sep 2013, 16:15
Location: Germany

Re: GUI: Placeholder im Edit

03 Nov 2013, 10:13

In diesem Thread postet VxE ein funktionsfähiges Beispiel:
http://www.autohotkey.com/board/topic/5 ... nner-work/

Allerdings scheint es bei mir nicht ganz zu funktionieren, es wird immer nur der erste Buchstabe des übergebenen Strings angezeigt.
cuebanner.JPG
cuebanner.JPG (9.68 KiB) Viewed 3785 times
(statt "First Name" und "Last Name")

Sowohl unter Win7 Home SP1 als auch bei WinXP Professional SP2.
"Nothing is quieter than a loaded gun." - Heinrich Heine
User avatar
SAPlayer
Posts: 48
Joined: 30 Sep 2013, 13:38
Location: Germany
Contact:

Re: GUI: Placeholder im Edit

03 Nov 2013, 10:16

Danke!
Habe etwas gefunden und mir daraus das gemacht (getestet unter ANSI 32bit):

Code: Select all

/*
## Funktion: SetEditPlaceholder
## Beschreibung: Setzt einen Platzhalter für ein Edit-Feld. Dieser ist nur sichtbar, solange nichts in dem Feld steht. Entspricht dem Attribut placeholder in HTML.
## Parameter:
# control: Entweder ein HWND oder die zugewiesene Variable (als String!) des Steuerelements.
# string: Der Text, der als Platzhalter im Steuerelement stehen soll.
# showalways: Bestimmt, ob der Text auch angezeigt werden soll, während das Steuerelement Fokus hat. Standard: 0 (deaktiviert)
## return: "" (kein besonderer Wert)
*/

SetEditPlaceholder(control, string, showalways = 0){
	if control is not number
		GuiControlGet, control, HWND, %control%
	if(!A_IsUnicode){
		VarSetCapacity(wstring, (StrLen(wstring) * 2) + 1)
		DllCall("MultiByteToWideChar", UInt, 0, UInt, 0, UInt, &string, Int, -1, UInt, &wstring, Int, StrLen(string) + 1)
	}
	else
		wstring := string
	DllCall("SendMessageW", "UInt", control, "UInt", 0x1501, "UInt", showalways, "UInt", &wstring)
	return
}
User avatar
joedf
Posts: 8959
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: GUI: Placeholder im Edit

07 Nov 2013, 23:43

cool! das muss ich ausprobieren! jetzt und sofort! :D

EDIT:

Code: Select all

Gui, Add, Edit, w100 hwndhEditA
Gui, Add, Edit, wp hwndhEditB
SetEditPlaceholder(hEditA,"First Name",1)
SetEditPlaceholder(hEditB,"Last Name",1)
Gui, Show
return

GuiClose:
ExitApp
Prima!!!!!!!!!!!!!!!!!!!!!!!!!

Code: Select all

EditShowBalloonTip(h, title, text, timeout=1000) {
	
	ControlGetPos,x,y,,t,,ahk_id %h%
	ToolTip, %title%`n%text%, %x%, % (y+t)
	SetTimer, RemoveToolTip, %timeout%
	return

	RemoveToolTip:
	SetTimer, RemoveToolTip, Off
	ToolTip
	return
}

SetEditPlaceholder(control, string, showalways = 0){
    if control is not number
        GuiControlGet, control, HWND, %control%
    if(!A_IsUnicode){
        VarSetCapacity(wstring, (StrLen(wstring) * 2) + 1)
        DllCall("MultiByteToWideChar", UInt, 0, UInt, 0, UInt, &string, Int, -1, UInt, &wstring, Int, StrLen(string) + 1)
    }
    else
        wstring := string
    DllCall("SendMessageW", "UInt", control, "UInt", 0x1501, "UInt", showalways, "UInt", &wstring)
    return
}

Gui, Add, Picture, y6 x6 w48 h-1 Icon161, %A_Windir%\system32\shell32.dll
Gui, Add, Edit, yp+4 x+2 w100 hwndhEditA gOnChangeEditA vA
Gui, Add, Edit, y+2 xp wp hwndhEditB gOnChangeEditB Password
Gui, Add, Button,y+2 x6 w150 gSub Default, &Submit
Gui, Show,w162 h84,Form1
SetEditPlaceholder(hEditA,"User Name",1)
SetEditPlaceholder(hEditB,"Password",1)
return

Sub:
MsgBox, 64, Form1, Login Successful! :D
GuiClose:
ExitApp

OnChangeEditA:
EditShowBalloonTip(hEditA,"User Name","Enter your User Name here.")
return

OnChangeEditB:
EditShowBalloonTip(hEditB,"Password","Enter your Password here.")
return

~Enter::
~Tab::
ToolTip
return
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]

Return to “Ich brauche Hilfe”

Who is online

Users browsing this forum: No registered users and 66 guests