Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

[AHK 1.1] Font and Color Dialogs


  • Please log in to reply
39 replies to this topic
maestrith
  • Members
  • 786 posts
  • Last active: Apr 10 2019 01:28 PM
  • Joined: 17 Sep 2005

I am sure that there are other versions of this but I would like to share my 2 functions that make interfacing with this window a bit easier.

Font Dialog (Both x32 and x64)

#SingleInstance,Force
name:="Tahoma" ;Default selected font
style:={size:14,color:0xFF0000,strikeout:1,underline:1,italic:1,bold:1} ;Set the style information Italic,Bold,Strikeout,and Underline are not necessary unless you want them pre-selected and order is not important
Gui,+hwndhwnd
Gui,Add,Edit,w500 h200 hwndedit,Sample Text
Gui,Add,Edit,w500 h200 hwndedit1,Sample Text
Gui,Add,Button,gfont Default,Change Font
Gui,Show,,Change Font Example
return
font:
if !font:=Dlg_Font(name,style,hwnd) ;shows the user the font selection dialog
;to get information from the style object use ( bold:=style.bold ) or ( underline:=style.underline )...
return
Gui,font,% "c" RGB(style.color)
GuiControl,font,Edit1
GuiControl,font,Edit2
SendMessage,0x30,font,1,,ahk_id%Edit%
SendMessage,0x30,font,1,,ahk_id%Edit1%
return
;to get any of the style return values : value:=style.bold will get you the bold value and so on
Dlg_Font(ByRef Name,ByRef Style,hwnd="",effects=1){
	static logfont
	VarSetCapacity(logfont,60),LogPixels:=DllCall("GetDeviceCaps","uint",DllCall("GetDC","uint",0),"uint",90),Effects:=0x041+(Effects?0x100:0)
	for a,b in fontval:={16:style.bold?700:400,20:style.italic,21:style.underline,22:style.strikeout,0:style.size?Floor(style.size*logpixels/72):16}
	NumPut(b,logfont,a)
	cap:=VarSetCapacity(choosefont,A_PtrSize=8?103:60,0)
	NumPut(hwnd,choosefont,A_PtrSize)
	for index,value in [[cap,0,"Uint"],[&logfont,A_PtrSize=8?24:12,"Uptr"],[effects,A_PtrSize=8?36:20,"Uint"],[style.color,A_PtrSize=4?6*A_PtrSize:5*A_PtrSize,"Uint"]]
	NumPut(value.1,choosefont,value.2,value.3)
	if (A_PtrSize=8)
	strput(name,&logfont+28),r:=DllCall("comdlg32\ChooseFont","uptr",&CHOOSEFONT,"cdecl"),name:=strget(&logfont+28)
	else
	strput(name,&logfont+28,32,"utf-8"),r:=DllCall("comdlg32\ChooseFontA","uptr",&CHOOSEFONT,"cdecl"),name:=strget(&logfont+28,32,"utf-8")
	if !r
	return 0
	for a,b in {bold:16,italic:20,underline:21,strikeout:22}
	style[a]:=NumGet(logfont,b,"UChar")
	style.bold:=style.bold<188?0:1
	style.color:=NumGet(choosefont,A_PtrSize=4?6*A_PtrSize:5*A_PtrSize)
	style.size:=NumGet(CHOOSEFONT,A_PtrSize=8?32:16,"UChar")//10
	;charset:=NumGet(logfont,23,"UChr")
	return DllCall("CreateFontIndirect",uptr,&logfont,"cdecl")
}
rgb(c){
	setformat,IntegerFast,H
	c:=(c&255)<<16|(c&65280)|(c>>16),c:=SubStr(c,1)
	SetFormat, integerfast,D
	return c
}
GuiClose:
GuiEscape:
ExitApp
return 

Font Dialog (AHK_L x32 only)

