flat assembler
Message board for the users of flat assembler.

Index > Windows > EM_CHARFROMPOS problems

Author
Thread Post new topic Reply to topic
moriman



Joined: 01 Apr 2006
Posts: 55
Location: Northern Ireland
moriman
Hi,

The following code creates a richedit control and fills it with some lines of text.
When the left mouse button is clicked in the control the main window text should be set to the 0 based text line number of the click.

It crashes on the SendMessage -> EM_CHARFROMPOS Confused
I have tried setting the handle for the message as the edit control, as I assume is correct, it crashes. I have tried setting the handle for the message as the main window; it crashes.

I have also found that the FASM user32.inc for EM_CHARFROMPOS=0x00D7 in accordance with the SDK WinUser.h...
but...
the SDK RichEdit.h has EM_CHARFROMPOS defined as WM_USER+39 which translates to 0x0427

I tried sending the message with this WM_USER+39 value. When I use the handle of the main window it doesn't crash but always returns 0, no matter what position is clicked. Again, using the edit control's handle the app crashes.

A search here for EM_CHARFROMPOS gives no results so any help would be greatly appreciated Smile

Code:
format PE GUI 4.0
entry start

include '%fasminc%\win32a.inc'
include '%fasminc%\Riched32.inc'
section '.code' code readable executable

  start:
        invoke  LoadLibrary, RichEditDllName
        invoke  GetModuleHandle,0
        mov     [wc.hInstance],eax
        invoke  RegisterClass, wc
        invoke  CreateWindowEx,0,myClass,myTitle,WS_VISIBLE+WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, CW_USEDEFAULT, 700, 400, NULL, NULL, [wc.hInstance],NULL
        mov     [hWndMain], eax
        invoke  InitCommonControls
        invoke  GetCurrentProcess
        mov     [hProcess], eax
  msg_loop:
        invoke  GetMessage,msg,NULL,0,0
        or      eax,eax
        jz      end_loop
        invoke  TranslateAccelerator, [hWndMain], [hAccel], msg
        or      eax, eax
        jnz     msg_loop
        invoke  IsDialogMessage, [hWndMain], msg
        or      eax, eax
        jnz     msg_loop
        invoke  TranslateMessage,msg
        invoke  DispatchMessage,msg
        jmp     msg_loop
  end_loop:
        invoke  ExitProcess,[msg.wParam]

proc wndProc hwnd,wmsg,wparam,lparam
        push    ebx esi edi
        cmp     [wmsg],WM_DESTROY
        je      .wmDESTROY
        cmp     [wmsg],WM_CREATE
        je      .wmCREATE
        cmp     [wmsg], WM_NOTIFY
        je      .wmNOTIFY
    .wmDEFAULT:
        invoke  DefWindowProc,[hwnd],[wmsg],[wparam],[lparam]
        jmp     .wmBYE
    .wmNOTIFY:
        mov     eax, [lparam]
        cmp     [eax+MSGFILTER.msg], WM_LBUTTONDOWN
        je      .wmNwmLBD
        jmp     .wmDEFAULT

;**************  PROBLEM AREA **********************

    .wmNwmLBD:
        int3            ; the four alternatives I've tried.
        invoke  SendMessage, [editHnd], EM_CHARFROMPOS, 0, [eax+MSGFILTER.lparam]
;        invoke  SendMessage, [hWndMain], EM_CHARFROMPOS, 0, [eax+MSGFILTER.lparam]
;        invoke  SendMessage, [editHnd], reEM_CHARFROMPOS, 0, [eax+MSGFILTER.lparam]
;        invoke  SendMessage, [hWndMain], reEM_CHARFROMPOS, 0, [eax+MSGFILTER.lparam]

;***************************************************

        shr     eax, 16         ;0 based line No. should now be in eax
        stdcall Word2Asc, eax, tmpBuff
        invoke  SetWindowText, [hWndMain], tmpBuff
        jmp     .wmDEFAULT
    .wmCREATE:
        invoke  GetClientRect, [hwnd], rect
        invoke  CreateWindowEx, WS_EX_CLIENTEDGE, RichEditClass, NULL, WS_CHILD+WS_VISIBLE+WS_VSCROLL+WS_HSCROLL+ES_AUTOVSCROLL+ES_AUTOHSCROLL+ES_MULTILINE+ES_WANTRETURN, [rect.left], [rect.top], [rect.right], [rect.bottom], [hwnd], NULL, [wc.hInstance], NULL
        mov     [editHnd], eax
        invoke  SendMessage,[editHnd],EM_SETEVENTMASK,0,ENM_MOUSEEVENTS ;capture mouse events in edit control
        invoke  SetWindowText, [editHnd], tmpStr
        jmp     .wmBYE
    .wmDESTROY:
        invoke  PostQuitMessage,0
        xor     eax,eax
    .wmBYE:
        pop     edi esi ebx
        ret
