From: mayayana on
Also, SetWinEventHook. It's a very easy way to
hook all windows and get selected messages sent
through a callback function. It's probably overkill
here, but I've used it to be informed when the focused
window is changing and to get the hWnd of the newly
focused window.

>
> I've seen several ways offered here, by noone mentioned using a hook.
>
> You can set a hook on your apps own window, and intercept wnd messages.
>
> http://www.vbaccelerator.com/home/Vb/Code/Libraries/Hooks/vbAccelerator_H
> ook_Library/article.asp
>
> I wouldn't use their library, but this is a decent article. The WH_CBT
> hook is what you would use....and this message may be the one to look
> for:
>
> HCBT_SETFOCUS
>
> Windows calls the WH_CBT hook with this hook code when Windows is about
> to set the focus to any window. In the case of thread-specific hooks, the
> window must belong to the thread. If the filter function returns TRUE,
> the focus does not change.
>
> The wParam parameter contains the handle to the window that receives the
> focus. The lParam parameter contains the handle to the window that loses
> the focus.
> ------------------------------------------------------------
>
> Most of, if not all, of the CBT hooks seem to happen just prior to the
> event.


From: Rick Raisley on
"DanS" <t.h.i.s.n.t.h.a.t(a)r.o.a.d.r.u.n.n.e.r.c.o.m> wrote in message
news:Xns9CD3E307926BAthisnthatroadrunnern(a)216.196.97.131...
> "Rick Raisley" <heavymetal-A-T-bellsouth-D-O-Tnet> wrote in
> news:u9sJyHfcKHA.5796(a)TK2MSFTNGP06.phx.gbl:
>
>> I'm embarrassed to ask this, but things aren't working quite as I
>> expected. I want to run some non-trivial code (non-trivial here means
>> it can take a bit of time to do) whenever a user goes back to my
>> running program. Basically, he could have changed the document loaded
>> in another (non-VB) program, and I want to see if that has happened,
>> and if so, get information on the active document from that program.
>>
>> Anyhow, I thought that the Form_Activate event might work, or
>> Form_GotFocus, but both of those don't fire when the form isn't
>> changed (this simple program only has one form). Form_Paint works, but
>> fires hundreds of times, so is not what I want.
>>
>> What can I use to determine that the user has gone back to my running
>> app, and as he/she may have changed the document in the other app, I
>> would then do a check to see if it's changed?
>>
>
> I've seen several ways offered here, by noone mentioned using a hook.
>
> You can set a hook on your apps own window, and intercept wnd messages.
>
> http://www.vbaccelerator.com/home/Vb/Code/Libraries/Hooks/vbAccelerator_H
> ook_Library/article.asp
>
> I wouldn't use their library, but this is a decent article. The WH_CBT
> hook is what you would use....and this message may be the one to look
> for:
>
> HCBT_SETFOCUS
>
> Windows calls the WH_CBT hook with this hook code when Windows is about
> to set the focus to any window. In the case of thread-specific hooks, the
> window must belong to the thread. If the filter function returns TRUE,
> the focus does not change.
>
> The wParam parameter contains the handle to the window that receives the
> focus. The lParam parameter contains the handle to the window that loses
> the focus.
> ------------------------------------------------------------
>
> Most of, if not all, of the CBT hooks seem to happen just prior to the
> event.

Looks interesting, although I've not used hooks to speak of, and especially
if you don't recommend using their library (which the code uses), I don't
know where to start. Let me explain exactly what I'm looking for:

I have a VB program (with no 3rd party controls and preferably no add-on
controls other than the very basic ones, as this runs from a server) which,
when started, reads the property information from the current Solid Edge
(CAD) document, and displays it. The program allows the user to make
selections in materials, etc., and then copy that information to custom
properties within the Solid Edge document.

Now, after starting, or after reading the Solid Edge document information,
the user can change to a different document. I do check, when the user
clicks the Apply Changes button, that the document is the same one that was
originally read, and if not, inform the user. But what I'd like to do in
addition is to display on the title bar of my program something like "NOT
CURRENT DOCUMENT" if the user reads a document with the program, then
changes to a different document, then goes back to my program. I'd like it
to be very obvious that the changes will NOT be made to the originally
selected document.

Anyhow, my intention was that every time my application /newly/ receives the
focus (like GotFocus would do for multiple forms, and only then, to save
time), I'd check the current document's filename, and if it is different,
display the above in the window title. And if it's the same, remove the
text.

--
Regards,

Rick Raisley
heavymetal-A-T-bellsouth-D-O-T-net


