SaveFileRequester in the foreground [Resolved]

Just starting out? Need help? Post your questions and find answers here.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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 
BERESHEIT
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Post 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)
ImageThe happiness is a road...
Not a destination
Post Reply