OberonPlace.com Forums  

Go Back   OberonPlace.com Forums > Developer Forums > VBA > CorelDRAW/Corel DESIGNER VBA

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 22-01-2008, 11:26
Gadget
Guest
 
Posts: n/a
Default Closing a form when something else gets focus

I've created a little pop-up form that will close when the user selects an item from it, but I also want to code it so that if the user clicks anywhere outside of this form it closes.

Can anyone help?

~Gadget~
Reply With Quote
  #2  
Old 22-01-2008, 13:38
wOxxOm's Avatar
wOxxOm wOxxOm is offline
Senior Member
 
Join Date: Mar 2005
Posts: 836
Default

rather tricky solution...

First, set the form's ShowModal property to false (manually in the Form's designer)

Form's code top
Code:
Option Explicit

Private Declare Function SetWindowsHookExW& Lib "user32" (ByVal idHook&, ByVal lpfn&, ByVal hmod&, ByVal dwThreadId&)
Private Declare Function UnhookWindowsHookEx& Lib "user32" (ByVal m_zlhHook&)
Private Declare Function GetCurrentThreadId& Lib "kernel32" ()
Private Declare Function GetWindowLongW& Lib "user32" (ByVal hWnd&, ByVal nIndex%)
Private Declare Function FindWindowExW& Lib "user32" (ByVal hParent&, ByVal hChildAfter&, ByVal lpClassW&, ByVal lpTitleW&)
Form's code anywhere
Code:
Private Sub UserForm_Initialize()
   Dim s$: s = Caption: Caption = Caption & Hex$(Now) & Hex$(Timer)
   hForm = FindWindowExW(0, 0, StrPtr("ThunderDFrame"), StrPtr(Caption)): Caption = s
   wHook = SetWindowsHookExW(5, AddressOf hookProc, GetWindowLongW(AppWindow.Handle, -6), GetCurrentThreadId())
End Sub

Private Sub UserForm_Terminate()
   UnhookWindowsHookEx wHook
End Sub
Standard code module (not the form's)
Put the correct name of your form instead of highlighted UserForm1 below
Code:
Option Explicit

Public wHook&, hForm&

Private Declare Function CallNextHookEx& Lib "user32" (ByVal hhk&, ByVal nCode%, ByVal wParam&, ByVal lParam&)

Function hookProc&(ByVal Lmsg&, ByVal wParam&, ByVal lParam&)
   hookProc = CallNextHookEx(wHook, Lmsg, wParam, lParam)
   If Lmsg = 5 Then If wParam <> hForm Then Unload UserForm1
End Function
Reply With Quote
  #3  
Old 23-01-2008, 06:33
Gadget
Guest
 
Posts: n/a
Default

Quote:
Originally Posted by wOxxOm View Post
rather tricky solution...
I wondered why you added the date/time stamp to the caption, but I suppose it's so that if there are multiple forms with the same caption it will ensure that you are only looking at this one... clever.

I take it that Function hookProc& is called by the system rather than anything I need to worry about?


Is there a (plain english ) resource somewhere that lists all the USER32.dll stuff?

Thanks for your help
~Gadget~
Reply With Quote
  #4  
Old 23-01-2008, 06:51
wOxxOm's Avatar
wOxxOm wOxxOm is offline
Senior Member
 
Join Date: Mar 2005
Posts: 836
Default

yeah, hookProc is being called by Windows at some certain events, google for SetWindowsHookEx syntax, WH_CBT (=5, your case). I haven't studied anything systematically, but rather used small and simple examples (in VB) found on the Net
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
custom form resizing xombie CorelDRAW/Corel DESIGNER VBA 3 20-07-2005 15:03
How sets size form same as size images ? woycek21 Corel Photo-Paint VBA 0 09-07-2005 12:30
attaching a form bumblebee CorelDRAW/Corel DESIGNER VBA 4 04-10-2004 10:27
CD11: Adding input from user form to document ddonnahoe Code Critique 2 09-04-2004 12:43
The CommandBar to linke the VB Form question levin CorelDRAW/Corel DESIGNER VBA 1 24-08-2003 12:40


All times are GMT -5. The time now is 08:00.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.
Copyright © 2011, Oberonplace.com