SetFocus ..how to?

Started by Zlatko Vid, April 05, 2023, 10:16:11 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Zlatko Vid

Also thanks Theo
that is very interesting ..but i already try many options
i will chaecki t again
  •  

Pierre Bellisle

Ok,

What if after ReleaseDc(hdc) you add SetFocus(hWnd) ?
  •  

Zlatko Vid

Pierre
ReleaseDC and SetFocus don't have nothing in common
I will post you this simple testing program so anyone can  try:

'unfocus control
$ filename "GUI038.exe"
% WM_NCACTIVATE = 0x0086
' new awinh037
include "rtl32.inc" : include "awinh037.inc" : #lookahead

'globals
INT win,x=0,y=0,w=600,h=430,wstyle = WS_MINMAXSIZE
INT inbox
INT butt, b0ID = 100
INT edit1, editID1 = 101
INT edit2, editID2 = 102
' open window with message loop...
win = SetWindow("GUI_App awinh037",x,y,w,h,0,wstyle)

butt = SetButton(win ,420,320,80,26,"ENTER",0x50001000 | WS_TABSTOP,0x200,b0ID)

edit1 = SetEditBox(win,20,320,380,22,"Enter text...",0x50800000 | WS_TABSTOP | ES_WANTRETURN,0x200,editID1)

'child window
'inbox = SetWindow("Inbox",10,300,230,80, win ,wstyle )
'add edit on child window
'edit2 = SetEditBox(inbox,10,10,150,23,"",0x50800000 | WS_TABSTOP | ES_WANTRETURN,0x200,editID2)
'setfocus inbox

Wait()  '///  message loop ///

Function WndProc(sys hwnd,wmsg,wparam,lparam) as sys callback
win = hwnd 'assign WIN with HWND
SELECT hwnd
CASE win
Select wmsg

    case wm_create
'button0 = SetButton(win,10,40,80,26,"Close (X)",0x50001000,0x200,b0ID)
    case wm_keydown
    'SetFocus butt

    if wParam = 13
      print "VK_ENTER"
     ' SetFocus butt
       'SendMessage WM_KILLFOCUS,edit1,0
    end if
/*
    CASE WM_NCACTIVATE
         STATIC hWndSaveFocus AS DWORD
            IF WPARAM = 0
                ' Save control focus
                hWndSaveFocus = GetFocus()
            ELSEIF hWndSaveFocus
                ' Restore control focus
                SetFocus(hWndSaveFocus)
                hWndSaveFocus = 0
            END IF  */                     

CASE WM_CLOSE
CloseWindow(win)
EndProgram
End Select
END SELECT


RETURN Default
END FUNCTION

  •  

Bernard Kunzy

#18
Once you press the enter key, you can put the focus on another control (even a hidden control).
Or you can use something like this, to simulate a mouse click on another control.

void PutFocusOn(IN HWND nObjectHandle, IN LPARAM lParam = 0) {
    PostMessage(nObjectHandle, WM_LBUTTONDOWN, 0, lParam);
    PostMessage(nObjectHandle, WM_LBUTTONUP, 0, lParam);
}
  •  

Zlatko Vid

Hi Bernard,Pierre...all
I understand what you say ...and i even can add hidden control in my interpreter
that is not problem , but would be better to set focus back to window..
In Pierre program ..main trick is with IDOK and have exactly what i want .
So i must test this first ..

thanks guys !!!
  •  

Zlatko Vid

Oh what a snap
Not work work without IsDialogMesage
  •  

Zlatko Vid

After try/error/not work ... things
it looks that work using IsDialogMessage(hwnd,msg)

I am not sure if i can add it into interpreter
but looks that work as native app ,ok looks like this :
and i hope that some of you try and find it useful ;)

'unfocus control
$ filename "GUI038.exe"
% WM_NCACTIVATE = 0x0086
' new awinh037
include "rtl32.inc" : include "awinh037.inc" : #lookahead
Declare Function IsDialogMessage Lib "user32.dll" Alias "IsDialogMessageA" (ByVal hDlg As Long, lpMsg As MSG) As Long

'globals
INT win,x=0,y=0,w=600,h=430,wstyle = WS_MINMAXSIZE
INT inbox
INT butt
INT edit1, editID1 = 101
INT edit2, editID2 = 102
% b0ID = 100
' open window...
win = SetWindow("GUI_App awinh037",x,y,w,h,0,wstyle)

butt = SetButton(win ,420,320,80,26,"ENTER",0x50001000 | WS_TABSTOP  ,0x200,b0ID)

edit1 = SetEditBox(win,20,320,380,22,"Enter text...",0x50800000| WS_TABSTOP  ,0x200,editID1)

