JBTALKS.CC

标题: VB.NET GetWindowRect() API疑问 [打印本页]

作者: goodhermit95    时间: 2011-2-28 03:38 PM
标题: VB.NET GetWindowRect() API疑问
  1. Dim r As RECT
  2.         Dim b As Boolean = GetWindowRect(GetForegroundWindow(), r)
复制代码
GetForegroundWindow是 API Return Hwnd
GetWindowRect Parameter (hwnd as long,r as RECT) as boolean
出现不明exception,哪里错?
作者: shippo    时间: 2011-2-28 09:22 PM
在这里那么久了,麻烦code也给完整点,不然怎么帮你。

  1. Module Module1
  2.     Public Structure RECT
  3.         Dim Left As Integer
  4.         Dim Top As Integer
  5.         Dim Right As Integer
  6.         Dim Bottom As Integer
  7.     End Structure

  8.     Public Declare Function GetWindowRect Lib "user32" (ByVal HWND As Integer, ByRef lpRect As RECT) As Integer
  9.     Public Declare Function GetForegroundWindow Lib "user32" () As IntPtr

  10.     Sub Main()
  11.         Dim R As RECT
  12.         Dim yesno As Boolean
  13.         yesno = GetWindowRect(GetForegroundWindow(), R)
  14.         Console.WriteLine(R.Top & "and" & R.Left & "and" & R.Bottom & "and" & R.Right)
  15.         Console.WriteLine(yesno)
  16.         Console.ReadLine()
  17.     End Sub

  18. End Module
复制代码

作者: goodhermit95    时间: 2011-2-28 10:01 PM
code给太完整麻烦人去看整个code
我这里的整个code两百多行==

等下我试看你的code
作者: shippo    时间: 2011-2-28 10:16 PM
我的Console.WriteLine(myVariable)出现不明错误,谁来帮帮我?
作者: goodhermit95    时间: 2011-3-1 02:39 PM
我只是想知道我的parameter有没有错,有时API一点点错他就出现exception
因为exception好像跟我讲parameter有错,找了很久都找不到
我刚刚弄了GUI的,

Imports System.Runtime.InteropServices

Public Class Form1
    Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, ByVal rectangle As RECT) As Boolean     'rectangle = receive handle's rectangle RECT,boolean = sucess or fail
    Structure RECT
        Dim x1 As Long
        Dim y1 As Long
        Dim x2 As Long
        Dim y2 As Long
    End Structure

    <DllImport("user32.dll", CharSet:=CharSet.Auto, ExactSpelling:=True)> _
    Public Shared Function GetForegroundWindow() As IntPtr
    End Function
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        Dim r As RECT
        Dim b As Boolean = GetWindowRect(GetForegroundWindow(), r) ’<- 这里exception
        ListBox1.Items.Add(GetForegroundWindow.ToString & " - " & r.x1 & "," & r.y1 & " | " & r.x2 & "," & r.y2 & "(" & b & ")")

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class

A call to PInvoke function 'GetWindowRectangle!GetWindowRectangle.Form1::GetWindowRect' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.
作者: goodhermit95    时间: 2011-3-1 02:46 PM
啊有了我的api declare 错误 谢谢
不好意思给不够完整 @@




欢迎光临 JBTALKS.CC (https://jbtalks.my/) Powered by Discuz! X2.5