Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Findwindow - how in managed code?

1,544 views
Skip to first unread message

Tony Hudson

unread,
Sep 13, 2005, 4:58:05 AM9/13/05
to
Hi people,

I have a suite of applications developed for a mobile device using VS2005
Beta 2 and the .NET compact framework. The applications need to be capable of
bringing each other to the front of the Z order. Problem is I cant use
P/invoke routines to do a FindWindow() and then SetForegroundWindow() due to
the application forms being derived from a base class form and visual
inheritance with P/Invoke will not work.

Is there an alternative way of doing this in managed code?

Thanks.

--
Tony Hudson

<ctacke/>

unread,
Sep 13, 2005, 7:34:20 AM9/13/05
to
So why can't you use P/Invoke? Visual inheritance has nothing to do with
it.

-Chris


"Tony Hudson" <TonyH...@discussions.microsoft.com> wrote in message
news:6A7F8EF4-7AF9-4534...@microsoft.com...

Tony Hudson

unread,
Sep 13, 2005, 12:47:22 PM9/13/05
to
Its a bug in VS2005 beta 2 that is not going to be fixed in the first
official release and it is causing me big problems. I have taken the
P/invoke references out of the base class form and exposed the functionality
through a DLL class library that is called by the base class form and it
still does not allow it. I need to find a workaround.

--
Tony Hudson

Daniel Moth

unread,
Sep 13, 2005, 5:28:49 PM9/13/05
to
Can you post a repro?

Cheers
Daniel
--
http://www.danielmoth.com/Blog/

"Tony Hudson" <TonyH...@discussions.microsoft.com> wrote in message

news:0678E4C6-C0C2-4D52...@microsoft.com...

Tony Hudson

unread,
Sep 14, 2005, 9:19:04 AM9/14/05
to
To reproduce the bug perform the following.

1) Create a PocketPC 2003 Class library and call it API_Library.
2) paste the following code into the ".vb" file of this project

Imports System.Runtime.InteropServices
Public Class AppSwapper

<DllImport("coredll.dll")> _
Private Shared Function FindWindow(ByVal className As String, ByVal
wndName As String) As IntPtr
End Function

<DllImport("coredll.dll")> _
Private Shared Function SetForegroundWindow(ByVal ipHandle As
IntPtr) As Int32
End Function

Public Function BringAppToFront(ByVal strWindowTitle As String) As Integer
Dim intErrCode As Integer = 1

Dim ipHandle As IntPtr = FindWindow(vbNullString, strWindowTitle)
If SetForegroundWindow(ipHandle) <> 0 Then
intErrCode = 0
End If

Return intErrCode
End Function

End Class

3) Build this project creating API_Library.dll

4) Create a PocketPC 2003 Class library and call it VIbaseclasses.
5) Add a form to the project
6) rename the form frmBase
7) drop two buttons on the form leaving the default names.
8) paste the following code into the form frmBase

Public Class frmBase
Protected Overridable Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
MsgBox("frmbase - button1 clicked")
'Dim objSwapper As New API_Library.AppSwapper
'objSwapper.BringAppToFront("Test")

End Sub
Protected Overridable Sub Button2_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button2.Click
MsgBox("frmbase - button2 clicked")
End Sub
End Class

9) Add a referenece to API_Library.dll created earlier.
10) Build this project creating VIbaseclasses.dll

11) Create a PocketPC 2003 device application and call it VItest.
12) Add a form to the project leaving it at the default name
13) Add a referenece to VIbaseclasses.dll created earlier.
14) Build the project

15) Exit the project and locate the folder containg the VITest project.
16) using a text editor, edit the file Form1.Designer.vb and change the line
Inherits System.Windows.Forms.Form to Inherits VIbaseclasses.frmBase.
17) save the file and re-open the VITest project. The form should appear as
expected showing the inherited buttons.

18) re-open the VIbaseclasses project and un-comment the lines in the
Button1_Click subroutine refering to the variable objSwapper.
19) save the file and re-build VIbaseclasses project.

20) re-open the VItest project and open the Form1 designer. You will get
the "Visual inheritance is currently disabled message..." with no buttons
from the inherited form.

--
Tony Hudson

Tony Hudson

unread,
Sep 15, 2005, 11:34:05 AM9/15/05
to
Daniel,

You managed to look into or duplicate this?

--
Tony Hudson

Daniel Moth

unread,
Sep 15, 2005, 3:13:10 PM9/15/05
to
Hi Tony... if nobody else picks it up, I may have time to look at it over
the weekend... just to be clear, is your issue that the designer gets lost
_or_ that the pinvokes don't work (which is what I originally understood)...

Also if this is the first time you are posting repro steps (you insinuate
that you've told someone before), I suggest you post them to
ladybug/feedbackcenter so the vsd/netcf teams have visibility of the
issue...

"Tony Hudson" <TonyH...@discussions.microsoft.com> wrote in message

news:64E63E63-B5D3-4053...@microsoft.com...

Daniel Moth

unread,
Sep 18, 2005, 6:51:06 AM9/18/05
to
I could repro what you describe and I have a fix for you.

- I run this using VS2005 TS RC
- No need for 3 separate projects, just use 1 with all the code files in it
(in fact, I generally advise against having forms in dlls)
- No need to edit the form in a text file for visual inheritance, just
follow the steps here:
http://www.danielmoth.com/Blog/2005/07/visual-form-inheritance-with-netcf.html

So, indeed the inherited form is not displayed in the designer when the form
it inherits from makes pinvoke calls.

To rectify this, follow the following steps:
1. Right click on frmBase and select "View Class Diagram"
2. Select the class shape and then go to the properties window. Select
Custom Attributes so a window come up.
3. The window has one line
Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()
4. Add one more line
DesktopCompatible(true)
and click OK
5. Rebuild your solution and the form displays fine in the designer

Let me know if it does not work for you.

"Daniel Moth" <dmo...@hotmail.com> wrote in message
news:e3TpDmiu...@TK2MSFTNGP10.phx.gbl...

Tony Hudson

unread,
Sep 19, 2005, 6:14:04 AM9/19/05
to
Daniel, You are a star. Works fine. Thankyou once again.
0 new messages