English 中文(简体)
迁就地窗户的规模在所有时间都是错误的。
原标题:Getting foreground window size wrong all the time

我正在撰写一个关于实施 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.

问题回答

最后,我找到了解决办法。 如果任何人都遇到同样的问题,这一数字就会随之而来。

我最后使用GetWindowInfo,而不是GetWindowRect,并且是像harm。

注意到该守则的例子和第一条在网上查阅:http://kennethorman.blogspot.com/201008/c-net-active-windows-size-helper.html” http://kennethorman.blogspot.com/2010/08/c-net-active-windows-size-helper.html。

感谢大家努力提供帮助。

使用<代码>System.Drawing.Rectangle 是不正确的。 这些田地不匹配。 你对<代码>RECT的定义是正确的。

I m guessing a small, but You problem may be related to the comment at the past of the GetWindowRect 7. 管理网专题。

在Vista下进行的与WINVER=6无关的申请将在此得到一套误导性的价值,而这种价值并不说明“玻璃”的皮条航空适用于窗户。 即便在Aero Basic(没有玻璃)也出现这种情况,以保持一致性。 工作方式(如果你不想确定WINVER=6)似乎对获得DwmGetWindowAttribute(DWMWA_EXTENDED_FRAME_BOUNDS)功能的“GProc Address”(如果你不希望确定WINVER=6)具有动态约束力,并且使用“GetWindowAttribute(......)”功能,将其称作“DWMWA_EXTENDED_FRAME_BOUNDS”的论点,要求真正建立窗口。





相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签