none
Errors with VB6 converted version in Visual Studio 2012 RRS feed

  • Question

  • I encountered errors when adding this block of code (converted from VB6 using Visual Studio 2008) into Visual Studio 2012.

    Original VB6 Code:
    Public Sub PutMeOnTopAndCentered() 'hwnd As Long, frmWidth As Integer, frmHeight As Integer)
    Dim OnTop As Long
    
    OnTop = SetWindowPos(frmPrint.hwnd, HWND_TOPMOST, (Screen.Width - frmPrint.Width) \ 2, _
    (Screen.Height - frmPrint.Height - 405) \ 2, 0, 0, SWP_flags) '405 for Start Menu
    If frmPrint.WindowState = 0 Then
    frmPrint.Left = (Screen.Width - frmPrint.Width) \ 2
    frmPrint.Top = (Screen.Height - frmPrint.Height - 405) \ 2
    End If
    End Sub

    Converted code with errors:

     Public Sub PutMeOnTopAndCentered() 'hwnd As Long, frmWidth As Integer, frmHeight As Integer)
            Dim OnTop As Integer
            OnTop = SetWindowPos(Me.Handle.ToInt32, HWND_TOPMOST, (VB6.PixelsToTwipsX(System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width) - VB6.PixelsToTwipsX(Me.Width)) \ 2, (VB6.PixelsToTwipsY(System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height) - VB6.PixelsToTwipsY(Me.Height) - 405) \ 2, 0, 0, SWP_flags) '405 for Start Menu
            If Me.WindowState = 0 Then
                Me.Left = VB6.TwipsToPixelsX((VB6.PixelsToTwipsX(System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width) - VB6.PixelsToTwipsX(Me.Width)) \ 2)
                Me.Top = VB6.TwipsToPixelsY((VB6.PixelsToTwipsY(System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height) - VB6.PixelsToTwipsY(Me.Height) - 405) \ 2)
            End If
        End Sub

    Errors are on: SetWindowPos, HWND_TOPMOST, VB6

    Does anyone know how to resolve this error, please guide me, does this block of code aim at reposition the form? Should I maximise the Window Form instead, by using only this block of code?

     Public Sub PutMeOnTopAndCentered() 'hwnd As Long, frmWidth As Integer, frmHeight As Integer)
            Me.WindowState = FormWindowState.Maximized
      End Sub



    • Edited by Amy12 Saturday, October 20, 2012 1:14 PM
    Saturday, October 20, 2012 12:34 PM

Answers

  • Hi Amy

    Try Me.TopMost = True if you want the form "pinned" on top of the application window and non-modal, rather than addressing the Windows API directly, as VB6 had to do.

    Although you might be able to do without this method completely and just put the TopMost and positioning lines of code in the procedure that's instantiating and showing the form...


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Amy12 Monday, October 22, 2012 1:32 PM
    Sunday, October 21, 2012 4:29 PM
    Moderator

All replies

  • Hi Amy

    Try Me.TopMost = True if you want the form "pinned" on top of the application window and non-modal, rather than addressing the Windows API directly, as VB6 had to do.

    Although you might be able to do without this method completely and just put the TopMost and positioning lines of code in the procedure that's instantiating and showing the form...


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Amy12 Monday, October 22, 2012 1:32 PM
    Sunday, October 21, 2012 4:29 PM
    Moderator
  • Hi Cindy

    Thanks heaps for your help again. It works better than maximising because when I maximise, the form become so huge! 

    Monday, October 22, 2012 1:31 PM