CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Nov 2004
    Posts
    54

    How can I use Windows messages through the .NET Framework?

    Through my other two topics, I think I'm coming to realize what I really need is a way to do my own receiving/processing and sending of Windows messages on Forms. So I shall try asking a different question.. how can I do this? Is there any way I can get to some sort of WndProc function, or use something like a message map (something I'm not previously familiar with)?

  2. #2
    Join Date
    Mar 2004
    Location
    (Upper-) Austria
    Posts
    2,899

    Re: How can I use Windows messages through the .NET Framework?

    Why not putting a delegate onto your OnClick events of your buttons/checkboxes etc? It's much more easier and safer then using oldstyle WndProc's and message handling. But for sure: It is possible by simply inheriting from System::Windows::Forms::Form and override the private member procedure WndProc.
    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!

  3. #3
    Join Date
    Nov 2004
    Posts
    54

    Re: How can I use Windows messages through the .NET Framework?

    It would be nice if I could just attach a delegate to some event, but as far as I know there aren't events that get triggered for arbitrary or non-standard functions. One of the messages I want to try processing is my own custom message.

    I will try inheriting and overriding the WndProc method.

    [edit]
    Sure enough, overriding the WndProc method was what I needed.
    Last edited by Monolith; May 24th, 2005 at 04:29 PM.

  4. #4
    Join Date
    Sep 2006
    Posts
    68

    Arrow Re: How can I use Windows messages through the .NET Framework?

    Quote Originally Posted by Monolith
    It would be nice if I could just attach a delegate to some event, but as far as I know there aren't events that get triggered for arbitrary or non-standard functions. One of the messages I want to try processing is my own custom message.

    I will try inheriting and overriding the WndProc method.

    [edit]
    Sure enough, overriding the WndProc method was what I needed.
    perhaps you can Show your solution here??
    i would really like that

  5. #5
    Join Date
    Nov 2004
    Posts
    54

    Re: How can I use Windows messages through the .NET Framework?

    It's been a while since I worked with this code, but I believe this should be the basics of what you're looking for...

    Here's an example of the receiving WndProc
    Code:
    public __gc class progresswindow : public System::Windows::Forms::Form
    {
    private: System::Windows::Forms::ProgressBar *  i_progressBar;
    private: System::Windows::Forms::TextBox *  i_textBox;
    
    // These are just a couple functions that I needed to trigger from a
    // different thread.
    private: System::Void i_workerthread_progress(int p_current, int p_total)
    		 {
    			i_progressBar->Maximum = p_total;
    			i_progressBar->Value = p_current;
    		 }
    
    private: System::Void i_workerthread_add_message(System::String* p_message)
    		 {
    			i_textBox->Text = String::Concat(i_textBox->Text, p_message);
    			i_textBox->Select(i_textBox->Text->Length, 0);
    			i_textBox->ScrollToCaret();
    		 }
    
    // This is the WndProc function that I'm overridding so that I can process
    // a couple of my own windows messages.
    protected: virtual void WndProc(System::Windows::Forms::Message* message)
    		 {
    			switch (message->Msg) {
    				case WM_APP + 1:
    					i_workerthread_progress((int)message->WParam, (int)message->LParam);
    				break;
    				
    				case WM_APP + 2:
    				{
    					String* s = new String((const char*)message->LParam.ToPointer());
    					i_workerthread_add_message(s->Replace("\n", "\r\n"));
    				}
    				break;
    			}
    			
    			Form::WndProc(message);
    		 }
    
    };
    And here's an example of sending messages to the window:
    Code:
    public __gc class colorreductionthread {
    
    		// These two functions were called from inside my worker thread.  They
    		// use PostMessage and SendMessage to send windows messages to my form
    		// that's running on another thread.
    
    		// SendMessage blocks execution until the target window has processed
    		// the message where as PostMessage just sends the message and returns
    		// immediately.
    
    		void progress_callback(int p_current, int p_total) {
    			PostMessage((HWND)i_parentForm->Handle.ToInt32(), WM_APP + 1, (WPARAM)p_current, (LPARAM)p_total);
    		}
    		
    		void add_message_callback(const std::string& p_message) {
    			SendMessage((HWND)i_parentForm->Handle.ToInt32(), WM_APP + 2, 0, (LPARAM)p_message.c_str());
    		}
    };
    The WndProc function is really the core component to processing your own windows messages.
    Last edited by Monolith; October 9th, 2006 at 02:55 AM.

  6. #6
    Join Date
    Sep 2006
    Posts
    68

    Unhappy Re: How can I use Windows messages through the .NET Framework?

    //Thanx, but still a little problem.
    //i am including the namespace system stuff
    //i am using .net Visual Studio 2005 for C++, but it doesn't know //SendMessage..
    //any clue?

    //Edit: What do you import/include/use etc.???

    Found it out, windows.h has to be included..
    But now i have the problem that i get some rather strange errors,
    when i use:
    Code:
    SendMessage(HWND_BROADCAST,1,0,0);
    When i remove this line, everything goes fine
    What could be the cause???

    Code:
    1>Meas.obj : error LNK2028: unresolved token (0A0000AA) "extern "C" long __stdcall SendMessageW(struct HWND__ *,unsigned int,unsigned int,long)" (?SendMessageW@@$$J216YGJPAUHWND__@@IIJ@Z) referenced in function "extern "C" long __clrcall SendMessage(struct HWND__ *,unsigned int,unsigned int,long)" (?SendMessage@@$$J0YMJPAUHWND__@@IIJ@Z)
    1>Meas.obj : error LNK2019: unresolved external symbol "extern "C" long __stdcall SendMessageW(struct HWND__ *,unsigned int,unsigned int,long)" (?SendMessageW@@$$J216YGJPAUHWND__@@IIJ@Z) referenced in function "extern "C" long __clrcall SendMessage(struct HWND__ *,unsigned int,unsigned int,long)" (?SendMessage@@$$J0YMJPAUHWND__@@IIJ@Z)
    1>C:\Documents and Settings\alm0001428\My Documents\Visual Studio 2005\Projects\V3_Spurious_Detection\Debug\V3_Spurious_Detection.exe : fatal error LNK1120: 2 unresolved externals
    Last edited by stefboerrigter; October 9th, 2006 at 08:09 AM.

  7. #7
    Join Date
    Nov 2004
    Posts
    54

    Re: How can I use Windows messages through the .NET Framework?

    I do include windows.h, and I believe I'm using SendMessage from the global namespace. Make sure you're linking with user32.lib. A default windows project should already link to this, but this is the only library that MSDN says to import for SendMessage.

  8. #8
    Join Date
    Sep 2006
    Posts
    68

    Red face Re: How can I use Windows messages through the .NET Framework?

    In your code u use:

    Code:
     SendMessage((HWND)i_parentForm->Handle.ToInt32(), WM_APP + 2, 0, (LPARAM)p_message.c_str());
    what do i_parentForm->Handle.ToInt32(), and the other variables/parameters mean??
    are they standard? or created by you in the header or code?
    And wich version of .NET do you use? wich studio?
    Greetings and thanx

  9. #9
    Join Date
    Nov 2004
    Posts
    54

    Re: How can I use Windows messages through the .NET Framework?

    Code:
    SendMessage(
    (HWND)i_parentForm->Handle.ToInt32(),
    i_parentForm is my pointer to the form that I want to send my custom message to. The first parameter that SendMessage takes is the handle to the window that you want to send the message to. Since I want to send my message to i_parentForm, I must get the window handle for i_parentForm. A handle is really just a 32 bit integer, and I cast that to a HWND which is the type that SendMessage takes.

    Code:
    WM_APP + 2,
    The second parameter for SendMessage is the ID of the message that you want to send. This could be some standard windows message, but what I'm doing is sending my own custom message. WM_APP is the base ID for you to come up with your own message IDs. In this case I have added an offset of 2 since I probably already had ones for +0 and +1. In your WndProc function where you'll process this message, you'll need to match the incoming message with the message that you specify here.

    Code:
    0,
    (LPARAM)p_message.c_str());
    The last two parameters are what's known as the WPARAM and LPARAM. Different windows messages use these parameters for different purposes. If you're making your own custom message, you can use these for whatever you want. In this case I'm not using the WPARAM for anything so I set it to 0, and I'm using the LPARAM to pass the pointer of a simple character string. One thing to remember about passing pointers, you need to make sure the memory their pointing to will still be valid by the time you try using the pointer in your WndProc function. This is why I use SendMessage in this case because it calls the WndProc function before SendMessage returns, meaning my p_message string will still be on the stack.

    I hope this helps.

  10. #10
    Join Date
    Sep 2006
    Posts
    68

    Re: How can I use Windows messages through the .NET Framework?

    Thanx a lot, i know i'm not the best programmer, but i'm learning a lot from you, thank you.

    after your tips i made some modifications to my code, and i managed to get rid of the most of the errors.

    some still exist. they are all created by this code:
    Code:
    SendMessage((HWND)detection.Handle,msg.wParam,(WPARAM)0,(LPARAM)0);
    These are the errors..:

    Code:
    1>.\Meas.cpp(47) : error C2440: 'type cast' : cannot convert from 'System::IntPtr' to 'HWND'
    1>        Conversion requires a constructor or user-defined-conversion operator, which can't be used by const_cast or reinterpret_cast
    I hope you can help me one more time,

    Regards

    -------------------------
    EDIT: one last error left,
    Last edited by stefboerrigter; October 10th, 2006 at 02:15 AM.

  11. #11
    Join Date
    Nov 2004
    Posts
    54

    Re: How can I use Windows messages through the .NET Framework?

    I'm afraid I don't know the solution to that off the top of my head. That appears to simply be an issue with getting the window handle casted to the right type though.

  12. #12
    Join Date
    Sep 2006
    Posts
    68

    Thumbs up Re: How can I use Windows messages through the .NET Framework?

    ok, ill try some different casting methods, it has to be type HWND right?
    thanx for youre help..
    many thanx

    Regards

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured