Hey guys. I'm using EM_SETCHARFORMAT to change the font of the Edit Control but it's not working. i Have this line of code: mov chFont.lStructSize,sizeof CHOOSEFONT
mov chFont.lpLogFont,offset LogFont
invoke ChooseFont,addr chFont
invoke CopyBuffer,addr LogFont.lfFaceName,addr chFormat.CHARFORMAT2.szFaceName
invoke MessageBox,0,addr LogFont.lfFaceName,addr chFormat.CHARFORMAT2.szFaceName,MB_OK
mov chFormat.cbSize,sizeof CHARFORMAT2
push LogFont.lfWeight
pop chFormat.wWeight
mov chFormat.dwMask,CFM_FACE
mov chFormat.crTextColor,0ffh
invoke SendMessage,hMdi,WM_MDIGETACTIVE,0,0
invoke GetWindowLong,eax,GWL_USERDATA
invoke SendMessage,[eax.MdiStruct.hEdit],EM_SETCHARFORMAT,SCF_ALL,addr chFormat
Thanks in advance.
"sets character formatting in a rich edit control"
It's a richedit 5.0 control. why is it not working?
Quote from: xandaz on October 18, 2021, 05:34:45 AM
Hey guys. I'm using EM_SETCHARFORMAT to change the font of the Edit Control
ha ha ha. You're very funny JJ. Still, can you see anything wrong with the code. Isn't that the way to change fonts and colors of richedit control?
ty
If you post a complete example, I might look at it. Some code out of context? No thanks. Besides, you should make an effort to test the return value and get error message.
it's in the mdichildproc. search for if ax==IDM_CHOOSEFONT. Thanks
Too much work for me. You are using a non-standard setup, maybe you modified some include files, and I am not willing to solve the mysteries...
The problem starts with your assumption that include files have to sit on drive C:
include c:\masm32\include\masm32rt.inc
include c:\masm32\include\winclasses.asm
The correct syntax is include \masm32\include\masm32rt.inc, i.e. no drive letter.
When I include your winclasses.asm file (not in the archive, but I have it from your earlier posts), I still get loads of errors, related to chFormat:
.\mdi.asm(280) : Error A2167: Missing quotation mark in string
.\mdi.asm(280): Included by
Tmp_File.asm(5): Main line code
.\mdi.asm(280) : Error A2233: Invalid data initializer: dwMask
.\mdi.asm(280): Included by
Tmp_File.asm(5): Main line code
.\mdi.asm(281) : Error A2167: Missing quotation mark in string
.\mdi.asm(281): Included by
Tmp_File.asm(5): Main line code
.\mdi.asm(281) : Error A2233: Invalid data initializer: dwMask
.\mdi.asm(281): Included by
Tmp_File.asm(5): Main line code
.\mdi.asm(282) : Error A2167: Missing quotation mark in string
.\mdi.asm(282): Included by
Tmp_File.asm(5): Main line code
.\mdi.asm(282) : Error A2233: Invalid data initializer: dwMask
.\mdi.asm(282): Included by
Tmp_File.asm(5): Main line code
.\mdi.asm(283) : Error A2167: Missing quotation mark in string
.\mdi.asm(283): Included by
Tmp_File.asm(5): Main line code
.\mdi.asm(283) : Error A2233: Invalid data initializer: dwMask
.\mdi.asm(283): Included by
Tmp_File.asm(5): Main line code
.\mdi.asm(284) : Error A2167: Missing quotation mark in string
.\mdi.asm(284): Included by
Tmp_File.asm(5): Main line code
.\mdi.asm(284) : Error A2233: Invalid data initializer: dwMask
.\mdi.asm(284): Included by
Tmp_File.asm(5): Main line code
.\mdi.asm(285) : Error A2167: Missing quotation mark in string
.\mdi.asm(285): Included by
Tmp_File.asm(5): Main line code
.\mdi.asm(285) : Error A2233: Invalid data initializer: dwMask
.\mdi.asm(285): Included by
Tmp_File.asm(5): Main line code
.\mdi.asm(286) : Error A2167: Missing quotation mark in string
.\mdi.asm(286): Included by
Tmp_File.asm(5): Main line code
.\mdi.asm(286) : Error A2233: Invalid data initializer: dwMask
.\mdi.asm(286): Included by
Tmp_File.asm(5): Main line code
.\mdi.asm(287) : Error A2167: Missing quotation mark in string
.\mdi.asm(287): Included by
Tmp_File.asm(5): Main line code
.\mdi.asm(287) : Error A2233: Invalid data initializer: dwMask
.\mdi.asm(287): Included by
Tmp_File.asm(5): Main line code
.\mdi.asm(288) : Error A2167: Missing quotation mark in string
.\mdi.asm(288): Included by
Tmp_File.asm(5): Main line code
.\mdi.asm(288) : Error A2233: Invalid data initializer: dwMask
.\mdi.asm(288): Included by
Tmp_File.asm(5): Main line code
.\mdi.asm(289) : Error A2167: Missing quotation mark in string
.\mdi.asm(289): Included by
Tmp_File.asm(5): Main line code
.\mdi.asm(289) : Error A2233: Invalid data initializer: dwMask
.\mdi.asm(289): Included by
Tmp_File.asm(5): Main line code
.\mdi.asm(290) : Error A2167: Missing quotation mark in string
.\mdi.asm(290): Included by
Tmp_File.asm(5): Main line code
.\mdi.asm(290) : Error A2233: Invalid data initializer: dwMask
.\mdi.asm(290): Included by
Tmp_File.asm(5): Main line code
.\mdi.asm(291) : Error A2167: Missing quotation mark in string
.\mdi.asm(291): Included by
Tmp_File.asm(5): Main line code
.\mdi.asm(291) : Error A2233: Invalid data initializer: dwMask
.\mdi.asm(291): Included by
Tmp_File.asm(5): Main line code
.\mdi.asm(292) : Error A2167: Missing quotation mark in string
.\mdi.asm(292): Included by
Tmp_File.asm(5): Main line code
.\mdi.asm(292) : Error A2233: Invalid data initializer: dwMask
.\mdi.asm(292): Included by
Tmp_File.asm(5): Main line code
.\mdi.asm(293) : Error A2167: Missing quotation mark in string
.\mdi.asm(293): Included by
Tmp_File.asm(5): Main line code
.\mdi.asm(293) : Error A2233: Invalid data initializer: dwMask
.\mdi.asm(293): Included by
Tmp_File.asm(5): Main line code
.\mdi.asm(294) : Error A2167: Missing quotation mark in string
.\mdi.asm(294): Included by
Tmp_File.asm(5): Main line code
.\mdi.asm(294) : Error A2233: Invalid data initializer: dwMask
.\mdi.asm(294): Included by
Tmp_File.asm(5): Main line code
.\mdi.asm(295) : Error A2167: Missing quotation mark in string
.\mdi.asm(295): Included by
Tmp_File.asm(5): Main line code
.\mdi.asm(295) : Error A2233: Invalid data initializer: dwMask
.\mdi.asm(295): Included by
Tmp_File.asm(5): Main line code
.\mdi.asm(296) : Error A2167: Missing quotation mark in string
.\mdi.asm(296): Included by
Tmp_File.asm(5): Main line code
.\mdi.asm(296) : Error A2233: Invalid data initializer: dwMask
.\mdi.asm(296): Included by
Tmp_File.asm(5): Main line code
.\mdi.asm(297) : Error A2167: Missing quotation mark in string
.\mdi.asm(297): Included by
Tmp_File.asm(5): Main line code
.\mdi.asm(297) : Error A2233: Invalid data initializer: dwMask
.\mdi.asm(297): Included by
Tmp_File.asm(5): Main line code
.\mdi.asm(298) : Error A2167: Missing quotation mark in string
.\mdi.asm(298): Included by
Tmp_File.asm(5): Main line code
.\mdi.asm(298) : Error A2233: Invalid data initializer: dwMask
.\mdi.asm(298): Included by
Tmp_File.asm(5): Main line code
.\mdi.asm(299) : Error A2167: Missing quotation mark in string
.\mdi.asm(299): Included by
Tmp_File.asm(5): Main line code
.\mdi.asm(299) : Error A2233: Invalid data initializer: dwMask
.\mdi.asm(299): Included by
Tmp_File.asm(5): Main line code
Tmp_File.asm: 352 lines, 1 passes, 144 ms, 0 warnings, 40 errors
*** Assembly error ***
This line is horrible, but it's not the reason for your problem:
mov chFormat.dwMask,CFM_FACE+CFM_WEIGHT+CFM_COLOR+CFM_SIZE
By sheer accident, there are no overlapping bits. How many times did we discuss in this forum that bitmasks must be constructed with the or operator, not with +?
And of course, not a single check for errors, not a single display of return values. Why bother if members of this forum can do it for you?
I don't have an example in 32 bit MASM but I have it in 32 bit PowerBASIC. Should be easy enough to translate.
SendMessage hEdit,%EM_SETBKGNDCOLOR,0,&H00333333
set_text_colour hEdit,&H00FFFFFF
' ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
FUNCTION set_text_colour( _
ByVal edit as DWORD, _
ByVal rgbcol as DWORD _
) as DWORD
LOCAL cfm as CHARFORMAT
cfm.cbSize = SIZEOF(cfm)
cfm.dwMask = %CFM_COLOR
cfm.dwEffects = 0
cfm.yHeight = 0
cfm.yOffset = 0
cfm.crTextColor = rgbcol
cfm.bCharSet = 0
cfm.bPitchAndFamily = 0
cfm.szFaceName = " "
cfm.wFiller = 0
SendMessage edit,%EM_SETCHARFORMAT,%SCF_ALL,VarPtr(cfm)
END FUNCTION
' ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
Sorry JJ. I'm thansfule for the help. i forgot that the winclasses.asm wasn't included. I wasn't trying to take any advantage from any forum member. Sorry for the trouble. TY
There is nothing to apologise for here, we all take shortcuts where we can.
Well Hutch. I got it to work thanks to your eaxmple. I was using CHARFORMAT2 and turned it to CHARFORMAT and thigs started working. I'm having some trouble with the font size. CHOOSEFONT.logfont has a parameter lfHeight that i'm not sure works with CHARFORMAT yHeight. The font size is too small. I'm also not sure that EM_SETCHARFORMAT is aware that one is using CHARFORMAT2. Is there a way to convert lfHeight into pixel units. I have to multiply it by 20 to be able to see some chars. Thanks JJ and Hutch
Quote from: xandaz on October 19, 2021, 07:47:17 PM
Well Hutch. I got it to work thanks to your eaxmple. I was using CHARFORMAT2 and turned it to CHARFORMAT and thigs started working.
I've counted 31 hits for CHARFORMAT2 in my RichMasm source, they all work perfectly.
Two problems:
- Your source doesn't assemble on my machine, even with WinClasses.asm included. Since many thousands of other sources assemble perfectly, the reason must be a modification of your Masm32 SDK.
- Your habit of adding flags together, as in
mov chFormat.dwMask,CFM_FACE+CFM_WEIGHT+CFM_COLOR+CFM_SIZE
This will sooner or later bite you. Most Windows flags are well-behaved, but some are not.
Use OR, not +.
yeah. you re right JJ. I usually forget to use OR. Can you tell me why the lfHeight member of LOGFONT doesn,t return in pixels? How to work around
that? ty
I got this code that is present in the help file but the fonts are still too small! invoke GetDeviceCaps,chFont.hDC,LOGPIXELSY
mov ebx,chFont.iPointSize;
xor edx,edx
mul ebx
mov ebx,72
xor edx,edx
div ebx
mov chFormat.yHeight,eax
The original code is:lfHeight = -MulDiv(PointSize, GetDeviceCaps(hDC, LOGPIXELSY), 72);
I must be doing something wrong. The font sizes don't match the ones in ChooseFont Common Dialog.
It seems to be working after all. Thanks!
And btw JJ. I'm using the MASM32 SDK exactly as it came only with the addtion of the winclasses file.
Quote from: xandaz on October 19, 2021, 11:19:10 PM
And btw JJ. I'm using the MASM32 SDK exactly as it came only with the addtion of the winclasses file.
Right, the Masm32 SDK is not the culprit. Line 278 of mdi.asm:
MdiStruct STRUCT
hMdi HWND ?
hToolbar HWND ?
wIDToolbar dd ?
hIml dd ?
hEdit HWND ?
hMenu HWND ?
hFile dd ?
hStatic HWND ?
szFile dw 1024 dup(0)
lpTextBuffer dd ?
cf CHARFORMAT <sizeof CHARFORMAT,<>> ; <<<<<<<<<<<<<<<<<<<<< ????
MdiStruct ENDS
Is it wrongly initialized? I thought worked. The first member of Struct being it's own size: chSTRUCT CHARFORMAT <sizeof CHARFORMAT,<other uninitialized members>. Anyway i don't see a real problem there. The problem is the Font dimension. That's what i was looking to get help for. Thanks
Hey guys. Having a hard time to get CHARFORMAT2 to work. Help is appreciated invoke ClearMemory,esi,sizeof CHARFORMAT2
push hInstance
pop chFont.hInstance
invoke SendMessage,hMdi,WM_MDIGETACTIVE,0,0
mov chFont.hwndOwner,eax
invoke GetDC,[edi].hEdit
mov chFont.hDC,eax
mov [esi].cbSize,sizeof CHARFORMAT2
mov chFont.lStructSize,sizeof CHOOSEFONT
mov chFont.lpLogFont,offset LogFont
mov chFont.nFontType,SCREEN_FONTTYPE
invoke ChooseFont,addr chFont
invoke ReleaseDC,[edi].hEdit,chFont.hDC
invoke GetDeviceCaps,chFont.hDC,LOGPIXELSY
mov ebx,chFont.iPointSize;
xor edx,edx
mul ebx
mov ebx,72
xor edx,edx
div ebx
mov [esi].yHeight,eax
push LogFont.lfWeight
pop [esi].wWeight
mov [esi].dwMask,CFM_FACE OR CFM_SIZE or CFM_WEIGHT or CFM_SIZE
mov [esi].bPitchAndFamily,al
mov [esi].bCharSet,ANSI_CHARSET
invoke CopyBuffer,addr LogFont.lfFaceName,addr [esi].szFaceName
invoke SendMessage,[edi].hEdit,EM_SETCHARFORMAT,SCF_ALL,esi
Here is some functions I have used for richedit to change color and put a wavy red underline under spelling mistakes or coding mistakes or other words or letters of interest:RichSetPos PROTO :DWORD, :DWORD
RichAppend PROTO :DWORD, :DWORD, :DWORD
RichAppendEx PROTO :DWORD, :DWORD, :DWORD
RichAppendW PROTO :DWORD, :DWORD, :DWORD
RichAppendExW PROTO :DWORD, :DWORD, :DWORD
.CODE
;-----------------------------------------------------------------------------------------
; RichSetPos - Set position
;-----------------------------------------------------------------------------------------
RichSetPos PROC hRWnd:DWORD, dwPos:DWORD
LOCAL charrng:CHARRANGE
mov eax, dwPos
mov charrng.cpMin, eax
mov charrng.cpMax, eax
invoke SendMessageW, hRWnd, EM_EXSETSEL, NULL, ADDR charrng
invoke SendMessageW, hRWnd, EM_SCROLLCARET, NULL, NULL ;scroll the text into view
ret
RichSetPos ENDP
;-----------------------------------------------------------------------------------------
; RichAppendW - WIDE - Append text with color to RichEdit control
;-----------------------------------------------------------------------------------------
RichAppendW proc hRWnd:DWORD, pBuff:DWORD,dwColor:DWORD
LOCAL charrng:CHARRANGE
LOCAL charfmt:CHARFORMAT2
Invoke RtlZeroMemory, Addr charfmt, SIZEOF CHARFORMAT2
mov eax, -1
mov charrng.cpMax, eax
mov charrng.cpMin, eax
mov charfmt.cbSize, SIZEOF CHARFORMAT2
mov charfmt.dwMask, CFM_COLOR or CFM_UNDERLINE or CFM_UNDERLINETYPE or CFM_BOLD or CFM_BACKCOLOR
mov charfmt.dwEffects, CFE_AUTOBACKCOLOR
mov charfmt.bUnderlineType, CFU_UNDERLINENONE
mov eax, dwColor
mov charfmt.crTextColor, eax
invoke SendMessageW, hRWnd, EM_EXSETSEL, NULL, ADDR charrng
invoke SendMessageW, hRWnd, EM_SETCHARFORMAT, SCF_SELECTION, ADDR charfmt
invoke SendMessageW, hRWnd, EM_REPLACESEL, FALSE, pBuff
invoke SendMessageW, hRWnd, EM_SCROLLCARET, NULL, NULL ;scroll the text into view
ret
RichAppendW endp
;-----------------------------------------------------------------------------------------
; RichAppendExW - WIDE - Append text with wavy underline and red bolded text to RE control
;-----------------------------------------------------------------------------------------
RichAppendExW proc hRWnd:DWORD, pBuff:DWORD,dwColor:DWORD
LOCAL charrng:CHARRANGE
LOCAL charfmt:CHARFORMAT2
Invoke RtlZeroMemory, Addr charfmt, SIZEOF CHARFORMAT2
mov eax, -1
mov charrng.cpMax, eax
mov charrng.cpMin, eax
mov charfmt.cbSize, SIZEOF CHARFORMAT2
mov charfmt.dwMask, CFM_COLOR or CFM_UNDERLINE or CFM_UNDERLINETYPE or CFM_BOLD or CFM_BACKCOLOR
mov charfmt.dwEffects, CFE_UNDERLINE or CFE_BOLD or CFE_OUTLINE or CFE_AUTOBACKCOLOR
mov charfmt.bUnderlineType, CFU_UNDERLINEHEAVYWAVE
mov eax, dwColor
mov charfmt.crTextColor, eax
mov eax, 0EEEEFFh
mov charfmt.crBackColor, eax
mov charfmt.bReserved1, 5
invoke SendMessageW, hRWnd, EM_EXSETSEL, NULL, ADDR charrng
invoke SendMessageW, hRWnd, EM_SETCHARFORMAT, SCF_SELECTION, ADDR charfmt
invoke SendMessageW, hRWnd, EM_REPLACESEL, FALSE, pBuff
invoke SendMessageW, hRWnd, EM_SCROLLCARET, NULL, NULL ;scroll the text into view
ret
RichAppendExW endp
;-----------------------------------------------------------------------------------------
; RichAppend - ASCII - Append text with color to RichEdit control
;-----------------------------------------------------------------------------------------
RichAppend proc hRWnd:DWORD, pBuff:DWORD,dwColor:DWORD
LOCAL charrng:CHARRANGE
LOCAL charfmt:CHARFORMAT2
Invoke RtlZeroMemory, Addr charfmt, SIZEOF CHARFORMAT2
mov eax, -1
mov charrng.cpMax, eax
mov charrng.cpMin, eax
mov charfmt.cbSize, SIZEOF CHARFORMAT2
mov charfmt.dwMask, CFM_COLOR or CFM_UNDERLINE or CFM_UNDERLINETYPE or CFM_BOLD or CFM_BACKCOLOR
mov charfmt.dwEffects, CFE_AUTOBACKCOLOR
mov charfmt.bUnderlineType, CFU_UNDERLINENONE
mov eax, dwColor
mov charfmt.crTextColor, eax
invoke SendMessage, hRWnd, EM_EXSETSEL, NULL, ADDR charrng
invoke SendMessage, hRWnd, EM_SETCHARFORMAT, SCF_SELECTION, ADDR charfmt
invoke SendMessage, hRWnd, EM_REPLACESEL, FALSE, pBuff
invoke SendMessage, hRWnd, EM_SCROLLCARET, NULL, NULL ;scroll the text into view
ret
RichAppend endp
;-----------------------------------------------------------------------------------------
; RichAppendEx - ASCII - Append text with wavy underline and red bolded text to RE control
;-----------------------------------------------------------------------------------------
RichAppendEx proc hRWnd:DWORD, pBuff:DWORD,dwColor:DWORD
LOCAL charrng:CHARRANGE
LOCAL charfmt:CHARFORMAT2
Invoke RtlZeroMemory, Addr charfmt, SIZEOF CHARFORMAT2
mov eax, -1
mov charrng.cpMax, eax
mov charrng.cpMin, eax
mov charfmt.cbSize, SIZEOF CHARFORMAT2
mov charfmt.dwMask, CFM_COLOR or CFM_UNDERLINE or CFM_UNDERLINETYPE or CFM_BOLD or CFM_BACKCOLOR
mov charfmt.dwEffects, CFE_UNDERLINE or CFE_BOLD ;or CFE_AUTOBACKCOLOR
mov charfmt.bUnderlineType, CFU_UNDERLINEHEAVYWAVE
mov eax, dwColor
mov charfmt.crTextColor, eax
mov eax, 0FF80EFh
mov charfmt.crBackColor, eax
invoke SendMessage, hRWnd, EM_EXSETSEL, NULL, ADDR charrng
invoke SendMessage, hRWnd, EM_SETCHARFORMAT, SCF_SELECTION, ADDR charfmt
invoke SendMessage, hRWnd, EM_REPLACESEL, FALSE, pBuff
invoke SendMessage, hRWnd, EM_SCROLLCARET, NULL, NULL ;scroll the text into view
ret
RichAppendEx endp
Thanks for your examples. I'm still haveing trouble. The EM_SETCHARFORMAT dones't work. It gives ERROR_SUCCESS but there's no change to the edit control. Strangelly CHARFORMAT2 seems to work if I don't use UNICODE. ty