From: Nobody on
"Rick Raisley" <heavymetal-A-T-bellsouth-D-O-Tnet> wrote in message
news:e4YYOiocKHA.4780(a)TK2MSFTNGP04.phx.gbl...
> Anyhow, my intention was that every time my application /newly/ receives
> the focus (like GotFocus would do for multiple forms, and only then, to
> save time), I'd check the current document's filename, and if it is
> different, display the above in the window title. And if it's the same,
> remove the text.

If you hate subclassing, try this code(air code):

Public Declare Function GetForegroundWindow Lib "user32" () As Long

Public Function IsActive() As Boolean
Dim f As Form

For Each f In Forms
If f.hWnd = GetForegroundWindow() Then
IsActive = True
Exit Function
End If
Next
End Function


From: Rick Raisley on
"Nobody" <nobody(a)nobody.com> wrote in message
news:efhjc4ocKHA.744(a)TK2MSFTNGP05.phx.gbl...
> "Rick Raisley" <heavymetal-A-T-bellsouth-D-O-Tnet> wrote in message
> news:e4YYOiocKHA.4780(a)TK2MSFTNGP04.phx.gbl...
>> Anyhow, my intention was that every time my application /newly/ receives
>> the focus (like GotFocus would do for multiple forms, and only then, to
>> save time), I'd check the current document's filename, and if it is
>> different, display the above in the window title. And if it's the same,
>> remove the text.
>
> If you hate subclassing, try this code(air code):
>
> Public Declare Function GetForegroundWindow Lib "user32" () As Long
>
> Public Function IsActive() As Boolean
> Dim f As Form
>
> For Each f In Forms
> If f.hWnd = GetForegroundWindow() Then
> IsActive = True
> Exit Function
> End If
> Next
> End Function
>
>

Thanks, Nobody. What would trigger this code? I only have one form, so can
eliminate the For Each. But assuming the IsActive tells you if the app has
the focus (and other apps do not), wouldn't I have to run it, say, every
second or so to see if it's changed?

--
Regards,

Rick Raisley
heavymetal-A-T-bellsouth-D-O-T-net


From: Rick Raisley on
"Rick Raisley" <heavymetal-A-T-bellsouth-D-O-Tnet> wrote in message
news:eGeu7ipcKHA.5796(a)TK2MSFTNGP06.phx.gbl...
> "Nobody" <nobody(a)nobody.com> wrote in message
> news:efhjc4ocKHA.744(a)TK2MSFTNGP05.phx.gbl...
>> "Rick Raisley" <heavymetal-A-T-bellsouth-D-O-Tnet> wrote in message
>> news:e4YYOiocKHA.4780(a)TK2MSFTNGP04.phx.gbl...
>>> Anyhow, my intention was that every time my application /newly/ receives
>>> the focus (like GotFocus would do for multiple forms, and only then, to
>>> save time), I'd check the current document's filename, and if it is
>>> different, display the above in the window title. And if it's the same,
>>> remove the text.
>>
>> If you hate subclassing, try this code(air code):
>>
>> Public Declare Function GetForegroundWindow Lib "user32" () As Long
>>
>> Public Function IsActive() As Boolean
>> Dim f As Form
>>
>> For Each f In Forms
>> If f.hWnd = GetForegroundWindow() Then
>> IsActive = True
>> Exit Function
>> End If
>> Next
>> End Function
>>
>>
>
> Thanks, Nobody. What would trigger this code? I only have one form, so can
> eliminate the For Each. But assuming the IsActive tells you if the app has
> the focus (and other apps do not), wouldn't I have to run it, say, every
> second or so to see if it's changed?
>
>

Answering my own question, it appears it would have to be triggered by a
timer (because if the program knew when it actually /received/ the focus,
I'd use that event instead.

So, I implemented a timer that checks every 1 second, using debug.print
IsActive to watch the result, and it works nicely. As I was concerned how
quickly it would work (therefore how much time it would use doing such a
check this often), I set up a loop in the function, to do it first 100
times, then 1,000, then 30,000 and found that on my PC 30,000 loops takes
about 0.05 seconds, meaning each check takes roughly .002 milliseconds. Even
at that, VB6 (running from the IDE) only took 1-2% of my CPU usage. And
without the loop, 0%.

So, I can even set it to check every half second, without taking any time to
speak of, and get what I want. I like it! It's simple, and doesn't require
subclassing. Sure, it's a workaround, but a simple one, and one I can live
with, I think.

Thanks. ;-)

--
Regards,

Rick Raisley
heavymetal-A-T-bellsouth-D-O-T-net


First  |  Prev  |  Next  |  Last
Pages: 1 2 3 4 5
Prev: dhSqliteDemo
Next: Scientific to Decimal