none
VB2008 代码升级问题 RRS feed

  • 问题

  • 我在制作一个菜单。需要在屏幕边缘像QQ一样自动停靠。网上找来了代码经过修改得到以下内容:

     

    Public Class primary_menu

        Private Declare Function GetCursorPos Lib "user32" (ByVal lpPoint As POINTAPI) As Long
        Private Structure POINTAPI
            Dim X As Long
            Dim Y As Long
        End Structure
        Dim p As POINTAPI

        Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
        Private Const HWND_TOPMOST = -1
        Private Const SWP_NOMOVE = &H2
        Private Const SWP_NOSIZE = &H1

        Private Sub Form_Load()
            SetWindowPos(Me.hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE Timer1.Interval = 100)
            Timer1.Enabled = True
        End Sub


        Private Sub Timer1_Timer()
            On Error Resume Next
            GetCursorPos(p)
            If Me.Top <= 0 Then
                If p.Y > Me.Height / 15 + Me.Top / 15 Or p.X > Me.Width / 15 + Me.Left / 15 Or p.X < Me.Left / 15 Then
                    Me.Top = 0 - Me.Height + 50
                End If
                If p.X > Me.Left / 15 And p.X < Me.Left / 15 + Me.Width / 15 And p.Y < 3 Then
                    Me.Top = 0
                End If
            End If

            If Me.Left <= 0 Then
                If p.Y > Me.Height / 15 + Me.Top / 15 Or p.Y < Me.Top / 15 Or p.X > Me.Width / 15 + Me.Left / 15 Then
                    Me.Left = 0 - Me.Width + 50

                End If
                If p.X < 3 And p.Y > Me.Top / 15 And p.Y < Me.Height / 15 + Me.Top / 15 Then
                    Me.Left = 0
                End If
            End If

            If Me.Left >= Screen.Width - Me.Width Then

                If p.Y > Me.Height / 15 + Me.Top / 15 Or p.Y < Me.Top / 15 Or p.X < Me.Left / 15 Then
                    Me.Left = Screen.Width - 50

                End If
                If p.X > Screen.Width / 15 - 3 And p.Y > Me.Top / 15 And p.Y < Me.Height / 15 + Me.Top / 15 Then
                    Me.Left = Screen.Width - Me.Width
                End If
            End If

            If Me.Top >= Screen.Height - Me.Height Then
                If p.Y > Me.Height / 15 + Me.Top / 15 Or p.X > Me.Width / 15 + Me.Left / 15 Or p.X < Me.Left / 15 Then
                    Me.Top = Screen.Height + 50
                End If
                If p.X > Me.Left / 15 And p.X < Me.Left / 15 + Me.Width / 15 And p.Y > Screen.Height / 15 - 3 Then
                    Me.Top = Screen.Height - Me.Height
                End If
            End If
        End Sub
    End Class

     

     

    其中红色加粗的字体是有错的地方,不知道要怎么修改!哪位高手指导一下!急啊

     

    Me.hwnd的错误提示为“"hwnd"不是"XXXXX.Primary_Menu"的成员” (XXXX是我的软件名称)

    Timer1的错误提示是“应为逗号、")"或有效的表达式继续符”

    下面的关于Screen的提示为“"Width"("Height")不是"System.Windows.Forms.Screen"的成员"”

    2009年1月22日 3:23

答案

  • SetWindowPos(Me.hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE Timer1.Interval = 100)

     

     

     

     

     

         SetWindowPos(Me.hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)

    Timer1.Interval = 100

     

     

     

    -------------------------------

     

     

    Screen.Width

     

    Screen.PrimaryScreen.Bounds.Width

     

    =----------------------------

    Screen.Height

     

    Screen.PrimaryScreen.Bounds.Height

    2009年1月23日 3:20

全部回复

  • 代码应该是放到一个form里的吧!有Form_Load

     

    Timer1.Interval = 100应该在下一行吧!

     

     

    Screen。Bonds。Height

     

     

    2009年1月22日 3:30
    版主
  • SetWindowPos(Me.hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE Timer1.Interval = 100)

     

     

     

     

     

         SetWindowPos(Me.hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)

    Timer1.Interval = 100

     

     

     

    -------------------------------

     

     

    Screen.Width

     

    Screen.PrimaryScreen.Bounds.Width

     

    =----------------------------

    Screen.Height

     

    Screen.PrimaryScreen.Bounds.Height

    2009年1月23日 3:20
  •  

    那个关于Screen的问题解决了。Hwnd要替换成handle

    谢谢

    2009年1月25日 5:38