Page 1 of 1

SaveFileRequester in the foreground [Resolved]

Posted: Wed Jan 02, 2008 4:44 pm
by Kwai chang caine
Hello at all

Is it possible to Position a saveFileRequester in the foreground ?

Thanks for your help 8)

Posted: Wed Jan 02, 2008 5:29 pm
by gnozal
You could hook the requester like in this post : http://www.purebasic.fr/english/viewtopic.php?t=24456
So you can force it to the foreground.

Posted: Wed Jan 02, 2008 5:37 pm
by netmaestro

Code: Select all

;====================================================================
; Demo:              Position a Requester
; Author:            Lloyd Gallant (netmaestro)
; Date:              January 2, 2008
; Target OS:         Microsoft Windows All
; Target Compiler:   PureBasic 4.0 and later
; Applies to:        Any requester or messagebox of dialog class
;====================================================================

Global hook

Procedure CbtProc(nCode, wParam, lParam) 
  Select nCode 
    Case #HCBT_CREATEWND 
      hWnd = wParam 
      *pcbt.CBT_CREATEWND = lParam 
      *pcs.CREATESTRUCT = *pcbt\lpcs 
      If *pcs\lpszClass = 32770    
        w = GetSystemMetrics_(#SM_CXSCREEN)
        h = GetSystemMetrics_(#SM_CYSCREEN)
        *pcs\x = w/2 -*pcs\cx /2 
        *pcs\y = h/2 -*pcs\cy /2
        UnhookWindowsHookEx_(hook) 
      EndIf 
  EndSelect 
  ProcedureReturn CallNextHookEx_(hook, nCode, wParam, lParam) 
 EndProcedure 


; This code, all except for one line (SetWindowsHookEx..[..]) is the PB doc example for SaveFileRequester:

StandardFile$ = "C:\autoexec.bat"   ; set initial file+path to display
; With next string we will set the search patterns ("|" as separator) for file displaying:
;  1st: "Text (*.txt)" as name, ".txt" and ".bat" as allowed extension
;  2nd: "PureBasic (*.pb)" as name, ".pb" as allowed extension
;  3rd: "All files (*.*) as name, "*.*" as allowed extension, valid for all files
Pattern$ = "Text (*.txt)|*.txt;*.bat|PureBasic (*.pb)|*.pb|All files (*.*)|*.*"
Pattern = 0    ; use the first of the three possible patterns as standard

hook = SetWindowsHookEx_(#WH_CBT, @CbtProc(), #Null, GetCurrentThreadId_()) 

File$ = SaveFileRequester("Please choose file to save", StandardFile$, Pattern$, Pattern)
If File$
  MessageRequester("Information", "You have selected following file:"+Chr(10)+File$, 0)
Else
  MessageRequester("Information", "The requester was canceled.", 0) 
EndIf

Posted: Wed Jan 02, 2008 5:38 pm
by Kwai chang caine
Thanks GNOZAL

I go to read this thread 8)

Posted: Wed Jan 02, 2008 5:45 pm
by netmaestro
I'm assuming by "foreground" you mean screencentered. If not then I missed the boat.

Posted: Wed Jan 02, 2008 6:15 pm
by Kwai chang caine
My hero, you are here :D

I don't see your POST, i POST At the same time
I was in the process of breaking my head on the code that gave me GNOZAL :cry:

I test your code and give you news :D

Posted: Wed Jan 02, 2008 6:21 pm
by Kwai chang caine
I'm assuming by "foreground" you mean screencentered. If not then I missed the boat.
I mean "au premier plan" in french :oops:

When i clic on a picture in a window, i open a SaveFileRequester for save the picture on the disk.
But the problem is the SaveFileRequester is behind my window picture :?
It's normal because the SaveFileRequester has not the focus.
I have to click on the SaveFileRequester to pass before.

I looking for a code that does this automatically :D

Image

Posted: Wed Jan 02, 2008 7:36 pm
by netmaestro
Could you post some code showing this? Because it really shouldn't be happening. (and I can't reproduce it)

Posted: Thu Jan 03, 2008 9:05 am
by Kwai chang caine
This is my code :

Code: Select all

;==================================================================== 
; Demo:              Position a Requester 
; Author:            Lloyd Gallant (netmaestro) 
; Date:              January 2, 2008 
; Target OS:         Microsoft Windows All 
; Target Compiler:   PureBasic 4.0 and later 
; Applies to:        Any requester or messagebox of dialog class 
;==================================================================== 

#Form = 1
#ImagePressePapier = 10

Global hook 
Global Encodeur
Global Extension.s

Procedure CbtProc(nCode, wParam, lParam) 
  Select nCode 
    Case #HCBT_CREATEWND 
      hWnd = wParam 
      *pcbt.CBT_CREATEWND = lParam 
      *pcs.CREATESTRUCT = *pcbt\lpcs 
      If *pcs\lpszClass = 32770    
        w = GetSystemMetrics_(#SM_CXSCREEN) 
        h = GetSystemMetrics_(#SM_CYSCREEN) 
        *pcs\x = w/2 -*pcs\cx /2 
        *pcs\y = h/2 -*pcs\cy /2 
        UnhookWindowsHookEx_(hook) 
      EndIf 
  EndSelect 
  ProcedureReturn CallNextHookEx_(hook, nCode, wParam, lParam) 
 EndProcedure 

hook = SetWindowsHookEx_(#WH_CBT, @CbtProc(), #Null, GetCurrentThreadId_()) 
Hwnd = OpenWindow(#Form, 10000, 10000, 640, 480, "")

Repeat
 
 Delay(100)
      
 If GetClipboardImage(#ImagePressePapier) 
 
  Fichier$ = SaveFileRequester("Please choose file to save", "c:\", "Image Jpg|*.jpg|Image BitMap|*.bmp", 0) 
  ExtensionChoisie = SelectedFilePattern()
  
  If Trim(Fichier$) <> ""
   
   Select ExtensionChoisie
   
    Case 0
         
     UseJPEGImageEncoder()
     Encodeur = #PB_ImagePlugin_JPEG
     Extension = ".jpg"
          
    Case 1
    
     Encodeur = #PB_ImagePlugin_BMP 
     Extension = ".bmp"
            
   EndSelect
  
   If Right(LCase(Fichier$), 4) <> LCase(Extension)
    Fichier$ + Extension
   EndIf
    
   SaveImage(#ImagePressePapier,Fichier$, Encodeur) 
   
  EndIf
  
  ClearClipboard() 
  Break
  
 EndIf
 
ForEver 

End

Posted: Thu Jan 03, 2008 6:21 pm
by netmaestro
OK, very good. You've got a couple of errors there, but they don't bear on the problem. First, you have a window and no WaitWindowEvent() or WindowEvent(). You must have an event loop if you're opening a window. Second, the hook must be placed directly before the OpenFileRequester, nowhere else. Where you've got it it would act on any dialog that comes up next.

Now those are fixed, back to the topic. The reason this is happening is that your application doesn't have the focus when the event happens that fills the clipboard with an image. So we just have to give it the focus before we call the OpenFileRequester and it should be solved. We'll use a hotkey for that:

Code: Select all

;==================================================================== 
; Demo:              Position a Requester 
; Author:            Lloyd Gallant (netmaestro) 
; Date:              January 2, 2008 
; Target OS:         Microsoft Windows All 
; Target Compiler:   PureBasic 4.0 and later 
; Applies to:        Any requester or messagebox of dialog class 
;==================================================================== 

#Form = 1 
#ImagePressePapier = 10 

Global hook 
Global Encodeur 
Global Extension.s 

Procedure CbtProc(nCode, wParam, lParam) 
  Select nCode 
    Case #HCBT_CREATEWND 
      hWnd = wParam 
      *pcbt.CBT_CREATEWND = lParam 
      *pcs.CREATESTRUCT = *pcbt\lpcs 
      If *pcs\lpszClass = 32770    
        w = GetSystemMetrics_(#SM_CXSCREEN) 
        h = GetSystemMetrics_(#SM_CYSCREEN) 
        *pcs\x = w/2 -*pcs\cx /2 
        *pcs\y = h/2 -*pcs\cy /2 
        UnhookWindowsHookEx_(hook) 
      EndIf 
  EndSelect 
  ProcedureReturn CallNextHookEx_(hook, nCode, wParam, lParam) 
 EndProcedure 


Hwnd = OpenWindow(#Form, 1000, 1000, 640, 480, "")

; give ourselves a handle to grab the application by 
wHotkey.w = 593 ; CTRL + Q
SendMessage_(WindowID(#Form), #WM_SETHOTKEY, wHotkey, 0)

Repeat 
  
 WaitWindowEvent(1) 
      
 If GetClipboardImage(#ImagePressePapier) 
 
  ; give this application keyboard focus 
  keybd_event_(#VK_CONTROL, 0,0,0)
  keybd_event_(#VK_Q, 0,0,0)
  keybd_event_(#VK_CONTROL, 0,#KEYEVENTF_KEYUP,0)
  keybd_event_(#VK_Q, 0,#KEYEVENTF_KEYUP,0)
  
  hook = SetWindowsHookEx_(#WH_CBT, @CbtProc(), #Null, GetCurrentThreadId_()) 
  Fichier$ = SaveFileRequester("Please choose file to save", "c:\", "Image Jpg|*.jpg|Image BitMap|*.bmp", 0) 
  ExtensionChoisie = SelectedFilePattern() 
  
  If Trim(Fichier$) <> "" 
    
   Select ExtensionChoisie 
    
    Case 0 
          
     UseJPEGImageEncoder() 
     Encodeur = #PB_ImagePlugin_JPEG 
     Extension = ".jpg" 
          
    Case 1 
    
     Encodeur = #PB_ImagePlugin_BMP 
     Extension = ".bmp" 
            
   EndSelect 
  
   If Right(LCase(Fichier$), 4) <> LCase(Extension) 
    Fichier$ + Extension 
   EndIf 
    
   SaveImage(#ImagePressePapier,Fichier$, Encodeur) 
    
  EndIf 
  
  ClearClipboard() 
  Break 
  
 EndIf 
  
ForEver 

End 

Posted: Thu Jan 03, 2008 6:32 pm
by Kwai chang caine
ALLELUIA !!!!!

It's exactly that what i wanted :D

How can I make you one day, anything you give me ?? 8)

You are a mother for me but also a great mother :D

Thanks,Thanks,Thanks,Thanks,Thanks,Thanks,Thanks,
I am your servant, ....MASTER 8)