name:="Tahoma" ;Default selected font
style:={size:20,color:0x0000ff,strikeout:1,underline:1,italic:1,bold:1} ;Set the style information Italic,Bold,Strikeout,and Underline are not necessary unless you want them pre-selected and order is not important
#SingleInstance,Force
Gui,Add,Edit,w500 h500,Sample Text
Gui,Add,Button,gfont Default,Change Font
Gui,Show,,Change Font Example
return
font:
if !Dlg_Font(name,style) ;shows the user the font selection dialog
return ;If they hit Escape or close the dialog without hitting OK it will stop
list=
for a,b in style
if b=1
list.=a " "
Gui,Font,Normal
GuiControl,font,Edit1 ;this returns the font to default (needs to be done)
Gui,Font,% "s" style.size " " list " c" style.color,%name%
GuiControl,font,Edit1 ;this changes the font to what you selected in the dlg_font
return
;to get any of the style return values : value:=style.bold will get you the bold value and so on
Dlg_Font(ByRef Name,ByRef Style,Effects=1){
	VarSetCapacity(LOGFONT,60),strput(name,&logfont+28,32,"CP0")
	LogPixels:=DllCall("GetDeviceCaps","uint",DllCall("GetDC","uint",0),"uint",90),Effects:=0x041+(Effects?0x100:0)
	for a,b in font:={16:"bold",20:"italic",21:"underline",22:"strikeout"}
	if style[b]
	NumPut(b="bold"?700:1,logfont,a)
	style.size?NumPut(Floor(style.size*logpixels/72),logfont,0):NumPut(16,LOGFONT,0)
	VarSetCapacity(CHOOSEFONT,60,0),NumPut(60,CHOOSEFONT,0),NumPut(&LOGFONT,CHOOSEFONT,12),NumPut(Effects,CHOOSEFONT,20),NumPut(RGB(style.color),CHOOSEFONT,24)
	if !r:=DllCall("comdlg32\ChooseFontA", "uint",&CHOOSEFONT)
	return
	Color:=RGB(NumGet(CHOOSEFONT,24))
	style:={size:NumGet(CHOOSEFONT,16)//10,name:name:=StrGet(&logfont+28,"CP0"),color:color,bold:bold}
	for a,b in font
	style[b]:=NumGet(LOGFONT,a,"UChar")?1:0
        style.bold:=NumGet(LOGFONT,16)>=700?1:0
	return 1
}
rgb(c){
	setformat,IntegerFast,H
	c:=(c&255)<<16|(c&65280)|(c>>16),c:=SubStr(c,1)
	SetFormat, integerfast,D
	return c
}
GuiClose:
GuiEscape:
ExitApp
return

Font Dialog x64 only

name:="Tahoma" ;Default selected font
style:={size:16,color:0x0000ff,strikeout:1,underline:1,italic:1,bold:1} ;Set the style information Italic,Bold,Strikeout,and Underline are not necessary unless you want them pre-selected and order is not important
#SingleInstance,Force
Gui,Add,Edit,w500 h500,Sample Text
Gui,Add,Button,gfont Default,Change Font
Gui,Show,,Change Font Example
return
font:
if !Dlg_Font(name,style) ;shows the user the font selection dialog
return ;If they hit Escape or close the dialog without hitting OK it will stop
list=
for a,b in style
if b=1
list.=a " "
Gui,Font,Normal
GuiControl,font,Edit1 ;this returns the font to default (needs to be done)
Gui,Font,% "s" style.size " " list " c" RGB(style.color),%name%
GuiControl,font,Edit1 ;this changes the font to what you selected in the dlg_font
return
;to get any of the style return values : value:=style.bold will get you the bold value and so on
Dlg_Font(ByRef Name,ByRef Style){
	VarSetCapacity(logfont,60,0)
	LogPixels:=DllCall("GetDeviceCaps","uint",DllCall("GetDC","uint",0),"uint",90)
	style.size?NumPut(Floor(style.size*logpixels/72),logfont,0):NumPut(16,LOGFONT,0)
	effects:=0x141
	cap:=VarSetCapacity(choosefont,103,0)
	for a,b in font:={16:"bold",20:"italic",21:"underline",22:"strikeout"}
	if style[b]
	NumPut(b="bold"?700:1,logfont,a)
	StrPut(name,&logfont+28)
	for index,value in [[cap,0,"Uint"],[&logfont,24,"Uptr"],[effects,36,"Uint"],[style.color,40,"Uint"]]
	NumPut(value.1,choosefont,value.2,value.3)
	r:=DllCall("comdlg32\ChooseFont","uint",&CHOOSEFONT,"cdecl")
	Color:=NumGet(CHOOSEFONT,5*A_PtrSize)
	style:={size:NumGet(CHOOSEFONT,32,"Uchar")//10,name:name:=StrGet(&logfont+28),color:color,bold:bold}
	for a,b in font
	style[b]:=NumGet(LOGFONT,a,"UChar")?1:0
	style.bold:=NumGet(LOGFONT,16)&0xffff>=700?1:0
	return r
}
rgb(c){
	setformat,IntegerFast,H
	c:=(c&255)<<16|(c&65280)|(c>>16),c:=SubStr(c,1)
	SetFormat, integerfast,D
	return c
}
GuiClose:
GuiEscape:
ExitApp
return

Color Dialog x32+x64

#SingleInstance,Force
IniRead,cc,color.ini,default,color,0
Gui,Color,% RGB(cc)
Gui,+hwndhwnd +ToolWindow
Gui,Add,Button,gchoose,Choose Color
Gui,Show,,Color Test
return
choose:
cc:=dlg_color(cc,hwnd)
Gui,Color,% RGB(cc)
return
GuiEscape:
GuiClose:
ExitApp
return
Dlg_Color(Color,hwnd){
	static
	if !cc{
		VarSetCapacity(CUSTOM,16*A_PtrSize,0),cc:=1,size:=VarSetCapacity(CHOOSECOLOR,9*A_PtrSize,0)
		Loop,16{
			IniRead,col,color.ini,color,%A_Index%,0
			NumPut(col,CUSTOM,(A_Index-1)*4,"UInt")
		}
	}
	NumPut(size,CHOOSECOLOR,0,"UInt"),NumPut(hwnd,CHOOSECOLOR,A_PtrSize,"UPtr")
	,NumPut(Color,CHOOSECOLOR,3*A_PtrSize,"UInt"),NumPut(3,CHOOSECOLOR,5*A_PtrSize,"UInt")
	,NumPut(&CUSTOM,CHOOSECOLOR,4*A_PtrSize,"UPtr")
	ret:=DllCall("comdlg32\ChooseColor","UPtr",&CHOOSECOLOR,"UInt")
	if !ret
	exit
	Loop,16
	IniWrite,% NumGet(custom,(A_Index-1)*4,"UInt"),color.ini,color,%A_Index%
	IniWrite,% Color:=NumGet(CHOOSECOLOR,3*A_PtrSize,"UInt"),color.ini,default,color
	return Color
}
rgb(c){
	setformat,IntegerFast,H
	c:=(c&255)<<16|(c&65280)|(c>>16),c:=SubStr(c,1)
	SetFormat,IntegerFast,D
	return c
}

Let me know what you think.



Cragaha
  • Members
  • 265 posts
  • Last active: Jan 04 2016 02:24 AM
  • Joined: 19 Nov 2010

Short and good happy.png

Just a little badly indented.



maestrith
  • Members
  • 786 posts
  • Last active: Apr 10 2019 01:28 PM
  • Joined: 17 Sep 2005

Thank you. Hopefully someone will get some use out of it.



tidbit
  • Administrators
  • 2709 posts
  • Hates playing Janitor
  • Last active: Jan 15 2016 11:37 PM
  • Joined: 09 Mar 2008

doesn't seem to be working on unicode 64bit.

 

side note: you should put all these (you got a Font one too, as of now) in one topic.


rawr. be very afraid
*poke*
. Populate the AutoHotkey city. Pointless but somewhat fun. .


mrmike
  • Members
  • 45 posts
  • Last active: Jun 07 2014 04:38 PM
  • Joined: 20 Jan 2007

For me this code and the Font code doesn't seem to be working on unicode 64bit.


Posted Image
Vista 32 Home Premium SP2

Alpha Bravo
  • Members
  • 1687 posts
  • Last active: Nov 07 2015 03:06 PM
  • Joined: 01 Sep 2011

good job, works fine here.

win 7 64bit unicode



DataLife
  • Members
  • 1022 posts
  • Last active: Nov 27 2015 01:09 AM
  • Joined: 27 Apr 2008
Very nice, I like being able to save custom colors.

Is there a way to detect that the cancel button was pressed?

Pressing "Cancel" changes the gui color to black on the first time a color is chosen after the script starts.

After that pressing the cancel button works properly and the color of the gui is not changed.
Check out my scripts.  (MyIpChanger) (XPSnap) (SavePictureAs) All my scripts are tested on Windows 7, AutoHotkey 32 bit Ansi unless otherwise stated.

Hamlet
  • Members
  • 302 posts
  • Last active: Mar 23 2014 03:37 PM
  • Joined: 22 Jan 2009

Fine, Thanks !!

(Edit: I'm on 32bit)

 

 

It reminds me previous work of some smart guy.

http://www.autohotke...ic.php?p=366371



just me
  • Members
  • 1496 posts
  • Last active: Nov 03 2015 04:32 PM
  • Joined: 28 May 2011

... cannot work on unicode 64-bit, because it is using the 32-bit size and offsets for the CHOOSECOLOR structure and the lack of  the Type parameter "UInt" for NumGet() (default is "UPtr").


Prefer ahkscript.org for the time being.


rbrtryn
  • Members
  • 1177 posts
  • Last active: Sep 11 2013 08:04 PM
  • Joined: 22 Jun 2011

Related: Windows Color Picker Plus
shameless self promotion biggrin.gif


My Scripts are written for the latest released version of AutoHotkey.

Need a secure, accessible place to backup your stuff? Use Dropbox!


maestrith
  • Members
  • 786 posts
  • Last active: Apr 10 2019 01:28 PM
  • Joined: 17 Sep 2005

Related: Windows Color Picker Plus
shameless self promotion biggrin.gif

I installed x64 version of AHK_L and could not get it to work.



maestrith
  • Members
  • 786 posts
  • Last active: Apr 10 2019 01:28 PM
  • Joined: 17 Sep 2005

Latest version tested on both x32 and x64



DataLife
  • Members
  • 1022 posts
  • Last active: Nov 27 2015 01:09 AM
  • Joined: 27 Apr 2008
Newest version works great.

Is there a way to have a default color selected when the color dialog opens?
Check out my scripts.  (MyIpChanger) (XPSnap) (SavePictureAs) All my scripts are tested on Windows 7, AutoHotkey 32 bit Ansi unless otherwise stated.

maestrith
  • Members
  • 786 posts
  • Last active: Apr 10 2019 01:28 PM
  • Joined: 17 Sep 2005

Newest version works great.

Is there a way to have a default color selected when the color dialog opens?

Thanks :)

when you call the function dlg_color(cc,hwnd) if you replace cc with a color (0xFF0000) in BGR format, it will have that selected

If that does not work let me know.



think
  • Members
  • 119 posts
  • Last active: Nov 01 2015 11:37 PM
  • Joined: 23 May 2013

this is excellent, thank you! It would be great to show the color in a little coloured box which could be clicked to change it. I'm not sure which control to use for this.