如何使用backgroundworker线程调用API GetWindowRect? [英] how to use backgroundworker thread call the API GetWindowRect?

查看:77
本文介绍了如何使用backgroundworker线程调用API GetWindowRect?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用backgroundworker线程调用API GetWindowRect?

任何人都可以给我一个演示,坦克〜!



how to use backgroundworker thread call the API GetWindowRect?
can anybody give me a demo,tanks~!

[DllImport("user32.dll")]
    private static extern int GetWindowRect(int hwnd, ref Rect lpRect);

    private void bgw_DoWork(object sender, DoWorkEventArgs e)
    {

        while (bgw.WorkerSupportsCancellation)
        {
            GetWindowRect(hWnd,ref lpRect);
        }


    }





问题的重要性不在于定义hWnd和lpRect,

i只是给出一个演示。

API GetWindowRect必须基于GUI线程,但backgroundworker是另一个不基于GUI线程的线程。

如何解决这个问题,任何人都可以正确编译。

即使我使用System.Windows.Forms.Control.Invoke方法,

但该方法只有两个重载,一个没有参数,

另一个参数是对象数组。

如何在对象数组中使用ref params?



the question importance is not at define hWnd and lpRect,
i just give a demo.
the API GetWindowRect must based on GUI thread,but backgroundworker is another thread not based on GUI thread.
how can solve this quetion, can anybody compilation right.
even though i use System.Windows.Forms.Control.Invoke method,
but the method only have two overload,one is not have params,
another's params is object array.
how can i use ref params in object array?

推荐答案

我猜你的问题可以通过简单的重构代码来解决。

1.根据给出的代码,必须在某处定义两个参数,在你的情况下,是不是在类中定义。

2.您可以简单地编写一个没有参数的函数来调用GetWindowRect,并通过control.invoke在后台工作程序中调用该函数或者dispatcher.invoke。从语法上讲,编译器不应该抱怨。

3.我不确定你是通过这种方式调用getWindowRect来实现的?应该使用后台工作程序进行一些异步调用(由网络事件触发或持续处理某些事情)。那么你想连续监控你的窗户大小吗?您可以使用Windows大小更改事件(检查MSDN以获取确切的事件名称)。

4.祝你好运。
I "guess" your problem can be solved by simply refactoring the code.
1. By the code given, the two parameter must be defined somewhere, in your case, are they defined in class.
2. You can simply write a function with no parameter to call the GetWindowRect, and call that function in your background worker through control.invoke or dispatcher.invoke. Syntactically, compiler should not complain.
3. I am not sure what you are trying to achieve by calling the getWindowRect this way? Background worker should be used to do some asynchronous call (triggered by network event or continuously working on something). SO you want to monitor your windows size continuously? You can use the windows size change event (check MSDN for exact event name).
4. Good luck.


[DllImport("user32.dll")]
private static extern int GetWindowRect(int hwnd, ref System.Drawing.Rectangle lpRect);





系统.Drawing.Rectangle lpRect = new Rectangle();

GetWindowRect(hWnd,ref lpRect);



System.Drawing.Rectangle lpRect = new Rectangle();
GetWindowRect(hWnd,ref lpRect);


这篇关于如何使用backgroundworker线程调用API GetWindowRect?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