Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
Hellow
in my form i have a button (its NAME=button1 ) when i pressed the button i wish to go to WndProc and use MessageBox to show "Hellow Word"
C#
namespace MyWndProc
{
    public partial class Form1 : Form
    {
        private const int WM_ACTIVATEAPP = 0x001C;
        private bool appActive = true;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // how can i call WndProc from here
        }

        [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
        protected override void WndProc(ref Message m)
        {
            // Listen for operating system messages. 
            switch (m.Msg)
            {
                // The WM_ACTIVATEAPP message occurs when the application 
                // becomes the active application or becomes inactive. 
                case WM_ACTIVATEAPP:
                    // The WParam value identifies what is occurring.
                    appActive = (((int)m.WParam != 0));
                    // Invalidate to get new text painted. 
                    this.Invalidate();
                    break;

                    case button1://not sure    <----------
                    MessageBox.Show("Hellow World");
                    break;
            }
            base.WndProc(ref m);
        }
    }
}
Posted
Updated 26-Dec-14 10:09am
v4
Comments
Zoltán Zörgő 26-Dec-14 16:16pm    
My I ask what's the purpose of this?
Sergey Alexandrovich Kryukov 26-Dec-14 16:29pm    
Why? why?!
You don't deal with Windows API messages directly; it can only compromise platform compatibility of your code.
You can do it all in pure System.Windows.Forms.
You only need to explain your purpose.
—SA

1 solution

Anyway, this works:
C#
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace SendMessageWndProc
{
    /// <summary>
    /// Description of MainForm.
    /// </summary>
    public partial class MainForm : Form
    {
        private const int WM_USER = 0x0400;
        private const int WM_BTNCLICKED = WM_USER + 1;

        [DllImport("user32.dll")]
        public static extern IntPtr SendMessage(IntPtr hwnd, uint Msg, IntPtr wParam, IntPtr lParam);

        public MainForm()
        {
            InitializeComponent();
        }

        [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
        protected override void WndProc(ref Message m)
        {
            // Listen for operating system messages.
            switch (m.Msg)
            {
                case WM_BTNCLICKED:
                    MessageBox.Show("Hellow World");
                    break;
            }
            base.WndProc(ref m);
        }
        void Button1Click(object sender, EventArgs e)
        {
            SendMessage(this.Handle, WM_BTNCLICKED, IntPtr.Zero, IntPtr.Zero);
        }
    }
}


But you can actually capture the click event in the WndProc. Still, it is a little bit more complicated than this. See the solution given here: http://stackoverflow.com/questions/10635518/capturing-wndproc-message-of-a-certain-button-click[^]
 
Share this answer
 
v2
Comments
Engineer khalid 26-Dec-14 16:39pm    
ok it works fine
i have 2 questions
how did you know that 0x0400+1 is for button1 ?
2nd question could i assume if i have 2 buttons and used the second button instead of the 1st should i write
private const int WM_BTNCLICKED = WM_USER + 2;
Zoltán Zörgő 26-Dec-14 16:42pm    
1) I didn't know. I defined that message. It is a custom message. If you want to capture the exact click without any custom message sent, consult the link I have added.
2) You can define several messages. Consult this: http://msdn.microsoft.com/en-us/library/windows/desktop/ms644931%28v=vs.85%29.aspx
Engineer khalid 26-Dec-14 16:58pm    
sorry i realized the answer from SendMessage thanks
Engineer khalid 26-Dec-14 17:02pm    
Dear Zoltan
i pressed on star 5 but it marks to star 3 ,beleive me may be something wrong with server
Zoltán Zörgő 26-Dec-14 17:05pm    
Than press again, you can change your mind :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900