Visio Guy

Visio Discussions => Programming & Code => Topic started by: mmulvenna on March 25, 2009, 01:51:39 PM

Title: Anchor Windows
Post by: mmulvenna on March 25, 2009, 01:51:39 PM
Anchor windows are new to me, looks like it has a lot of uses. I am trying the following code from the sdk.
Private Sub UserForm_Resize()
    txtForm.Width = txtForm.Parent.Width - 10
    txtForm.Height = txtForm.Parent.Height - 10
End Sub

Then add the following code to the document project:

Visual Basic for Applications
Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

Private Const GWL_STYLE = (-16)
Private Const WS_CHILD = &H40000000
Private Const WS_VISIBLE = &H10000000

Public Sub AddWindow_Example()

    Dim vsoWindow As Visio.Window
    Dim frmNewWindow As UserForm
    Dim lngFormHandle As Long

    'Add a new Anchor Bar window docked to the bottom of the Visio drawing window
    Set vsoWindow = ActiveWindow.Windows.Add("My New Window", visWSVisible + visWSDockedBottom, visAnchorBarAddon, , , 300, 210)
  
    'Create a new windows form
    Set frmNewWindow = New frmMain
       
    'Get the 32-bit handle of the new window.
    lngFormHandle = FindWindow(vbNullString, "My New Window")
   
    SetWindowLong lngFormHandle, GWL_STYLE, WS_CHILD Or WS_VISIBLE
    SetParent lngFormHandle, vsoWindow.WindowHandle32

End Sub



The window does get added but the user form is not in the window. Anyone have any ideas as to why it is not there.
Just looking for a sample so i can start playing with the Anchor Windows concept


Thanks in advance
Mike
Title: Re: Anchor WiIndows
Post by: wapperdude on March 25, 2009, 02:49:05 PM
Hi Mike

Don't know if you've seen this already, but, it does go thru an example.

http://msdn.microsoft.com/en-us/library/aa140261(office.10).aspx

Title: Re: Anchor Windows
Post by: Visio Guy on March 25, 2009, 02:51:34 PM
I couldn't see right away what was wrong, but you can play with this sample.

There are a bunch of mini web pages in the zip directory that the VBA code needs. It displays these web pages in a docked browser window that belongs to the Visio drawing window.

When you drop shapes, they are "sponsored" (gag) and a related banner-ad shows in the docked window.

Title: Re: Anchor Windows
Post by: AndyW on March 26, 2009, 08:25:03 AM
Your code seems ok, but you are missing a jiggle of the window to make it update, the size of the parent is increased and decreased by a pixel forcing the update. All this is covered in the excellent book, Visio 2003 Developer's Survival Pack by Graham Wideman.

Call this after your SetParent.


Public Sub JiggleWindow(hwnd As Long)

    ' This function causes Windows to refresh the onscreen display according
    ' to changes we've made to style bits and parent
    Dim l As Long
    Dim t As Long
    Dim W As Long
    Dim H As Long
    Dim flags As Long
    Dim HR As Long
    Dim ARect As RECT

    If hwnd <> 0 Then
   
        HR = GetWindowRect(hwnd, ARect)
   
        If HR <> 0 Then
           
            l = ARect.Left
            t = ARect.Top
            W = ARect.Right - ARect.Left
            H = ARect.Bottom - ARect.Top

            flags = SWP_NOCOPYBITS Or SWP_NOMOVE Or SWP_NOZORDER
           
            HR = SetWindowPos(hwnd, 0, l, t, W, H + 1, flags)
            HR = SetWindowPos(hwnd, 0, l, t, W, H, flags)
       
        End If
       
    End If
   
End Sub

Title: Re: Anchor Windows
Post by: Visio Guy on March 26, 2009, 08:46:59 AM
Hi Andy,

I'm using Visio 2007 and my window is just fine without the jiggle. I'm aware of Graham's jiggle procedure, but I didn't see a need for it, because everything is working just fine.

Are you using Visio 2003?
Title: Re: Anchor Windows
Post by: mmulvenna on March 26, 2009, 12:17:03 PM
Quote from: AndyW on March 26, 2009, 08:25:03 AM
Your code seems ok, but you are missing a jiggle of the window to make it update, the size of the parent is increased and decreased by a pixel forcing the update. All this is covered in the excellent book, Visio 2003 Developer's Survival Pack by Graham Wideman.


Andy,
Thanks. If you are refering to the Visio Guys code, his works fine on 2007 on my machine also. If you are referring to the code I posted it is a complete copy of the code from the SKK. Since VISIO guys worked I did bother to look any further at the stuff I sent.

Thanks a lot for taking the time to respond.
Title: Re: Anchor Windows
Post by: AndyW on March 27, 2009, 12:00:00 PM
Yes I'm using 2003.
Title: Re: Anchor Windows
Post by: Zigoto on August 12, 2009, 02:09:20 PM
Hi, work on that today,
mmulvenna what is wrong in visio help file is that it is not writen that you should enter "My New Window" in the userform.caption field.
even thet you have to move the window or even to resize it to get it updated.

Ziorg77