我正在撰写一个关于实施 mo和键盘hoo的 c片的方案,一旦某个特定的关键点点被点击,就会去掉草地窗,将其X、y、高和宽放到Xml文档中。
我不敢肯定什么是错的,但我却不断发现错误的规模和错误参数。
我愿在此问题上提供帮助。
下面是相关的法典。
标准申报:
//rectangle for the windows size
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
//win API
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GetForegroundWindow();
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern bool SetForegroundWindow(IntPtr hwnd);
[DllImport("user32.dll", SetLastError = true)]
static extern bool GetWindowRect(IntPtr hWnd, ref RECT Rect);
相关法典本身
void mouseHook_MouseDown(object sender, MouseEventArgs e)
{
this.handle = GetForegroundWindow();
}
#region keyboard pressed
void keyboardHook_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F9) //if slot selected
{
RECT Rect = new RECT();
SetForegroundWindow(this.handle);
//this.handle = GetForegroundWindow();
GetWindowRect(this.handle, ref Rect);
Grid newSlot = new Grid();
newSlot.topX = Rect.Top;
newSlot.topY = Rect.Left;
newSlot.width = Rect.Right - Rect.Left;
newSlot.height = Rect.Bottom - Rect.Top;
layoutGrid.Add(newSlot);
lbl_slots.Text = layoutGrid.Count().ToString();
}
else if (e.KeyCode == Keys.F10) //if main stack slot selected
{
RECT Rect = new RECT();
SetForegroundWindow(handle);
GetWindowRect(GetForegroundWindow(), ref Rect);
for (int i = 0; i < layoutGrid.Count(); i++) //selecting slot for main stack
{
layoutGrid[i].slot_number = i + 1; //setting slots numbers
if (layoutGrid[i].topX != Rect.Top && layoutGrid[i].topY != Rect.Left)
layoutGrid[i].is_stack = false;
else
{
layoutGrid[i].is_stack = true;
lbl_stackSlot.Text = (i + 1).ToString();
}
}
}
}
EDIT: I have tried to use both public struct RECT and Rectangle, the values with RECT I receive seems to be random, I mean left and top and height and width, sometimes it will find the proper points , but sometimes it seems completely random ones. The values I receive with Rectangle seems to have proper left and top but return wrong height and width.