How to Force window_1 to have Focus ?

Just starting out? Need help? Post your questions and find answers here.
vmars316
Enthusiast
Enthusiast
Posts: 474
Joined: Fri Jun 29, 2012 12:24 am
Contact:

How to Force window_1 to have Focus ?

Post by vmars316 »

Hello & Thanks ,
I have a two window program (Window_0 and Window_1) , (pimClip.pb).
Whenever there is a 'copy text' to the clipboard ,
this code opens up Window_1 :

Code: Select all

If OpenFile(0,cliplogname$,#PB_File_Append |#PB_Ascii)
    LogThisString$ = (dttime$ + " " + "NoTopic: " + clipstring$)
    SaveClipText$ = clipstring$ ;   Wait til SelectTopic Window_1 clicks then use SaveClipText$
    If showTopicsCkBoxState
      HideWindow(Window_1, #False) ; show Window_1 
      SetActiveWindow(Window_1)
      SetWindowPos_(WindowID(Windomw_1),#HWND_TOPMOST,0,0,0,0,#SWP_NOMOVE|#SWP_NOSIZE) ; Make Window_1 OnTop 
    Else 
     WriteStringN(0,(LogThisString$), #PB_Ascii) ; no showTopicsCkBox checked
    EndIf ; If showTopicsCkBoxState
  CloseFile(0)
EndIf ;If OpenFile(0,cliplogname$

The StringGadget (NewTopic) shows the cursur blinking ,
But the Window_1 does not have the Focus .
How can I force Window_1 to have the Focus ?
If I manually click Window_1 , all works as intended .
I need Window_1 to show Focus , so user doesn't have to click it first .

Code: Select all

Repeat code :

     If WindowID = Window_1 
       Select Event
         Case #PB_Event_Gadget
           Select EventGadget()
             Case 1 To MaxBtnCount
               TopicBtn_clicked()
             Case NewTopic
               Select EventType()
                 Case #PB_EventType_Focus
                   SetActiveWindow(Window_1)
                   AddKeyboardShortcut(Window_1, #PB_Shortcut_Return, 1)
                   Debug "Case #PB_EventType_Focus" 
                 Case #PB_EventType_LostFocus
                   RemoveKeyboardShortcut(Window_1, #PB_Shortcut_Return)
               EndSelect ; Select EventType()
             Case EditListBtn
                    RunProgram("TopicsBtnList.txt")
           EndSelect ; Select EventGadget()
        
           Case #PB_Event_Menu
;                 MessageRequester("Select EventMenu()","Select EventMenu()= "+Str(EventMenu()))
             Select EventMenu()
               Case 1
                 SelectedTopicText$ = GetGadgetText(NewTopic)
;                 MessageRequester("NewTopicText$ = ",SelectedTopicText$ )
                 SetActiveGadget(NewTopic)
                 LogSelectedTopic()
            EndSelect ; Select EventMenu()
         Case #PB_Event_CloseWindow
              CloseWindow(Window_0) 
              CloseWindow(Window_1) 
         EndSelect     ; Select Event           
     EndIf           ; WindowID = #Window_0         
ForEver          ; OpenWindow_0()
End 
vmars.us Win11 x64 , Martin Guitar 000-16 (1995)
"All things in moderation , except for love and forgiveness."
User avatar
skywalk
Addict
Addict
Posts: 4217
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: How to Force window_1 to have Focus ?

Post by skywalk »

Code: Select all

Macro win_SetActiveWin(wN)
  ; Using native PB commands.
  StickyWindow(wN,1)
  StickyWindow(wN,0)
  ; Using Windows API
  ;SetForegroundWindow_(hAP)  ; Works best!
  ;BringWindowToTop_(hAP)     ; Works for Gadgets too!
  ;SetWindowPos_(hAP,#HWND_TOPMOST,0,0,0,0,#SWP_NOMOVE | #SWP_NOSIZE) ; Yes, but forces Sticky Window behavior
  ;SetWindowPos_(hAP,#HWND_TOP,0,0,0,0,#SWP_NOMOVE | #SWP_NOSIZE)     ; No, Forces Sticky Window behavior
  ;SetActiveWindow_(hAP)  ; No, Window remains behind others   
EndMacro
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
vmars316
Enthusiast
Enthusiast
Posts: 474
Joined: Fri Jun 29, 2012 12:24 am
Contact:

Re: How to Force window_1 to have Focus ?

Post by vmars316 »

Thanks ,
I tried all these , but none gave Focus to Window_1 ,
not even 'SetForegroundWindow_(WindowID(Window_1))' .

I am thinking that copying to clipboard has something to do with prob .
When I do a Cntrl-C (from , say , notepad) , Window_1 displays fine ,
but notepad keeps its Focus .

So I have Shrunk the code down to the following , and hope someone can run it and
help me find the error :

TopicBtnList.txt looks like this:
Addr/Phone
Amazon
Appt.
The Debug Log looks like this:
Case NewTopic
Case #PB_EventType_Focus
Case NewTopic
Case NewTopic
Case #PB_EventType_LostFocus
Case NewTopic
Case showTopicsCkBox

Procedure CallBack( & If IsClipboardFormatAvailable_(#CF_TEXT)

Procedure LogSomething() & GetDate() & If d$ <> OpenClipLogName$

If OpenFile(0,cliplogname$, & If showTopicsCkBoxState

Case NewTopic
Case #PB_EventType_Focus
Case NewTopic
Case NewTopic
Case #PB_EventType_LostFocus
Case NewTopic
Case NewTopic
Case #PB_EventType_Focus
Case NewTopic
And here is the code:

Code: Select all

; pimClip-Shrunk.pb
;}
;{ Gadgets
;}
Global Window_0 = 0
Global ClipboardViewerNext.i, showTopicsCkBox, RestartPgm = #False, SaveClipText$
Global clipstring$, d$, getdatecnt, dttime$, showTopicsCkBoxState = #False, LogThis = #False
Global ClipLogName$="****",OpenClipLogName$="00000000", selectedTopic$, Event, WindowID, Result
Global TrayIcon = 1, IconName$ = "myFavs48x48.ico"
Global sendToBtn, findBtn, exitBtn, helpBtn, restartBtn
;
Global Window_1 = 1
Global TopicsListFile$ = "TopicsBtnList.txt", TopicBtnName$, TopicBtnCount.l = 1, LogThisString$
Global TopicContainer, NewTopic, EditListBtn, Btn_1, Btn_2, Btn_3 
Global SelectedTopicText$, Window_1_Width.l = 250, Window_1_Height.l = 640, Window_1_Xpos,
       Window_1_Ypos, MaxBtnCount.l = 3 
;
Declare OpenWindow_0()
Declare OpenWindow_1()
Declare LogSomething()
Declare GetDate()
Declare CallBack(hWnd, Msg, wParam, lParam)
Declare CloseWindow_window_0()
Declare LogSelectedTopic()
Declare SetupTopicButtons()
;
Procedure OpenWindow_0()  
    OpenWindow(Window_0, 0, 0, 448, 110, "pimClip 2.0", #PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_TitleBar) ; |#PB_Window_ScreenCentered)
    SetWindowColor(Window_0, RGB(192,192,192))
    SetWindowPos_(WindowID(Window_0),#HWND_TOPMOST,0,0,0,0,#SWP_NOMOVE|#SWP_NOSIZE)
    exitBtn = ButtonGadget(#PB_Any, 260, 20, 80, 28, "EXIT", #PB_Button_Default)
    showTopicsCkBox = CheckBoxGadget(#PB_Any, 260, 63, 168, 30, "Prompt for Topics List", #PB_CheckBox_Center | #PB_Checkbox_Unchecked)
EndProcedure ; OpenWindow_0()  
;
Procedure OpenWindow_1()
  Window_1_Xpos = (GetSystemMetrics_(#SM_CXSCREEN) - Window_1_Width) ; horizontal width.
  Window_1_Ypos = (GetSystemMetrics_(#SM_CYSCREEN) - Window_1_Height) ;  vertical height.
  If OpenWindow(Window_1, Window_1_Xpos, 0, Window_1_Width, Window_1_Height, "Enter or Select Topic", #PB_Window_SystemMenu| #PB_Window_TitleBar  | #PB_Window_SizeGadget)
    SetWindowColor(Window_1, RGB(192,192,192))
    SetWindowPos_(WindowID(Window_1),#HWND_TOPMOST,0,0,0,0,#SWP_NOMOVE|#SWP_NOSIZE) 
    TopicContainer = ContainerGadget(#PB_Any, 12, 10, 222, 584)
    NewTopic = StringGadget(#PB_Any, 10, 10, 100, 24, " ")
    EditListBtn = ButtonGadget(#PB_Any, 10, 42, 100, 24, "Edit Topic List")
    Btn_1 = ButtonGadget(1, 10, 96, 100, 24, "")
    Btn_2 = ButtonGadget(2, 10, 120, 100, 24, "")
    Btn_3 = ButtonGadget(3, 10, 144, 100, 24, "")
    CloseGadgetList()
  EndIf ; If OpenWindow(Window_1
  HideWindow(Window_1, #True)
EndProcedure
;
Procedure TopicBtn_clicked()
  SelectedTopicText$ = GetGadgetText(EventGadget())
  MessageRequester("BtnGadgetText$","BtnGadgetText$  " + SelectedTopicText$)
  LogThis = #True 
  LogSelectedTopic()
  LogThis = #False
  HideWindow(Window_1, #True)
EndProcedure ; TopicBtn_clicked()
;
Procedure GetDate()
GetDateCnt+1
   d$=FormatDate("%yyyy-%mm-%dd",Date())
   dttime$=FormatDate("%hh:%ii:%ss",Date())
; SetWindowTitle(Window_0, "pimLog 1.0 "+d$+".txt") 
EndProcedure  ; GetDate()
;
Procedure LogSelectedTopic()
GetDate()
If d$ <> OpenClipLogName$ ; file could have been closed but Exists
     ClipLogName$ = (d$ + ".txt")
     OpenClipLogName$ = ClipLogName$
     Debug "IF d$ <> OpenClipLogName$"
EndIf  ; IF d$ <> OpenClipLogName$
;
If OpenFile(0,cliplogname$,#PB_File_Append |#PB_Ascii)
       WriteStringN(0,(dttime$ + " " + SelectedTopicText$ +  ": " + SaveClipText$), #PB_Ascii) ; no showTopicsCkBox checked 
;       MessageRequester("WriteStringN(0,","Logging = " + dttime$ + " " + SelectedTopicText$ +  " " + SaveClipText$)
       LogThis = #False 
       HideWindow(Window_1, #True)
       Debug "OpenFile(0,cliplogname$ & WriteStringN(0,(dttime$"
       CloseFile(0)
EndIf
EndProcedure ; LogSelectedTopic()
;
Procedure LogSomething()
GetDate()
If d$ <> OpenClipLogName$ ; file could have been closed but Exists
     ClipLogName$ = (d$ + ".txt")
     OpenClipLogName$ = ClipLogName$
  Debug "Procedure LogSomething() & GetDate() & If d$ <> OpenClipLogName$ "
EndIf  ; IF d$ <> OpenClipLogName$
;
If OpenFile(0,cliplogname$,#PB_File_Append |#PB_Ascii)
    LogThisString$ = (dttime$ + " " + "NoTopic: " + clipstring$)
    SaveClipText$ = clipstring$ ;   Wait til SelectTopic Window_1 clicks then use SaveClipText$
    If showTopicsCkBoxState
      Debug "If OpenFile(0,cliplogname$, & If showTopicsCkBoxState"
      HideWindow(Window_1, #False) ; show Window_1 
      SetActiveWindow(Window_1)  ;  focus has been put on this window
      SetWindowPos_(WindowID(Windomw_1),#HWND_TOPMOST,0,0,0,0,#SWP_NOMOVE|#SWP_NOSIZE) ; Make Window_1 OnTop 
    Else 
      WriteStringN(0,(LogThisString$), #PB_Ascii) ; no showTopicsCkBox checked
      Debug "If OpenFile(0,cliplogname$, & WriteStringN(0,(LogThisString$)"
    EndIf ; If showTopicsCkBoxState
  CloseFile(0)
EndIf ;If OpenFile(0,cliplogname$
EndProcedure ; LogSomething() stickywindow
;
Procedure CallBack(hWnd, Msg, wParam, lParam)
  Protected result
  Result = #PB_ProcessPureBasicEvents
  Select Msg
    Case #WM_DRAWCLIPBOARD
      If IsClipboardFormatAvailable_(#CF_TEXT)
        Debug "Procedure CallBack( & If IsClipboardFormatAvailable_(#CF_TEXT)"
        clipstring$=GetClipboardText()
        LogSomething() ; ============================================== LogSomething() 
        clipString$=""
      EndIf
      SendMessage_(ClipboardViewerNext, Msg, wParam, lParam)
    Case #WM_CHANGECBCHAIN
      If wParam = ClipboardViewerNext
        ClipboardViewerNext = lParam
      Else
        SendMessage_(ClipboardViewerNext, Msg, wParam, lParam)
      EndIf
  EndSelect
  ProcedureReturn Result
EndProcedure ; CallBack(hWnd, Msg, wParam, lParam)

Procedure CloseWindow_window_0()
  RemoveSysTrayIcon(TrayIcon)
  ChangeClipboardChain_(WindowID(Window_0), ClipboardViewerNext)
  If RestartPgm 
    RunProgram("pimClip.exe") 
  EndIf 
    CloseWindow(Window_0) 
EndProcedure ; CloseWindow_window_0()
;             
Procedure SetupTopicButtons() ; Run once at beginning ==========
  If ReadFile(0, TopicsListFile$) 
    TopicBtnCount = 1
    Repeat 
      TopicBtnName$ = ReadString(0) 
      SetGadgetText(TopicBtnCount,TopicBtnName$)
      TopicBtnCount = TopicBtnCount + 1 
  Until Eof(0) | TopicBtnCount > MaxBtnCount 

  CloseFile(0) 
EndIf
EndProcedure ; SetupTopicButtons() ==============================  
;-begin       
OpenWindow_0()
  If IsClipboardFormatAvailable_(#CF_TEXT) 
     ClearClipboard()
     clipString$=""
  EndIf
SetWindowCallback(@callback())
ClipboardViewerNext = SetClipboardViewer_(WindowID(Window_0))
  AddSysTrayIcon(TrayIcon, WindowID(0), LoadImage(0, IconName$))
  SysTrayIconToolTip(TrayIcon, "LeftDouble_Click to Open pimClip WIndow")
;  
OpenWindow_1()
SetActiveGadget(NewTopic)
AddKeyboardShortcut(Window_1, #PB_Shortcut_Return, 1)  
SetupTopicButtons() 
; =====================================
;{- Event loop
Repeat
    Event = WaitWindowEvent()
    WindowID = EventWindow()
    ; ///////////////////
    If WindowID = Window_0 
;       Debug "Window_0 Event" + "#PB_Event_Gadget = " + Str(#PB_Event_Gadget)
       Select Event
         Case #PB_Event_Gadget
           Select EventGadget()
             Case exitBtn
               CloseWindow_window_0()
               Break
             Case showTopicsCkBox
               Result = GetGadgetState(showTopicsCkBox)
               showTopicsCkBoxState = Result
               MessageRequester( "Case showTopicsCkBox" , "Case showTopicsCkBox = "  + Str(Result) ) 
               Debug "Case showTopicsCkBox"  + Str(Result)  
           EndSelect ; EventGadget()
; ////////////////////////
         Case #PB_Event_SysTray
           If EventType() = #PB_EventType_LeftDoubleClick
              HideWindow(Window_0, #False)
           EndIf
         Case #PB_Event_CloseWindow
              CloseWindow(Window_0) 
              CloseWindow(Window_1) 
       EndSelect ; Window_0 Event 
     EndIf       ; If WindowID = Window_0   
     
     If WindowID = Window_1 
       Select Event
         Case #PB_Event_Gadget
           Select EventGadget()
             Case 1 To MaxBtnCount
               Debug "Case 1 To MaxBtnCount"
               TopicBtn_clicked()
             Case NewTopic
               Debug "Case NewTopic"
               Select EventType()
                 Case #PB_EventType_Focus
                   SetActiveWindow(Window_1)
                   AddKeyboardShortcut(Window_1, #PB_Shortcut_Return, 1)
                   Debug "Case #PB_EventType_Focus" 
                 Case #PB_EventType_LostFocus
                   RemoveKeyboardShortcut(Window_1, #PB_Shortcut_Return)
                   Debug "Case #PB_EventType_LostFocus"
               EndSelect ; Select EventType()
             Case EditListBtn
                    RunProgram("TopicsBtnList.txt")
           EndSelect ; Select EventGadget()
        
         Case #PB_Event_Menu
             Debug "Case #PB_Event_Menu"
;                 MessageRequester("Select EventMenu()","Select EventMenu()= "+Str(EventMenu()))
             Select EventMenu()
               Case 1
                 Debug "Select EventMenu() & Case 1"
                 SelectedTopicText$ = GetGadgetText(NewTopic)
;                 MessageRequester("NewTopicText$ = ",SelectedTopicText$ )
                 SetActiveGadget(NewTopic)
                 LogSelectedTopic()
            EndSelect ; Select EventMenu()
         Case #PB_Event_CloseWindow
              CloseWindow(Window_0) 
              CloseWindow(Window_1) 
         EndSelect     ; Select Event           
     EndIf           ; WindowID = #Window_0         
ForEver          ; OpenWindow_0()
End 
vmars.us Win11 x64 , Martin Guitar 000-16 (1995)
"All things in moderation , except for love and forgiveness."
vmars316
Enthusiast
Enthusiast
Posts: 474
Joined: Fri Jun 29, 2012 12:24 am
Contact:

Re: How to Force window_1 to have Focus ?

Post by vmars316 »

Come on folks , give me a hand here :(
vmars.us Win11 x64 , Martin Guitar 000-16 (1995)
"All things in moderation , except for love and forgiveness."
drgolf
Enthusiast
Enthusiast
Posts: 111
Joined: Tue Mar 03, 2009 3:40 pm
Location: france

Re: How to Force window_1 to have Focus ?

Post by drgolf »

maybe like this (but i dont understand your code) :

Code: Select all

; pimClip-Shrunk.pb
;}
;{ Gadgets
;}
Global Window_0 = 0
Global ClipboardViewerNext.i, showTopicsCkBox, RestartPgm = #False, SaveClipText$
Global clipstring$, d$, getdatecnt, dttime$, showTopicsCkBoxState = #False, LogThis = #False
Global ClipLogName$="****",OpenClipLogName$="00000000", selectedTopic$, Event, WindowID, Result
Global TrayIcon = 1, IconName$ = "myFavs48x48.ico"
Global sendToBtn, findBtn, exitBtn, helpBtn, restartBtn
;
Global Window_1 = 1
Global TopicsListFile$ = "TopicsBtnList.txt", TopicBtnName$, TopicBtnCount.l = 1, LogThisString$
Global TopicContainer, NewTopic, EditListBtn, Btn_1, Btn_2, Btn_3
Global SelectedTopicText$, Window_1_Width.l = 250, Window_1_Height.l = 640, Window_1_Xpos,
       Window_1_Ypos, MaxBtnCount.l = 3
;
Declare OpenWindow_0()
Declare OpenWindow_1()
Declare LogSomething()
Declare GetDate()
Declare CallBack(hWnd, Msg, wParam, lParam)
Declare CloseWindow_window_0()
Declare LogSelectedTopic()
Declare SetupTopicButtons()
;
Procedure OpenWindow_0() 
    OpenWindow(Window_0, 0, 0, 448, 110, "pimClip 2.0", #PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_TitleBar) ; |#PB_Window_ScreenCentered)
    SetWindowColor(Window_0, RGB(192,192,192))
    SetWindowPos_(WindowID(Window_0),#HWND_TOPMOST,0,0,0,0,#SWP_NOMOVE|#SWP_NOSIZE)
    exitBtn = ButtonGadget(#PB_Any, 260, 20, 80, 28, "EXIT", #PB_Button_Default)
    showTopicsCkBox = CheckBoxGadget(#PB_Any, 260, 63, 168, 30, "Prompt for Topics List", #PB_CheckBox_Center | #PB_Checkbox_Unchecked)
EndProcedure ; OpenWindow_0() 
;
Procedure OpenWindow_1()
  Window_1_Xpos = (GetSystemMetrics_(#SM_CXSCREEN) - Window_1_Width) ; horizontal width.
  Window_1_Ypos = (GetSystemMetrics_(#SM_CYSCREEN) - Window_1_Height) ;  vertical height.
  If OpenWindow(Window_1, Window_1_Xpos, 0, Window_1_Width, Window_1_Height, "Enter or Select Topic", #PB_Window_SystemMenu| #PB_Window_TitleBar  | #PB_Window_SizeGadget)
    SetWindowColor(Window_1, RGB(192,192,192))
    SetWindowPos_(WindowID(Window_1),#HWND_TOPMOST,0,0,0,0,#SWP_NOMOVE|#SWP_NOSIZE)
    TopicContainer = ContainerGadget(#PB_Any, 12, 10, 222, 584)
    NewTopic = StringGadget(#PB_Any, 10, 10, 100, 24, " ")
    EditListBtn = ButtonGadget(#PB_Any, 10, 42, 100, 24, "Edit Topic List")
    Btn_1 = ButtonGadget(1, 10, 96, 100, 24, "")
    Btn_2 = ButtonGadget(2, 10, 120, 100, 24, "")
    Btn_3 = ButtonGadget(3, 10, 144, 100, 24, "")
    CloseGadgetList()
  EndIf ; If OpenWindow(Window_1
  HideWindow(Window_1, #True)
EndProcedure
;
Procedure TopicBtn_clicked()
  SelectedTopicText$ = GetGadgetText(EventGadget())
  MessageRequester("BtnGadgetText$","BtnGadgetText$  " + SelectedTopicText$)
  LogThis = #True
  LogSelectedTopic()
  LogThis = #False
  HideWindow(Window_1, #False)
EndProcedure ; TopicBtn_clicked()
;
Procedure GetDate()
GetDateCnt+1
   d$=FormatDate("%yyyy-%mm-%dd",Date())
   dttime$=FormatDate("%hh:%ii:%ss",Date())
; SetWindowTitle(Window_0, "pimLog 1.0 "+d$+".txt")
EndProcedure  ; GetDate()
;
Procedure LogSelectedTopic()
GetDate()
If d$ <> OpenClipLogName$ ; file could have been closed but Exists
     ClipLogName$ = (d$ + ".txt")
     OpenClipLogName$ = ClipLogName$
     Debug "IF d$ <> OpenClipLogName$"
EndIf  ; IF d$ <> OpenClipLogName$
;
 If OpenFile(0,cliplogname$,#PB_File_Append |#PB_Ascii)
        WriteStringN(0,(dttime$ + " " + SelectedTopicText$ +  ": " + SaveClipText$), #PB_Ascii) ; no showTopicsCkBox checked
 ;       MessageRequester("WriteStringN(0,","Logging = " + dttime$ + " " + SelectedTopicText$ +  " " + SaveClipText$)
        LogThis = #False
        HideWindow(Window_1, #True)
        Debug "OpenFile(0,cliplogname$ & WriteStringN(0,(dttime$"
        CloseFile(0)
 EndIf
EndProcedure ; LogSelectedTopic()
;
Procedure LogSomething()
GetDate()
If d$ <> OpenClipLogName$ ; file could have been closed but Exists
     ClipLogName$ = (d$ + ".txt")
     OpenClipLogName$ = ClipLogName$
  Debug "Procedure LogSomething() & GetDate() & If d$ <> OpenClipLogName$ "
EndIf  ; IF d$ <> OpenClipLogName$
;
 If OpenFile(0,cliplogname$,#PB_File_Append |#PB_Ascii)
     LogThisString$ = (dttime$ + " " + "NoTopic: " + clipstring$)
     SaveClipText$ = clipstring$ ;   Wait til SelectTopic Window_1 clicks then use SaveClipText$
     If showTopicsCkBoxState
       Debug "If OpenFile(0,cliplogname$, & If showTopicsCkBoxState"
        SetActiveWindow(Window_1)
        HideWindow(Window_1, #False) ; show Window_1
     ;  SetActiveWindow(Window_1)  ;  focus has been put on this window
;       SetWindowPos_(WindowID(Windomw_1),#HWND_TOPMOST,0,0,0,0,#SWP_NOMOVE|#SWP_NOSIZE) ; Make Window_1 OnTop
     Else
       WriteStringN(0,(LogThisString$), #PB_Ascii) ; no showTopicsCkBox checked
       Debug "If OpenFile(0,cliplogname$, & WriteStringN(0,(LogThisString$)"
     EndIf ; If showTopicsCkBoxState
   CloseFile(0)
EndIf ;If OpenFile(0,cliplogname$
EndProcedure ; LogSomething() stickywindow
;
Procedure CallBack(hWnd, Msg, wParam, lParam)
  Protected result
  Result = #PB_ProcessPureBasicEvents
  Select Msg
    Case #WM_DRAWCLIPBOARD
      If IsClipboardFormatAvailable_(#CF_TEXT)
        Debug "Procedure CallBack( & If IsClipboardFormatAvailable_(#CF_TEXT)"
        clipstring$=GetClipboardText()
        LogSomething() ; ============================================== LogSomething()
        clipString$=""
      EndIf
      SendMessage_(ClipboardViewerNext, Msg, wParam, lParam)
    Case #WM_CHANGECBCHAIN
      If wParam = ClipboardViewerNext
        ClipboardViewerNext = lParam
      Else
        SendMessage_(ClipboardViewerNext, Msg, wParam, lParam)
      EndIf
  EndSelect
  ProcedureReturn Result
EndProcedure ; CallBack(hWnd, Msg, wParam, lParam)

Procedure CloseWindow_window_0()
  RemoveSysTrayIcon(TrayIcon)
  ChangeClipboardChain_(WindowID(Window_0), ClipboardViewerNext)
  If RestartPgm
  RunProgram("pimClip.exe")
  EndIf
    CloseWindow(Window_0)
EndProcedure ; CloseWindow_window_0()
;             
Procedure SetupTopicButtons() ; Run once at beginning ==========
  If ReadFile(0, TopicsListFile$)
    TopicBtnCount = 1
    Repeat
      TopicBtnName$ = ReadString(0)
      SetGadgetText(TopicBtnCount,TopicBtnName$)
      TopicBtnCount = TopicBtnCount + 1
  Until Eof(0) | TopicBtnCount > MaxBtnCount

  CloseFile(0)
EndIf
EndProcedure ; SetupTopicButtons() ============================== 
;-begin       
OpenWindow_0()
  If IsClipboardFormatAvailable_(#CF_TEXT)
     ClearClipboard()
     clipString$=""
  EndIf
SetWindowCallback(@callback())
ClipboardViewerNext = SetClipboardViewer_(WindowID(Window_0))
  AddSysTrayIcon(TrayIcon, WindowID(window_0), LoadImage(0, IconName$))
  SysTrayIconToolTip(TrayIcon, "LeftDouble_Click to Open pimClip WIndow")
; 
OpenWindow_1()
SetActiveGadget(NewTopic)
AddKeyboardShortcut(Window_1, #PB_Shortcut_Return, 1) 
SetupTopicButtons()
; =====================================
;{- Event loop
Repeat
    Event = WaitWindowEvent()
    WindowID = EventWindow()
    ; ///////////////////
    If WindowID = Window_0
;       Debug "Window_0 Event" + "#PB_Event_Gadget = " + Str(#PB_Event_Gadget)
       Select Event
         Case #PB_Event_Gadget
           Select EventGadget()
             Case exitBtn
               CloseWindow_window_0()
               Break
             Case showTopicsCkBox
               Result = GetGadgetState(showTopicsCkBox)
               showTopicsCkBoxState = Result
               MessageRequester( "Case showTopicsCkBox" , "Case showTopicsCkBox = "  + Str(Result) )
               Debug "Case showTopicsCkBox"  + Str(Result) 
           EndSelect ; EventGadget()
; ////////////////////////
         Case #PB_Event_SysTray
           If EventType() = #PB_EventType_LeftDoubleClick
              HideWindow(Window_0, #False)
           EndIf
         Case #PB_Event_CloseWindow
              CloseWindow(Window_0)
              CloseWindow(Window_1)
              Break
       EndSelect ; Window_0 Event
     EndIf       ; If WindowID = Window_0   
     
     If WindowID = Window_1
       Select Event
         Case #PB_Event_Gadget
           Select EventGadget()
             Case 1 To MaxBtnCount
               Debug "Case 1 To MaxBtnCount"
               TopicBtn_clicked()
             Case NewTopic
               Debug "Case NewTopic"
               Select EventType()
                 Case #PB_EventType_Focus
                   SetActiveWindow(Window_1)
                   AddKeyboardShortcut(Window_1, #PB_Shortcut_Return, 1)
                   Debug "Case #PB_EventType_Focus"
                 Case #PB_EventType_LostFocus
                   RemoveKeyboardShortcut(Window_1, #PB_Shortcut_Return)
                   Debug "Case #PB_EventType_LostFocus"
               EndSelect ; Select EventType()
             Case EditListBtn
                    RunProgram("TopicsBtnList.txt")
           EndSelect ; Select EventGadget()
       
         Case #PB_Event_Menu
             Debug "Case #PB_Event_Menu"
;                 MessageRequester("Select EventMenu()","Select EventMenu()= "+Str(EventMenu()))
             Select EventMenu()
               Case 1
                 Debug "Select EventMenu() & Case 1"
                 SelectedTopicText$ = GetGadgetText(NewTopic)
;                 MessageRequester("NewTopicText$ = ",SelectedTopicText$ )
                 SetActiveGadget(NewTopic)
                 LogSelectedTopic()
            EndSelect ; Select EventMenu()
         Case #PB_Event_CloseWindow
            CloseWindow(Window_1)
           CloseWindow(Window_0)
             
         EndSelect     ; Select Event           
     EndIf           ; WindowID = #Window_0         
ForEver          ; OpenWindow_0()
End 

vmars316
Enthusiast
Enthusiast
Posts: 474
Joined: Fri Jun 29, 2012 12:24 am
Contact:

Re: How to Force window_1 to have Focus ?

Post by vmars316 »

Thanks drgolf ;
Unfortunately , same results , 'no focus for Window_1' .

Below is code followed by DebugTraceLog.txt .
I put sequence #s for all debug lines , easier to follow , I hope .

drgolf code with, numbered debug statements .

Code: Select all

;Ported from 'hbClipLog.bas'To 'pimClip.pb' PureBasic With Help from by DrGolf - 07/2016
;Original program 'hbClipLog.bas'  Author: vmars ,  vmars316    08/10/2012
;
;EnableExplicit
;{- Enumerations / DataSections
;{ Windows
Enumeration
  
EndEnumeration
;}
;{ Gadgets
;}
Global Window_0 = 0
Global ClipboardViewerNext.i, showTopicsCkBox, RestartPgm = #False, SaveClipText$
Global clipstring$, d$, getdatecnt, dttime$, showTopicsCkBoxState = #False, LogThis = #False
Global ClipLogName$="****",OpenClipLogName$="00000000", selectedTopic$, Event, WindowID, Result
Global TrayIcon = 1, IconName$ = "myFavs48x48.ico"
Global sendToBtn, findBtn, exitBtn, helpBtn, restartBtn, DebugCount.l = 0
;
Global Window_1 = 1
Global TopicsListFile$ = "TopicsBtnList.txt", TopicBtnName$, TopicBtnCount.l = 1, LogThisString$
Global TopicContainer, NewTopic, EditListBtn, Btn_1, Btn_2, Btn_3 
Global SelectedTopicText$, Window_1_Width.l = 250, Window_1_Height.l = 640, Window_1_Xpos,
       Window_1_Ypos, MaxBtnCount.l = 3 
;
Declare OpenWindow_0()
Declare OpenWindow_1()
Declare LogSomething()
Declare GetDate()
Declare CallBack(hWnd, Msg, wParam, lParam)
Declare CloseWindow_window_0()
Declare LogSelectedTopic()
Declare SetupTopicButtons()
;
Procedure OpenWindow_0()  
    OpenWindow(Window_0, 0, 0, 448, 110, "pimClip 2.0", #PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_TitleBar) ; |#PB_Window_ScreenCentered)
    SetWindowColor(Window_0, RGB(192,192,192))
    SetWindowPos_(WindowID(Window_0),#HWND_TOPMOST,0,0,0,0,#SWP_NOMOVE|#SWP_NOSIZE)
    exitBtn = ButtonGadget(#PB_Any, 260, 20, 80, 28, "EXIT", #PB_Button_Default)
    showTopicsCkBox = CheckBoxGadget(#PB_Any, 260, 63, 168, 30, "Prompt for Topics List", #PB_CheckBox_Center | #PB_Checkbox_Unchecked)
EndProcedure ; OpenWindow_0()  
;
Procedure OpenWindow_1()
  Window_1_Xpos = (GetSystemMetrics_(#SM_CXSCREEN) - Window_1_Width) ; horizontal width.
  Window_1_Ypos = (GetSystemMetrics_(#SM_CYSCREEN) - Window_1_Height) ;  vertical height.
  If OpenWindow(Window_1, Window_1_Xpos, 0, Window_1_Width, Window_1_Height, "Enter or Select Topic", #PB_Window_SystemMenu| #PB_Window_TitleBar  | #PB_Window_SizeGadget)
    SetWindowColor(Window_1, RGB(192,192,192))
    SetWindowPos_(WindowID(Window_1),#HWND_TOPMOST,0,0,0,0,#SWP_NOMOVE|#SWP_NOSIZE) 
    TopicContainer = ContainerGadget(#PB_Any, 12, 10, 222, 584)
    NewTopic = StringGadget(#PB_Any, 10, 10, 100, 24, " ")
    EditListBtn = ButtonGadget(#PB_Any, 10, 42, 100, 24, "Edit Topic List")
    Btn_1 = ButtonGadget(1, 10, 96, 100, 24, "")
    Btn_2 = ButtonGadget(2, 10, 120, 100, 24, "")
    Btn_3 = ButtonGadget(3, 10, 144, 100, 24, "")
    CloseGadgetList()
  EndIf ; If OpenWindow(Window_1
  HideWindow(Window_1, #True)
EndProcedure
;
Procedure TopicBtn_clicked()
  SelectedTopicText$ = GetGadgetText(EventGadget())
  MessageRequester("BtnGadgetText$","BtnGadgetText$  " + SelectedTopicText$)
  LogThis = #True 
  LogSelectedTopic()
  LogThis = #False
  HideWindow(Window_1, #True)
EndProcedure ; TopicBtn_clicked()
;
Procedure GetDate()
GetDateCnt+1
   d$=FormatDate("%yyyy-%mm-%dd",Date())
   dttime$=FormatDate("%hh:%ii:%ss",Date())
; SetWindowTitle(Window_0, "pimLog 1.0 "+d$+".txt") 
EndProcedure  ; GetDate()
;
Procedure LogSelectedTopic()
GetDate()
If d$ <> OpenClipLogName$ ; file could have been closed but Exists
     ClipLogName$ = (d$ + ".txt")
     OpenClipLogName$ = ClipLogName$
     Debug "81: IF d$ <> OpenClipLogName$"
EndIf  ; IF d$ <> OpenClipLogName$
;
If OpenFile(0,cliplogname$,#PB_File_Append |#PB_Ascii)
       WriteStringN(0,(dttime$ + " " + SelectedTopicText$ +  ": " + SaveClipText$), #PB_Ascii) ; no showTopicsCkBox checked 
;       MessageRequester("WriteStringN(0,","Logging = " + dttime$ + " " + SelectedTopicText$ +  " " + SaveClipText$)
       LogThis = #False 
       HideWindow(Window_1, #True)
       Debug "89: OpenFile(0,cliplogname$ & WriteStringN(0,(dttime$"
       CloseFile(0)
EndIf
EndProcedure ; LogSelectedTopic()
;
Procedure LogSomething()
GetDate()
If d$ <> OpenClipLogName$ ; file could have been closed but Exists
     ClipLogName$ = (d$ + ".txt")
     OpenClipLogName$ = ClipLogName$
  Debug "99: Procedure LogSomething() & GetDate() & If d$ <> OpenClipLogName$ "
EndIf  ; IF d$ <> OpenClipLogName$
;
If OpenFile(0,cliplogname$,#PB_File_Append |#PB_Ascii)
    LogThisString$ = (dttime$ + " " + "NoTopic: " + clipstring$)
    SaveClipText$ = clipstring$ ;   Wait til SelectTopic Window_1 clicks then use SaveClipText$
    If showTopicsCkBoxState
      Debug "106: If OpenFile(0,cliplogname$, & If showTopicsCkBoxState"
      HideWindow(Window_1, #False) ; show Window_1 
      SetActiveWindow(Window_1)  ;  focus has been put on this window
      SetWindowPos_(WindowID(Windomw_1),#HWND_TOPMOST,0,0,0,0,#SWP_NOMOVE|#SWP_NOSIZE) ; Make Window_1 OnTop 
    Else 
      WriteStringN(0,(LogThisString$), #PB_Ascii) ; no showTopicsCkBox checked
      Debug "112: If OpenFile(0,cliplogname$, & WriteStringN(0,(LogThisString$)"
    EndIf ; If showTopicsCkBoxState
  CloseFile(0)
EndIf ;If OpenFile(0,cliplogname$
EndProcedure ; LogSomething() stickywindow
;
Procedure CallBack(hWnd, Msg, wParam, lParam)
  Protected result
  Result = #PB_ProcessPureBasicEvents
  Select Msg
    Case #WM_DRAWCLIPBOARD
      If IsClipboardFormatAvailable_(#CF_TEXT)
        Debug "124: Procedure CallBack( & If IsClipboardFormatAvailable_(#CF_TEXT)"
        clipstring$=GetClipboardText()
        LogSomething() ; ============================================== LogSomething() 
        clipString$=""
      EndIf
      SendMessage_(ClipboardViewerNext, Msg, wParam, lParam)
    Case #WM_CHANGECBCHAIN
      If wParam = ClipboardViewerNext
        ClipboardViewerNext = lParam
      Else
        SendMessage_(ClipboardViewerNext, Msg, wParam, lParam)
      EndIf
  EndSelect
  ProcedureReturn Result
EndProcedure ; CallBack(hWnd, Msg, wParam, lParam)

Procedure CloseWindow_window_0()
  RemoveSysTrayIcon(TrayIcon)
  ChangeClipboardChain_(WindowID(Window_0), ClipboardViewerNext)
  If RestartPgm 
    RunProgram("pimClip.exe") 
  EndIf 
    CloseWindow(Window_0) 
EndProcedure ; CloseWindow_window_0()
;             
Procedure SetupTopicButtons() ; Run once at beginning ==========
  If ReadFile(0, TopicsListFile$) 
    TopicBtnCount = 1
    Repeat 
      TopicBtnName$ = ReadString(0) 
      SetGadgetText(TopicBtnCount,TopicBtnName$)
      TopicBtnCount = TopicBtnCount + 1 
  Until Eof(0) | TopicBtnCount > MaxBtnCount 

  CloseFile(0) 
EndIf
EndProcedure ; SetupTopicButtons() ==============================  
;-begin       
OpenWindow_0()
  If IsClipboardFormatAvailable_(#CF_TEXT) 
     ClearClipboard()
     clipString$=""
  EndIf
SetWindowCallback(@callback())
ClipboardViewerNext = SetClipboardViewer_(WindowID(Window_0))
  AddSysTrayIcon(TrayIcon, WindowID(0), LoadImage(0, IconName$))
  SysTrayIconToolTip(TrayIcon, "LeftDouble_Click to Open pimClip WIndow")
;  
OpenWindow_1()
SetActiveGadget(NewTopic)
AddKeyboardShortcut(Window_1, #PB_Shortcut_Return, 1)  
SetupTopicButtons() 
; =====================================
;{- Event loop
Repeat
    Event = WaitWindowEvent()
    WindowID = EventWindow()
    ; ///////////////////
    If WindowID = Window_0 
;       Debug "Window_0 Event" + "#PB_Event_Gadget = " + Str(#PB_Event_Gadget)
       Select Event
         Case #PB_Event_Gadget
           Select EventGadget()
             Case exitBtn
               CloseWindow_window_0()
               Break
             Case showTopicsCkBox
               Result = GetGadgetState(showTopicsCkBox)
               showTopicsCkBoxState = Result
;               MessageRequester( "Case showTopicsCkBox" , "Case showTopicsCkBox = "  + Str(Result) ) 
               Debug "194: Case showTopicsCkBox"  + Str(Result)  
           EndSelect ; EventGadget()
; ////////////////////////
         Case #PB_Event_SysTray
           If EventType() = #PB_EventType_LeftDoubleClick
              HideWindow(Window_0, #False)
           EndIf
         Case #PB_Event_CloseWindow
              CloseWindow(Window_0) 
              CloseWindow(Window_1) 
       EndSelect ; Window_0 Event 
     EndIf       ; If WindowID = Window_0   
     
     If WindowID = Window_1 
       Select Event
         Case #PB_Event_Gadget
           Select EventGadget()
             Case 1 To MaxBtnCount
               Debug "212: Case 1 To MaxBtnCount"
               TopicBtn_clicked()
             Case NewTopic
               Debug "215: Case NewTopic"
               Select EventType()
                 Case #PB_EventType_Focus
                   SetActiveWindow(Window_1)
                   AddKeyboardShortcut(Window_1, #PB_Shortcut_Return, 1)
                   Debug "220: Case #PB_EventType_Focus" 
                 Case #PB_EventType_LostFocus
                   RemoveKeyboardShortcut(Window_1, #PB_Shortcut_Return)
                   Debug "223: Case #PB_EventType_LostFocus"
               EndSelect ; Select EventType()
             Case EditListBtn
                    RunProgram("TopicsBtnList.txt")
           EndSelect ; Select EventGadget()
        
         Case #PB_Event_Menu
             Debug "230: Case #PB_Event_Menu"
;                 MessageRequester("Select EventMenu()","Select EventMenu()= "+Str(EventMenu()))
             Select EventMenu()
               Case 1
                 Debug "234: Select EventMenu() & Case 1"
                 SelectedTopicText$ = GetGadgetText(NewTopic)
;                 MessageRequester("NewTopicText$ = ",SelectedTopicText$ )
                 SetActiveGadget(NewTopic)
                 LogSelectedTopic()
            EndSelect ; Select EventMenu()
         Case #PB_Event_CloseWindow
              CloseWindow(Window_0) 
              CloseWindow(Window_1) 
         EndSelect     ; Select Event           
     EndIf           ; WindowID = #Window_0         
ForEver          ; OpenWindow_0()
End 
DebugTrace.txt :

Code: Select all

215: Case NewTopic
220: Case #PB_EventType_Focus
215: Case NewTopic

Now click chBox :

194: Case showTopicsCkBox1

Now copy some txt from Notepad :


124: Procedure CallBack( & If IsClipboardFormatAvailable_(#CF_TEXT)

99: Procedure LogSomething() & GetDate() & If d$ <> OpenClipLogName$ 
106: If OpenFile(0,cliplogname$, & If showTopicsCkBoxState
215: Case NewTopic
220: Case #PB_EventType_Focus
215: Case NewTopic
215: Case NewTopic
223: Case #PB_EventType_LostFocus
215: Case NewTopic
215: Case NewTopic
220: Case #PB_EventType_Focus
215: Case NewTopic 

Window_1 is showing 'onTop' , but doesn't have focus . 

Now I click on Window_1 to get focus :
No line added to debug (which surprises me) .
vmars.us Win11 x64 , Martin Guitar 000-16 (1995)
"All things in moderation , except for love and forgiveness."
User avatar
TI-994A
Addict
Addict
Posts: 2740
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: How to Force window_1 to have Focus ?

Post by TI-994A »

vmars316 wrote:...Unfortunately , same results , 'no focus for Window_1' ...
Try the SetForegroundWindow() API function:

Code: Select all

If OpenFile(0, cliplogname$...
  ...
  ...
  If showTopicsCkBoxState
    ...
    ;SetActiveWindow(...
    ;SetWindowPos_(...
    
    HideWindow(Window_1, #False)
    SetForegroundWindow_(WindowID(Window_0))
    
  Else 
    ...
    ...
Hope it helps. :wink:
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
drgolf
Enthusiast
Enthusiast
Posts: 111
Joined: Tue Mar 03, 2009 3:40 pm
Location: france

Re: How to Force window_1 to have Focus ?

Post by drgolf »

this :

SetActiveWindow(Window_1)
HideWindow(Window_1, #False) ; show Window_1

work form me and window_1 has the focus.

Setactivewindow before hidewindow.
User avatar
JHPJHP
Addict
Addict
Posts: 2257
Joined: Sat Oct 09, 2010 3:47 am

Re: How to Force window_1 to have Focus ?

Post by JHPJHP »

Try the following:

Code: Select all

Procedure SetWindowFocus(wID)
  If GetWindowState(wID) <> #PB_Window_Minimize And IsWindowVisible_(WindowID(wID))
    ForeThread = GetWindowThreadProcessId_(GetForegroundWindow_(), #Null)
    AppThread = GetCurrentThreadId_()

    If ForeThread <> AppThread
      AttachThreadInput_(ForeThread, AppThread, #True)
      BringWindowToTop_(WindowID(wID))
      ShowWindow_(WindowID(wID), #SW_SHOW)
      AttachThreadInput_(ForeThread, AppThread, #False)
    Else
      BringWindowToTop_(WindowID(wID))
      ShowWindow_(WindowID(wID), #SW_SHOW)
    EndIf
  EndIf
EndProcedure

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
vmars316
Enthusiast
Enthusiast
Posts: 474
Joined: Fri Jun 29, 2012 12:24 am
Contact:

Re: How to Force window_1 to have Focus ?

Post by vmars316 »

Thanks All ,
Window_1 is defined as : Global Window_1 = 1

Sorry to say , for TI-994A and drgolf suggestions , I get same nogo , and no errors .

For JHPJHP :
With changes below , I get an :
[ERROR] The specified #Window is not initialised.
for line:
If GetWindowState(wID) <> #PB_Window_Minimize And IsWindowVisible_(WindowID(wID))


changes in code:

Code: Select all

;
Declare SetupTopicButtons() : Declare SetWindowFocus(wID) 

If OpenFile(0,cliplogname$,#PB_File_Append |#PB_Ascii)
    LogThisString$ = (dttime$ + " " + "NoTopic: " + clipstring$)
    SaveClipText$ = clipstring$ ;   Wait til SelectTopic Window_1 clicks then use SaveClipText$
    If showTopicsCkBoxState
      Debug "106: If OpenFile(0,cliplogname$, & If showTopicsCkBoxState"
      ShowWindowId = WindowID(Window_1)
      SetWindowFocus(WindowID(Window_1))
;      HideWindow(Window_1, #False) ; show Window_1 : SetActiveWindow(Window_1)  ;  focus has been put on this window
    Else 
      WriteStringN(0,(LogThisString$), #PB_Ascii) ; no showTopicsCkBox checked
      Debug "112: If OpenFile(0,cliplogname$, & WriteStringN(0,(LogThisString$)"
    EndIf ; If showTopicsCkBoxState
  CloseFile(0)
EndIf ;If OpenFile(0,cliplogname$
EndProcedure ; LogSomething() stickywindow
;

Code: Select all

;
Procedure SetWindowFocus(wID)
  If GetWindowState(wID) <> #PB_Window_Minimize And IsWindowVisible_(WindowID(wID))
    ForeThread = GetWindowThreadProcessId_(GetForegroundWindow_(), #Null)
    AppThread = GetCurrentThreadId_()

    If ForeThread <> AppThread
      AttachThreadInput_(ForeThread, AppThread, #True)
      BringWindowToTop_(WindowID(wID))
      ShowWindow_(WindowID(wID), #SW_SHOW)
      AttachThreadInput_(ForeThread, AppThread, #False)
    Else
      BringWindowToTop_(WindowID(wID))
      ShowWindow_(WindowID(wID), #SW_SHOW)
    EndIf
  EndIf
EndProcedure ; Procedure SetWindowFocus(wID)
;   
vmars.us Win11 x64 , Martin Guitar 000-16 (1995)
"All things in moderation , except for love and forgiveness."
User avatar
JHPJHP
Addict
Addict
Posts: 2257
Joined: Sat Oct 09, 2010 3:47 am

Re: How to Force window_1 to have Focus ?

Post by JHPJHP »

Hi vmars316,

Change:

Code: Select all

SetWindowFocus(WindowID(Window_1))
To:

Code: Select all

SetWindowFocus(Window_1)

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
User avatar
TI-994A
Addict
Addict
Posts: 2740
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: How to Force window_1 to have Focus ?

Post by TI-994A »

vmars316 wrote:...I get same nogo , and no errors .
Give this a try, then:

Code: Select all

If OpenFile(0, cliplogname$...
  ...
  ...
  If showTopicsCkBoxState
    ...
    ;SetWindowPos_(...
    
    HideWindow(Window_1, #False)
    SetActiveWindow(Window_1)
    SetForegroundWindow_(WindowID(Window_0))
    
  Else 
    ...
    ...
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4953
Joined: Sun Apr 12, 2009 6:27 am

Re: How to Force window_1 to have Focus ?

Post by RASHAD »

Hi
Window_1 is activated as per your request
Your code has some bugs dealing with 2 Windows you have to fix them
I used the code in your first post

Code: Select all

; pimClip-Shrunk.pb
;}
;{ Gadgets
;}
Global Window_0 = 0
Global ClipboardViewerNext.i, showTopicsCkBox, RestartPgm = #False, SaveClipText$
Global clipstring$, d$, getdatecnt, dttime$, showTopicsCkBoxState = #False, LogThis = #False
Global ClipLogName$="****",OpenClipLogName$="00000000", selectedTopic$, Event, WindowID, Result
Global TrayIcon = 1, IconName$ = "d:\Analyse.ico"
Global sendToBtn, findBtn, exitBtn, helpBtn, restartBtn
;
Global Window_1 = 1
Global TopicsListFile$ = "TopicsBtnList.txt", TopicBtnName$, TopicBtnCount.l = 1, LogThisString$
Global TopicContainer, NewTopic, EditListBtn, Btn_1, Btn_2, Btn_3
Global SelectedTopicText$, Window_1_Width.l = 250, Window_1_Height.l = 640, Window_1_Xpos,
       Window_1_Ypos, MaxBtnCount.l = 3
;
Declare OpenWindow_0()
Declare OpenWindow_1()
Declare LogSomething()
Declare GetDate()
Declare CallBack(hWnd, Msg, wParam, lParam)
Declare CloseWindow_window_0()
Declare LogSelectedTopic()
Declare SetupTopicButtons()
;
Procedure OpenWindow_0()
    OpenWindow(Window_0, 0, 0, 448, 110, "pimClip 2.0", #PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_TitleBar) ; |#PB_Window_ScreenCentered)
    SetWindowColor(Window_0, RGB(192,192,192))
    SetWindowPos_(WindowID(Window_0),#HWND_TOPMOST,0,0,0,0,#SWP_NOMOVE|#SWP_NOSIZE)
    exitBtn = ButtonGadget(#PB_Any, 260, 20, 80, 28, "EXIT", #PB_Button_Default)
    showTopicsCkBox = CheckBoxGadget(#PB_Any, 260, 63, 168, 30, "Prompt for Topics List", #PB_CheckBox_Center | #PB_Checkbox_Unchecked)
EndProcedure ; OpenWindow_0()
;
Procedure OpenWindow_1()
  Window_1_Xpos = (GetSystemMetrics_(#SM_CXSCREEN) - Window_1_Width) ; horizontal width.
  Window_1_Ypos = (GetSystemMetrics_(#SM_CYSCREEN) - Window_1_Height) ;  vertical height.
  If OpenWindow(Window_1, Window_1_Xpos, 0, Window_1_Width, Window_1_Height, "Enter or Select Topic",#PB_Window_Invisible | #PB_Window_SystemMenu| #PB_Window_TitleBar  | #PB_Window_SizeGadget)
    SetWindowColor(Window_1, RGB(192,192,192))
    TopicContainer = ContainerGadget(#PB_Any, 12, 10, 222, 584)
    NewTopic = StringGadget(#PB_Any, 10, 10, 100, 24, " ")
    EditListBtn = ButtonGadget(#PB_Any, 10, 42, 100, 24, "Edit Topic List")
    Btn_1 = ButtonGadget(1, 10, 96, 100, 24, "")
    Btn_2 = ButtonGadget(2, 10, 120, 100, 24, "")
    Btn_3 = ButtonGadget(3, 10, 144, 100, 24, "")
    CloseGadgetList()
  EndIf ; If OpenWindow(Window_1
  ;HideWindow(Window_1, #True)
EndProcedure
;
Procedure TopicBtn_clicked()
  SelectedTopicText$ = GetGadgetText(EventGadget())
  MessageRequester("BtnGadgetText$","BtnGadgetText$  " + SelectedTopicText$)
  LogThis = #True
  LogSelectedTopic()
  LogThis = #False
  HideWindow(Window_1, #True)
EndProcedure ; TopicBtn_clicked()
;
Procedure GetDate()
GetDateCnt+1
   d$=FormatDate("%yyyy-%mm-%dd",Date())
   dttime$=FormatDate("%hh:%ii:%ss",Date())
; SetWindowTitle(Window_0, "pimLog 1.0 "+d$+".txt")
EndProcedure  ; GetDate()
;
Procedure LogSelectedTopic()
GetDate()
If d$ <> OpenClipLogName$ ; file could have been closed but Exists
     ClipLogName$ = (d$ + ".txt")
     OpenClipLogName$ = ClipLogName$
     Debug "IF d$ <> OpenClipLogName$"
EndIf  ; IF d$ <> OpenClipLogName$
;
If OpenFile(0,cliplogname$,#PB_File_Append |#PB_Ascii)
       WriteStringN(0,(dttime$ + " " + SelectedTopicText$ +  ": " + SaveClipText$), #PB_Ascii) ; no showTopicsCkBox checked
;       MessageRequester("WriteStringN(0,","Logging = " + dttime$ + " " + SelectedTopicText$ +  " " + SaveClipText$)
       LogThis = #False
       HideWindow(Window_1, #True)
       Debug "OpenFile(0,cliplogname$ & WriteStringN(0,(dttime$"
       CloseFile(0)
EndIf
EndProcedure ; LogSelectedTopic()
;
Procedure LogSomething()
GetDate()
If d$ <> OpenClipLogName$ ; file could have been closed but Exists
     ClipLogName$ = (d$ + ".txt")
     OpenClipLogName$ = ClipLogName$
  Debug "Procedure LogSomething() & GetDate() & If d$ <> OpenClipLogName$ "
EndIf  ; IF d$ <> OpenClipLogName$
;
If OpenFile(0,cliplogname$,#PB_File_Append |#PB_Ascii)
    LogThisString$ = (dttime$ + " " + "NoTopic: " + clipstring$)
    SaveClipText$ = clipstring$ ;   Wait til SelectTopic Window_1 clicks then use SaveClipText$
    If showTopicsCkBoxState
      Debug "If OpenFile(0,cliplogname$, & If showTopicsCkBoxState"
      HideWindow(Window_1, #False) ; show Window_1
      ;SetActiveWindow(Window_1)  ;  focus has been put on this window
      ;SetWindowPos_(WindowID(Windomw_1),#HWND_TOP,0,0,0,0,#SWP_NOMOVE|#SWP_NOSIZE)
      SendMessage_(#HWND_BROADCAST, #WM_SYSCOMMAND, #SC_HOTKEY, WindowID(Window_1))
      ;SetActiveWindow(Window_1)
    Else
      WriteStringN(0,(LogThisString$), #PB_Ascii) ; no showTopicsCkBox checked
      Debug "If OpenFile(0,cliplogname$, & WriteStringN(0,(LogThisString$)"
    EndIf ; If showTopicsCkBoxState
  CloseFile(0)
EndIf ;If OpenFile(0,cliplogname$
EndProcedure ; LogSomething() stickywindow
;
Procedure CallBack(hWnd, Msg, wParam, lParam)
  Protected result
  Result = #PB_ProcessPureBasicEvents
  Select Msg   
               
    Case #WM_DRAWCLIPBOARD
      If IsClipboardFormatAvailable_(#CF_TEXT)
        Debug "Procedure CallBack( & If IsClipboardFormatAvailable_(#CF_TEXT)"
        clipstring$=GetClipboardText()
        LogSomething() ; ============================================== LogSomething()
        clipString$=""
      EndIf
      SendMessage_(ClipboardViewerNext, Msg, wParam, lParam)
    Case #WM_CHANGECBCHAIN
      If wParam = ClipboardViewerNext
        ClipboardViewerNext = lParam
      Else
        SendMessage_(ClipboardViewerNext, Msg, wParam, lParam)
      EndIf
  EndSelect
  ProcedureReturn Result
EndProcedure ; CallBack(hWnd, Msg, wParam, lParam)

Procedure CloseWindow_window_0()
  RemoveSysTrayIcon(TrayIcon)
  ChangeClipboardChain_(WindowID(Window_0), ClipboardViewerNext)
  If RestartPgm
    RunProgram("pimClip.exe")
  EndIf
    CloseWindow(Window_0)
EndProcedure ; CloseWindow_window_0()
;             
Procedure SetupTopicButtons() ; Run once at beginning ==========
  If ReadFile(0, TopicsListFile$)
    TopicBtnCount = 1
    Repeat
      TopicBtnName$ = ReadString(0)
      SetGadgetText(TopicBtnCount,TopicBtnName$)
      TopicBtnCount = TopicBtnCount + 1
  Until Eof(0) | TopicBtnCount > MaxBtnCount

  CloseFile(0)
EndIf
EndProcedure ; SetupTopicButtons() ==============================
;-begin       
OpenWindow_0()
  If IsClipboardFormatAvailable_(#CF_TEXT)
     ClearClipboard()
     clipString$=""
  EndIf
SetWindowCallback(@callback())
ClipboardViewerNext = SetClipboardViewer_(WindowID(Window_0))
  AddSysTrayIcon(TrayIcon, WindowID(0), LoadImage(0, IconName$))
  SysTrayIconToolTip(TrayIcon, "LeftDouble_Click to Open pimClip WIndow")
;
OpenWindow_1()
SetActiveGadget(NewTopic)
AddKeyboardShortcut(Window_1, #PB_Shortcut_Return, 1)
SetupTopicButtons()
; =====================================
;{- Event loop
Repeat   
    Event = WaitWindowEvent()
    WindowID = EventWindow()
    ; ///////////////////
    If WindowID = Window_0
;       Debug "Window_0 Event" + "#PB_Event_Gadget = " + Str(#PB_Event_Gadget)
       Select Event
         Case #PB_Event_Gadget
           Select EventGadget()
             Case exitBtn
               CloseWindow_window_0()
               Break
             Case showTopicsCkBox
               Result = GetGadgetState(showTopicsCkBox)
               showTopicsCkBoxState = Result
               MessageRequester( "Case showTopicsCkBox" , "Case showTopicsCkBox = "  + Str(Result) )
               Debug "Case showTopicsCkBox"  + Str(Result)
           EndSelect ; EventGadget()
; ////////////////////////
         Case #PB_Event_SysTray
           If EventType() = #PB_EventType_LeftDoubleClick
              HideWindow(Window_1, #False)
              SendMessage_(#HWND_BROADCAST, #WM_SYSCOMMAND, #SC_HOTKEY, WindowID(Window_1))
           EndIf
         Case #PB_Event_CloseWindow
              CloseWindow(Window_0)
              CloseWindow(Window_1)
       EndSelect ; Window_0 Event
     EndIf       ; If WindowID = Window_0   
     
     If WindowID = Window_1
       Select Event
         Case #PB_Event_Gadget
           Select EventGadget()
             Case 1 To MaxBtnCount
               Debug "Case 1 To MaxBtnCount"
               TopicBtn_clicked()
             Case NewTopic
               Debug "Case NewTopic"
               Select EventType()
                 Case #PB_EventType_Focus
                   SetActiveWindow(Window_1)
                   AddKeyboardShortcut(Window_1, #PB_Shortcut_Return, 1)
                   Debug "Case #PB_EventType_Focus"
                 Case #PB_EventType_LostFocus
                   RemoveKeyboardShortcut(Window_1, #PB_Shortcut_Return)
                   Debug "Case #PB_EventType_LostFocus"
               EndSelect ; Select EventType()
             Case EditListBtn
                    RunProgram("TopicsBtnList.txt")
           EndSelect ; Select EventGadget()
       
         Case #PB_Event_Menu
             Debug "Case #PB_Event_Menu"
;                 MessageRequester("Select EventMenu()","Select EventMenu()= "+Str(EventMenu()))
             Select EventMenu()
               Case 1
                 Debug "Select EventMenu() & Case 1"
                 SelectedTopicText$ = GetGadgetText(NewTopic)
;                 MessageRequester("NewTopicText$ = ",SelectedTopicText$ )
                 SetActiveGadget(NewTopic)
                 LogSelectedTopic()
            EndSelect ; Select EventMenu()
         Case #PB_Event_CloseWindow
              CloseWindow(Window_0)
              CloseWindow(Window_1)             
 
         EndSelect     ; Select Event           
     EndIf           ; WindowID = #Window_0
       
ForEver          ; OpenWindow_0()
End 
Last edited by RASHAD on Wed Aug 17, 2016 8:53 pm, edited 1 time in total.
Egypt my love
vmars316
Enthusiast
Enthusiast
Posts: 474
Joined: Fri Jun 29, 2012 12:24 am
Contact:

Re: How to Force window_1 to have Focus ?

Post by vmars316 »

Thanks All ,
but still nogo , noerrors :

JHPJHP:
Your changes doesn't open Window_1 (last time also) .
vmars.us Win11 x64 , Martin Guitar 000-16 (1995)
"All things in moderation , except for love and forgiveness."
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4953
Joined: Sun Apr 12, 2009 6:27 am

Re: How to Force window_1 to have Focus ?

Post by RASHAD »

Sorry I did not notice before about the clipboard thing
Previous post updated
Egypt my love
Post Reply