'///  message loop ///
 WHILE GetMessage (wm,0,0,0)<>0
    if IsDialogMessage(win,wm)=0
       TranslateMessage wm
       DispatchMessage wm
    end if
 WEND

Function WndProc(sys hwnd,wmsg,wparam,lparam) as sys callback
win = hwnd 'assign WIN with HWND
SELECT hwnd
CASE win
Select wmsg

    case wm_create
'button0 = SetButton(win,10,40,80,26,"Close (X)",0x50001000,0x200,b0ID)
    CASE WM_KEYDOWN
    'SetFocus butt
    if wParam = 13
      print "VK_ENTER"
     'InvalidateRect(edit1, 0, 1)
   end if

   CASE WM_LBUTTONDOWN
        InvalidateRect(win, 0, 1)
        'print "invalid"

   CASE WM_COMMAND
       controlID = LoWord(wParam) 'get control ID
       notifyCode = HiWord(wParam) 'get notification message

      select controlID

         case b0ID
           if notifycode = BN_CLICKED OR notifycode = 1
               print "ENTER PRES!"
           end if

         case IDOK
            if  notifycode = 0
                'print "IDOK-1"
                'InvalidateRect(edit1, 0, -1)
                SendMessage edit1,WM_SETTEXT,0,""
            end if

         case IDCANCEL
            print "ESCAPE"


     end select
       

CASE WM_CLOSE
CloseWindow(win)
EndProgram
End Select
END SELECT


RETURN Default
END FUNCTION

  •  

Pierre Bellisle

QuotePierre, ReleaseDC and SetFocus don't have nothing in common...

Aurel, I never said it have.
ReleaseDc(hdc) is there to take care of sys hdc = GetDC(hWnd).
  •  

Zlatko Vid

OK Pierre i get it ..
but in overall it is not anything connected with HDC
i have probem with IDOK . so IDOK message not work if IsDialogMessage is not added into message loop
Please if you have time check next program , in order to work add include file awinh037.inc
and tell me how work ?
seems all work fine ..only strange problem i have with CRLF which NOT execute after text then i need to add it to buffer
before text  ???
that looks crazy :

'unfocus control
$ filename "GUI038.exe"
% WM_NCACTIVATE = 0x0086
' new awinh037
include "rtl32.inc" : include "awinh037.inc" : #lookahead
Declare Function IsDialogMessage Lib "user32.dll" Alias "IsDialogMessageA" (ByVal hDlg As Long, lpMsg As MSG) As Long

'globals
INT win,x=0,y=0,w=600,h=430,wstyle = WS_MINMAXSIZE
INT inbox
INT butt, b0ID = 100
INT edit1, editID1 = 101
INT edit2, editID2 = 102
string CRLF = chr(13)+chr(10)
string txbuff = space(256)
char buff[4096]   
' open window...
win = SetWindow("GUI_App awinh037",x,y,w,h,0,wstyle)

butt = SetButton(win ,420,320,80,26,"ENTER",0x50001000 | WS_TABSTOP  ,0x200,b0ID)
'single line edit control
edit1 = SetEditBox(win,20,320,380,23,"Enter text...",0x50800000| WS_TABSTOP  ,0x200,editID1)
ControlFont(edit1, 17,7, 100, "Courier New")
'multi line edit control
edit2 = SetEditBox(win,20,40,400,200,"",WS_CHILD|WS_VISIBLE|ES_MULTILINE|ES_WANTRETURN|WS_VSCROLL|WS_HSCROLL|ES_AUTOVSCROLL,0x200,editID2)
ControlFont(edit2, 16,8, 100, "Consolas")

'///  message loop ///
 WHILE GetMessage (wm,0,0,0)<>0
    if IsDialogMessage(win,wm)=0
       TranslateMessage wm
       DispatchMessage wm
    end if
 WEND

Function WndProc(sys hwnd,wmsg,wparam,lparam) as sys callback
win = hwnd 'assign WIN with HWND
SELECT hwnd
CASE win
Select wmsg

    case wm_create
'button0 = SetButton(win,10,40,80,26,"Close (X)",0x50001000,0x200,b0ID)
    CASE WM_KEYDOWN
    'SetFocus butt
    if wParam = 13
      print "VK_ENTER"
     'InvalidateRect(edit1, 0, 1)
   end if

   'CASE WM_LBUTTONDOWN
    '    InvalidateRect(win, 0, 1)
        'print "invalid"

   CASE WM_COMMAND
       controlID = LoWord(wParam)  'get control ID
       notifyCode = HiWord(wParam) 'get notification message

      select controlID

         case b0ID
           if notifycode = BN_CLICKED OR notifycode = 1
               print "ENTER PRES!"
           end if

         case IDOK
            if  notifycode = 0
                'get text and put it into buffer
                SendMessage edit1, WM_GETTEXT, 256 , txbuff
                buff = buff + crlf + txbuff
               ' print buff
                SetText(edit2,buff  )
                'clear control edit1
                SendMessage edit1, WM_SETTEXT, 0, ""
               
                'set text to multi-line
                'SendMessage edit2, WM_SETTEXT, 0,strptr(txbuff) + CRLF
               
            end if

         case IDCANCEL
            print "ESCAPE"


     end select
       

CASE WM_CLOSE
CloseWindow(win)
EndProgram
End Select
END SELECT


RETURN Default
END FUNCTION
  •  

Pierre Bellisle

Quotebut in overall it is not anything connected with HDC
Quotegeneral idea is to type text in edit control and then print that text on window
using GDI ...
Hi Aurel,

I never said that the your problem was related to any DC.

This is what I used in my demo to  print that text on window,
sys hdc = GetDC(hWnd)
SetBkMode(hDC, TRANSPARENT)
TextOutW(hdc, 20, 30, sText, TextLen)
ReleaseDc(hdc)

and I did put it because you said 
"general idea is to type text in edit control and then print that text on window"
I wanted an example as clear as possible.
I guess it is very difficult to understand each other...
I will have to work harder.

  •  

Pierre Bellisle

#25
Aurel the awinh037 inc I got is 61,927 bytes, dated 2022-02-25 21:42.
Is it the right version?
I got it from AurelEdit
  •  

Pierre Bellisle

#26
Hi Aurel,
if I understand, the focus() thing belong to the past and we are treating new stuff.
Great!
About strange problem i have with CRLF which NOT execute after text then i need to add it to buffer before text.

I use the following code, it have the advantage to automatically scroll the text when needed. One more  advantage is you do not have to copy the text back every time from edit1.

string sBuffer
long BufferLen
BufferLen = SendMessage(edit1, WM_GETTEXTLENGTH, 0, 0)    'Get character count
sBuffer   = nuls(BufferLen)                               'Build a buffer to get it
SendMessage(edit1, WM_GETTEXT, BufferLen + 1, sBuffer)    'Get the text
SendMessage(edit2, %EM_SETSEL, -1, -1)                    'Move caret to the end
SendMessage(edit2, %EM_REPLACESEL, %TRUE, sBuffer & crlf) 'Insert text there
SendMessage(edit1, %WM_SETTEXT, 0, BYVAL 0)               'Reset input edit

  •  

Zlatko Vid

Hi Pierre

It is about focus thing
I am not sure why you don't understand ?

Maybe my english really sucks...

Of course that is about focus and without IsDialogWindow ..IDOK message not work
yes rgis is only good way to get it properly and seems that work as it should be
and maybe you skip point where i was talking that i need it for my interpreter micro(A). 
So when IDOK work ..focus or better to say carret is still in edit control and enable processing
what is under IDOK..i hope that you understand me now .
I don't need this functionality directly for OxygenBasic than indirectly for interpreter
which is written in Oxygenbasic...but as such could be useful in some other programs too.
all best
  •  

Pierre Bellisle

#28
About your last posted code, you said:
seems all work fine ..only strange problem i have with CRLF which NOT execute after text then i need to add it to buffer
before text

If all works fine then it does what you want (except for the CRLF where I gave solution in my previous post)
but in this code, there is nothing special about focus.

Maybe, when your program start, you do not want IDOK to work if there is no focus/caret on edit1.
If it is so, try this...

case IDOK
  if notifyCode = BN_CLICKED OR notifyCode = 1 then
    if getfocus() = edit1 'Only if focus/caret is on edit1
      long TextLen = SendMessageW(edit1, WM_GETTEXTLENGTH, 0, 0) 'Get character count (0x0E)
      string sBuffer = nuls(TextLen)                             'Build a buffer to get it
      SendMessage(edit1, WM_GETTEXT, TextLen + 1, sBuffer)       'Get the text
      SendMessage(edit2, %EM_SETSEL, -1, -1)                     'Move caret to the end
      SendMessage(edit2, %EM_REPLACESEL, %TRUE, sBuffer & crlf)  'Insert text there
      SendMessage(edit1, %WM_SETTEXT, 0, BYVAL 0)                'Reset input edit
    end if
  end if


Else, if anybody see what I don't, please do tell...
  •  

Zlatko Vid

Hi Pierre
all is fine
i must test all this in my interpreter
  •