endp

proc Word2Asc HexVal, buff
        push    eax ebx ecx edx edi
        mov     eax, [HexVal]
        mov     ebx, 0x0A
        mov     ecx, 5
    @@:
        xor     edx, edx
        div     ebx
        add     edx, 0x30
        push    edx
        loop    @b
        mov     edi, [buff]
        mov     ecx, 5
    @@:
        pop     eax
        stosb
        loop    @b
        pop     edi edx ecx ebx eax
        ret
endp


section '.data' data readable writeable

reEM_CHARFROMPOS        = WM_USER+39
tmpBuff                 db 5 dup 'X', 0
editHnd                 dd ?
hWndMain                dd ?
hProcess                dd ?
hAccel                  dd ?
msg                     MSG
rect                    RECT
wc                      WNDCLASS 0, wndProc, 0, 0, NULL, NULL, NULL, COLOR_BTNFACE+1, NULL, myClass
RichEditDllName         db 'riched20.DLL', 0
RichEditClass           db 'RICHEDIT20A', 0
myTitle                 db 'Test', 0
myClass                 db 'Test', 0
ENM_MOUSEEVENTS         = 0x00020000
tmpStr                  db 'A line of text in the window', 0x0D, 0x0A
                        db 'A line of text in the window', 0x0D, 0x0A
                        db 'A line of text in the window', 0x0D, 0x0A
                        db 'A line of text in the window', 0x0D, 0x0A
                        db 'A line of text in the window', 0x0D, 0x0A
                        db 'A line of text in the window', 0x0D, 0x0A
                        db 'A line of text in the window', 0x0D, 0x0A
                        db 'A line of text in the window', 0x0D, 0x0A
                        db 'A line of text in the window', 0x0D, 0x0A
                        db 'A line of text in the window', 0x0D, 0x0A
                        db 'A line of text in the window', 0x0D, 0x0A, 0
struct  MSGFILTER
        nmhdr           NMHDR
        msg             dd ?
        wparam          dd ?
        lparam          dd ?
ends


section '.idata' import data readable writeable

library kernel32,'KERNEL32.DLL',\
        user32,  'USER32.DLL',\
        gdi32,   'GDI32.DLL',\
        comdlg32,'COMDLG32.DLL',\
        advapi32,'ADVAPI32.DLL',\
        comctl32,'COMCTL32.DLL'

include '%fasminc%\apia\Kernel32.inc'
include '%fasminc%\apia\User32.inc'
include '%fasminc%\apia\Gdi32.inc'
include '%fasminc%\apia\Comdlg32.inc'
include '%fasminc%\apia\Advapi32.inc'
include '%fasminc%\apia\Comctl32.inc'
    
Post 07 Aug 2006, 22:14
View user's profile Send private message Reply with quote
moriman



Joined: 01 Apr 2006
Posts: 55
Location: Northern Ireland
moriman
OK,

EM_CHARFROMPOS expects the lparam to be a pointer to a POINT structure. When I altered this in the above code then none of my four alternatives crashes Smile

Using the main windows handle returns 0 so isn't correct, as I assumed.
Using the edit controls handle with the above I get a partial result.

According to the WinAPI.hlp

when EM_CHARFROMPOS returns...

Quote:

The return value specifies the character index in the low-order word and the line index in the high-order word


but all I am getting is a character count with no line count;
i.e

If I click between the 1st line's e & x (of text) I get Line 0 Char 12
If I click between the 2nd line's e & x I get Line 0 Char 41

I have attached the amended code which now doesn't crash in case someone can help Confused

btw, It appears that whether I use EM_CHARFROMPOS (0x00D7) or WM_USER+39 (0x0427) makes absolutely no difference Question

thx


Description:
Download
Filename: test.asm
Filesize: 5.34 KB
Downloaded: 55 Time(s)

Post 07 Aug 2006, 23:19
View user's profile Send private message Reply with quote
moriman



Joined: 01 Apr 2006
Posts: 55
Location: Northern Ireland
moriman
Got a workaround but still can't figure what I'm doing wrong with EM_CHARFROMPOS Confused

Workaround...

Code:
.
.
invoke  SendMessage, [editHnd], EM_CHARFROMPOS, 0, pt
invoke  SendMessage, [editHnd], EM_LINEFROMCHAR, eax, 0
.
.
    


returns the 0 based line number Very Happy
Post 07 Aug 2006, 23:46
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar.

Powered by rwasa.