开发者

New to C#-would like to add WndProc

Everyone, I am just so new to C#, please help me...

I would like to add WndProc to process messages, I have look into properties but I don't see the thunderbolt to display function name so I can add one I like. I search the internet and see WndProc as

protected override void WndProc(ref Message msg) 
{
   //do something
}

I would li开发者_如何学Goke it to be generated for me, not to type it down ?


WndProc is not a .NET event handler; it's a window procedure, part of the native Win32 API. You won't get any code generation for it as an event handler in Visual Studio.

In Windows Forms all you have to do is override a form's existing WndProc() method and start coding. As it's found in the Form class, there is an autocomplete option for it if you type the following:

override WndProc

which then generates:

    protected override void WndProc(ref Message m)
    {
        base.WndProc(ref m);
    }


just to make this perfectly clear: it's rather improbable that you ever have to do something with WndProc inside winforms/wpf/whatever in the .net world. All this nasty stuff is abstracted and hidden away from you and I don't know a single case where I really needed/missed it.

In Winforms you just wire up events with

Eventname += EventHandlerMethod;

(or you can do such more advanced stuff with annonymous methods and lambdas but don't concern yourself to much with it at the moment).

The easiest way is to just use the Designer and hook your events there:

New to C#-would like to add WndProc

After subscribed to a event with this tool the editor will show you the handler it created and you can start coding away.

Here is a quick example: I just started a new project and added a single button "button1" onto the form:

New to C#-would like to add WndProc

then I hook up the OnClick-Event of the button (select the button and goto the event-tab):

New to C#-would like to add WndProc

and finaly I added code to change the buttons text to "clicked" into the codebehind:

using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace StackOverflowHelp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            // the following line is from InitializeComponent - here you can see how the eventhandler is hook
            // this.button1.Click += new System.EventHandler(this.OnButton1Clicked);
        }

        private void OnButton1Clicked(object sender, EventArgs e)
        {
            var button = sender as Button; // <- same as button1
            if (button == null) return; // <- should never happen, but who is to know?
            button.Text = "clicked";
        }
    }
}

that's all. The nasty dispatching of events is done by the framework.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