0

I have encountered problem that I wasn't able to solve so maybe I ll have some luck here. I am overriding WndProc in my application like this :

#include "stdafx.h"
#include <Windows.h>
#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>
#using <System.dll>

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Drawing;


namespace EXAMPLE
{
    public ref class Form1: public System::Windows::Forms::Form
    {
        public:
        Form1()
        {
            this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->ClientSize = System::Drawing::Size(GetSystemMetrics(SM_CXSCREEN)/2, GetSystemMetrics(SM_CYSCREEN)/2);
            this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::Sizable;
            this->Name = L"Test";
            this->Text = L"Test";
            this->ResumeLayout(false);
        }
        protected:  virtual void WndProc(System::Windows::Forms::Message% m) override 
        {
            //Do stuff in here
            Form::WndProc(m);
        }
   };
}
[STAThreadAttribute]
int main()
{
    Application::Run(gcnew projector_fixer::Form1() );
    return 0;
}

This works great but since I would like to have my main as clean as possible I have decided to create "controller class" that handles everything by connecting with main. So for example, rather than including event for mouse_click inside EXAMPLE namespace, I would include clicked object in my "controller class" constructor and handle that inside this class. This is easy, but when wanted to override wndproc I realized that I have no idea how do that. I imagine that it might look something like this:

inside main

myclass ^MYCLASS=gcnew myclass(//some way to send wndproc override to function//);

MYCLASS.h

#pragma once

ref class MYCLASS
{
public:
    MYCLASS();
    overriden_wndproc(System::Windows::Forms::Message% m);
};

MYCLASS.cpp

MYCLASS::MYCLASS(//some way to send wndproc override to function//)
{
     overriden_wndproc=//some way to send wndproc override to function//;
}
MYCLASS::overriden_wndproc(System::Windows::Forms::Message% m)
{
     //Do stuff in here
     Form::WndProc(m);
}

Please note that this is only my guessed version of what would seem logical. All help would be much appreciated.

Peter

I am adding whole wndproc since i strongly believe that event raising might become tricky because i am using break in my function.

protected:  virtual void WndProc(System::Windows::Forms::Message% m) override 
{
    System::Diagnostics::Debug::WriteLine(m);
    switch(hZoomSwitch)
    {
        case 0:
        {
            hScrollBar1->Value=0;
            hTimer->Stop();
            hZoomSwitch=2;
            break;
        }
        case 1:
        {
            hTimer->Start();
            hZoomSwitch=2;
            break;
        }
    }
    switch(vZoomSwitch)
    {
        case 0:
        {
            vScrollBar1->Value=0;
            vTimer->Stop();
            vZoomSwitch=2;
            break;
        }
        case 1:
        {
            vTimer->Start();
            vZoomSwitch=2;
            break;
        }
    }
    Form::WndProc(m);
}
Peter Kottas
  • 893
  • 13
  • 27
  • That cannot work. Consider raising an event instead that passes m. Your controller can subscribe the event. – Hans Passant Sep 28 '12 at 16:22
  • I certainly think that it is possible to do this. I looked here http://stackoverflow.com/questions/8780700/how-to-wrap-a-win32-wndproc-into-a-c-class?rq=1 before writing this post, actually I googled for some time and found few people who seemed to get it working, but I cant get my head around how to do this in my particular case. – Peter Kottas Sep 28 '12 at 17:19
  • 1
    That requires Marshal::GetFunctionPointerForDelegate(). Lots of ways to shoot your foot doing that, an event is much easier to get right. – Hans Passant Sep 28 '12 at 17:26
  • Hmm .. okey do you mind going into detail of your event idea ? I am not really sure how to do it and if you consider it a good approach it might be good solution for others who run into similar problem. – Peter Kottas Sep 28 '12 at 17:44

0 Answers0