Re: getFontDialog
- Posted by Tom Irwin <tomirwin506 at ?otmail?com> Oct 09, 2007
- 675 views
CChris wrote: > > Tom Irwin wrote: > > > > I use w32Lib's getFontDialog to change selected text in richedit controls. > > > > However, endusers often want to change only one item of fontname, style, > > size, > > > > or color and report finding it awkward to have to set all four each time the > > dialog > > is called. In fact they make mistakes by changing just what they want > > changed, > > > > not realizing that they just reset the other attributes to default. It > > works > > differently > > for them than their other common Windows programs. > > > > It would be extremely useful if the dialog could be made to show the font of > > the > > original selected text (as in text editors like Wordpad). > > > > Can anyone help as to how this might be accomplished? > > It can be done by copying the text of the getFontDialog() function and > deriving > a variant from it. It initialises a structure for the font chooser to display > its initial values; the variant needs to set these values to those relevant > to the selection, while the original uses values relevant for the control as > a whole. > > To get these values, send the RichEdit control an EM_GETCHARFORMAT message > with > wParam = SCF_SELECTION (which is 1), and lParam being the address of a valid > CHARFORMAT structure. This structure must be initialised so that, on return > from sendMessage(), the fields which you need initialised are indeed set up. > Use w32fetch() to retrieve the values to w32store() in the font chooser init > structure. > > The newer win32lib can create the structure and get its address by calling > w32to_memory(ID_CHARFORMAT,{field > values}). The field values will be as needed by the needs of the clone of > getFontDialog(). > With older versions, there is a struct_CHARFORMAT() function to do this - not > sure it was documented -. > > CChris Thanks Christian for your help. In case it saves someone else some time, here is the code I came up with to have the font dialog select the existing font characteristics rather than the control default font: (Sorry, this is for w32Lib 60.6 that I was working with, but later version can use similar) Add this function just following existing getFontDialog function:
global function getFontDialogSelTxt( integer id, atom sentmask,atom senteffects,atom sentheight,atom sentoffset, atom senttxtcolor,atom sentcharset,atom sentpitchnfam, sequence sentfacename) integer points, attrib sequence font atom hChooseFont, hLogFont, color, mset, cf object result,colort integer owner -- assume failure result = -1 owner = findParentWindow(id) -- create structures mset = w32new_memset() hChooseFont = w32acquire_mem(mset, SIZEOF_CHOOSEFONT) hLogFont = w32acquire_mem(mset, SIZEOF_LOGFONT) -- populate the choose font structure w32store( hLogFont, lfFaceName, sentfacename) if sequence(ctrl_Font[id][FontSize]) then w32store( hLogFont, lfHeight, ctrl_Font[id][FontSize][1] ) else w32store( hLogFont, lfHeight, -(5 * sentheight)/74 ) -- don't know proper method so adjusted nums to --give produce correct point size end if w32store( hLogFont, lfWeight, ((and_bits( senteffects, Bold) ) != 0) * vFontWeight ) w32store( hLogFont, lfItalic, (and_bits( senteffects, Italic)) != 0 ) w32store( hLogFont, lfUnderline, (and_bits( senteffects, Underline)) != 0 ) w32store( hLogFont, lfStrikeOut, (and_bits( senteffects, Strikeout)) != 0 ) w32store( hChooseFont, CF_lStructSize, SIZEOF_CHOOSEFONT ) w32store( hChooseFont, CF_hwndOwner, getHandle( owner ) ) w32store( hChooseFont, CF_Flags, CF_INITTOLOGFONTSTRUCT+CF_SCRIPTSONLY+CF_SCREENFONTS+CF_EFFECTS) w32store( hChooseFont, CF_hLogFont, hLogFont ) w32store(hChooseFont, CF_rgbColors, senttxtcolor) -- call the dialog if w32Func( xChooseFont,{ hChooseFont } ) then -- pointer to structure good? if w32fetch( hChooseFont, CF_hLogFont ) then -- got pointer successfully font = w32peek_string( w32address( hLogFont, lfFaceName ) ) --font = "Arial Black" points = floor( w32fetch( hChooseFont, CF_iPointSize ) / 10 ) -- clear attributes attrib = 0 -- NEW! 0.45o check correct flag -- bold? if w32fetch( hLogFont, lfWeight ) >= vFontWeight then attrib += Bold end if -- italic? if w32fetch( hLogFont, lfItalic ) then attrib += Italic end if -- underline? if w32fetch( hLogFont, lfUnderline ) then attrib += Underline end if -- strikeout? if w32fetch( hLogFont, lfStrikeOut ) then attrib += Strikeout end if --get font color color = w32fetch( hChooseFont, CF_rgbColors ) -- save result result = { font, points, attrib, color } end if end if -- Free the structures w32release_mem(mset) return result end function
Then this code in the main program:
cf = struct_CHARFORMAT ( 0,0,0,0,0,0,0,"") VOID = sendMessage(lyricriched2, EM_GETCHARFORMAT, SCF_SELECTION, cf) fname = w32fetch( cf, CHARFORMAT_szFaceName ) fcolor = w32fetch( cf, CHARFORMAT_crTextColor ) fheight = w32fetch( cf, CHARFORMAT_yHeight ) feffects = w32fetch( cf, CHARFORMAT_dwEffects) font = getFontDialogSelTxt( Win1,0,feffects,fheight,0,fcolor,0,0,fname )