简体   繁体   English

谁能告诉我我在摘要中犯错了吗

[英]Can anyone tell me were I am making mistake in the snippet

public partial class Form1 : Form
{
  [DllImport("coredll.dll")]
  static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

  const int GWL_WNDPROC = -4;

  public delegate int WindProc(IntPtr hWnd, uint msg, long Wparam, long lparam);

    public Form1()
    {
        InitializeComponent();

        WindProc SampleProc = new WindProc (SubclassWndProc);

        SetWindowLong(this .Handle , GWL_WNDPROC,
            SampleProc.Method .MethodHandle.Value.ToInt32());

    }

public int SubclassWndProc(IntPtr  hwnd, uint  msg, long  Wparam, long  lparam)
{
    return 1;
}

Here is the sample which i was trying to take the window procedure of a form, this is how i do in C++ i get the windwproc easlily if i try the same in C# .net 3.5 i am unable to get the window proc,, after calling SetWindowLong API application hangs and it pops up some dont send report... i have read this is the way to get the window proc.. please let me know were i am making mistake... 这是我尝试采用窗体的窗口过程的示例,这是我在C ++中所做的事情,如果我在C#.net 3.5中尝试相同的操作,则无法轻松获得windwproc,在调用SetWindowLong API应用程序挂起,并弹出一些不发送报告的信息...我已阅读这是获取窗口proc的方法..请让我知道我在犯错误...

SampleProc.Method .MethodHandle.Value.ToInt32() SampleProc.Method .MethodHandle.Value.ToInt32()

Just use SampleProc . 只需使用SampleProc If that fails, try marshalling it to a FunctionPointer . 如果失败,请尝试将其编组到FunctionPointer

The delegate instance does not need to be static. 委托实例不需要是静态的。 No idea why you think it should. 不知道为什么你认为应该。

I think you need to declare your delegate instance at the form level, like this: 我认为您需要在表单级别声明您的委托实例,如下所示:

public partial class Form1 : Form
{
    [DllImport("coredll.dll")]
    static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
    const int GWL_WNDPROC = -4;
    public delegate int WindProc(IntPtr hWnd, uint msg, 
        long Wparam, long lparam);
    private WindProc _SampleProc;
    public Form1()
    {
        InitializeComponent();
        _SampleProc = new WindProc(SubclassWndProc);
        SetWindowLong(this.Handle, GWL_WNDPROC,
            _SampleProc.Method.MethodHandle.Value.ToInt32());

    }
    public int SubclassWndProc(IntPtr  hwnd, uint  msg, 
        long  Wparam, long  lparam)
    {
        return 1;
    }

Your original delegate instance was being declared in the form's constructor, where it immediately went out of scope (and thus wasn't around anymore to be called back to). 您最初的委托实例在窗体的构造函数中声明,在该实例中它立即超出范围(因此不再被调用)。

There might be other problems with your sample, too. 您的样本可能还会有其他问题。

You are working with the compact framework, right? 您正在使用紧凑型框架,对吗? Are you doing everything from the same process? 您是否从同一过程中进行所有操作?

I have experienced this kind of trouble myself, if the process where the window is created is not the same process as the message stems from. 如果创建窗口的过程与消息所源自的过程不同,则我本人也遇到了这种麻烦。 However I actively used SendMessage to send a message. 但是我积极使用SendMessage发送消息。 If I did this from a different process, I got the "send error report" page. 如果我是通过其他流程执行此操作,则会看到“发送错误报告”页面。 I now have found out, that if the SendMessage comes from the same process, things work fine. 我现在发现,如果SendMessage来自同一进程,则一切正常。

In the above you could probably replace process with thread, although i am no sure. 在上面,您可能可以用线程替换进程,尽管我不确定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在尝试从 a.csv 读取信息并将其放入 C# 中的数组中。 谁能告诉我为什么代码不起作用? - I am trying to read information from a .csv and put it into an array in C#. Can anyone tell me why the code doesn't work? 序列化时我犯了什么错误? - What mistake am I making when serializing? 我无法为此代码编写测试..有人告诉我该怎么做吗? - I am not able to write test for this code ..anyone tell me how to do? 谁能告诉我在Linq是否可行? - Can anyone tell me if this is possible to do in Linq? 谁能告诉我SoapDocumentMethodAttribute做什么? - Can anyone tell me what SoapDocumentMethodAttribute does? 谁能告诉我为什么我的触发器没有按照我的预期工作? - Can anyone tell me why my triggers are not working the way I intended them to? 谁能告诉我为什么我不能从项目外部访问此代码? - Can anyone tell me why I cant access this code from outside of it's project? 谁能告诉我为什么我的XML编写器不编写属性? - Can anyone tell me why my XML writer is not writing attributes? 任何人都可以告诉我为什么AllowDrop不能与文本框一起使用 - Can anyone tell me why the AllowDrop does not work with text boxes 谁能告诉我为什么此代码的可维护性指数仅为40? - Can anyone tell me why the maintainability index is only 40 for this code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM