Sign in to follow this  
chrisliando

[.net] What is Project::Subclass::NativeWindowsEvent and how to use it?

Recommended Posts

(1) What is Project::Subclass::NativeWindowsEvent and how to use it? I've got codes in old C++ syntax and I want to change it to the new one. // =================================================================== // NativeWindow Subclassing // =================================================================== ref class Subclass : public System::Windows::Forms::NativeWindow { public: delegate void WindowsEventHandler(Object ^sender, Message ^uMsg); event WindowsEventHandler ^NativeWindowsEvent; Subclass(IntPtr pWindowHandle) { this->AssignHandle(pWindowHandle); } void SendWndProc( System::Windows::Forms::Message ^uMsg ) { //super::WndProc(uMsg); } protected: void WndProc( System::Windows::Forms::Message ^uMsg ) { //__super::WndProc(uMsg); if ( Project::Subclass::NativeWindowsEvent != nullptr ) Project::Subclass::NativeWindowsEvent(this, uMsg); } }; When I compiled it, the error message was: Error C3918: usage requires 'Project::Subclass::NativeWindowsEvent' to be a data member see declaration of 'Project::Subclass::NativeWindowsEvent' How to change it to the new C++ syntax? (2) What is keyword "__super" in the old C++ syntax converted to the new C++ syntax? Thank you very much.

Share this post


Link to post
Share on other sites
Quote:
Original post by Hodgman
That's not C++ code...


Hodgman is correct. Are you porting code from standard c++ to c++/clr, or managed c++ to c++/clr? Or something else?

Quote:
Original post by chrisliando
(1)
What is Project::Subclass::NativeWindowsEvent and how to use it?


I have no idea, because I don't know what library you're using. It's clearly not a part of the .net framework, and a search found nothing. Check the docs for the library, or ask one of it's developers.

Quote:
Original post by chrisliando
(2)
What is keyword "__super" in the old C++ syntax converted to the new C++ syntax?


Assuming you're using c++/clr, I believe that the __super keyword is still valid. You can also call it through the base class name as well. For example, in your SendWndProc method, you should be able to do NativeWindow::WndProc.

Share this post


Link to post
Share on other sites
1. Yes, I am porting from standard c++ to c++/clr, or managed c++ to c++/clr. But in my editor IntelliSense, there is a "namespace" called Project. After I typed Project, I gave ::, it showed ref class Subclass and the last :: is event NativeWindowsEvent.

I also confused here, so the Project is a namespace, Subclass is a ref class ( it said to be NativeWindow subclassing in the hint) and NativeWindowsEvent is an event.

How to solve this?

! I forgot to tell you, I am using the Visual C++ 2005 Express Edition (WHIDBEY)and .Net 2.0. !!

I attached the complete old C++ syntax code below.

2. How to convert __super keyword?

3. Is there a tool to convert from C# codes to C++ or old C++ to new C++ or VB to C++?

Thank you very much.

The Complete Codes:

#pragma once
//==================================================
// Form1.h
//==================================================
namespace TestMultScroll
{
using namespace System;
using namespace System::IO;
using namespace System::Data;
using namespace System::Text;
using namespace System::Drawing;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Runtime::InteropServices;

// ===================================================================
// NativeWindow Subclassing
// ===================================================================
__gc class Subclass : public System::Windows::Forms::NativeWindow
{
public:
__delegate void WindowsEventHandler(Object * sender, Message * uMsg);
__event WindowsEventHandler * NativeWindowsEvent;

Subclass(IntPtr pWindowHandle)
{
this->AssignHandle(pWindowHandle);
}

void SendWndProc( System::Windows::Forms::Message * uMsg)
{
__super::WndProc(uMsg);
}

protected: void WndProc( System::Windows::Forms::Message * uMsg)
{
__super::WndProc(uMsg);
if (NativeWindowsEvent != 0)
NativeWindowsEvent(this, uMsg);
}
};



// ===================================================================
// API Function: GetScrollPos()
// ===================================================================
[DllImport("user32.dll")]
extern int GetScrollPos(IntPtr hWnd, int nBar);
// ===================================================================
// API Function: PostMessageA()
// ===================================================================
[DllImport("user32.dll")]
extern bool PostMessageA(IntPtr hwnd, int wMsg, int wParam, int lParam);



///
/// Summary for Form1
///
/// WARNING: If you change the name of this class, you will need to change the
/// 'Resource File Name' property for the managed resource compiler tool
/// associated with all .resx files this class depends on. Otherwise,
/// the designers will not be able to interact properly with localized
/// resources associated with this form.
///
public __gc class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
}

protected:
void Dispose(Boolean disposing)
{
if (disposing && components)
{
components->Dispose();
}
__super::Dispose(disposing);
}

private:System::ComponentModel::Container * components;
private: Subclass * sClass1 ;
private: Subclass * sClass2 ;
private: Subclass * sClass3 ;

public: System::Windows::Forms::RichTextBox * RichTextBox1 ;
public: System::Windows::Forms::RichTextBox * RichTextBox2 ;
public: System::Windows::Forms::RichTextBox * RichTextBox3 ;

// ===================================================================
// for NativeWindow and PostMessageA
// ===================================================================
__value enum NWConst {
SBS_HORZ = 0,
SBS_VERT = 1,
SB_THUMBPOSITION = 4,
WM_COMMAND = 0x111,
WM_HSCROLL = 0x114,
WM_VSCROLL = 0x115,
WM_MOUSEWHEEL = 0x20A,
WM_USER = 0x400
};


// ===================================================================

///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///

private:
void InitializeComponent()
{
this->RichTextBox1 = new System::Windows::Forms::RichTextBox ();
this->RichTextBox2 = new System::Windows::Forms::RichTextBox ();
this->RichTextBox3 = new System::Windows::Forms::RichTextBox ();
this->SuspendLayout();
//
// RichTextBox1
//
this->RichTextBox1->Location = System::Drawing::Point (24, 24);
this->RichTextBox1->Name = "RichTextBox1";
this->RichTextBox1->Size = System::Drawing::Size (128, 128);
this->RichTextBox1->TabIndex = 0;
this->RichTextBox1->Text = "RichTextBox1";
this->RichTextBox1->WordWrap = false;
//
// RichTextBox2
//
this->RichTextBox2->Location = System::Drawing::Point (160, 24);
this->RichTextBox2->Name = "RichTextBox2";
this->RichTextBox2->Size = System::Drawing::Size (128, 128);
this->RichTextBox2->TabIndex = 1;
this->RichTextBox2->Text = "RichTextBox2";
this->RichTextBox2->WordWrap = false;
//
// RichTextBox3
//
this->RichTextBox3->Location = System::Drawing::Point (296, 24);
this->RichTextBox3->Name = "RichTextBox3";
this->RichTextBox3->Size = System::Drawing::Size (128, 128);
this->RichTextBox3->TabIndex = 3;
this->RichTextBox3->Text = "RichTextBox3";
this->RichTextBox3->WordWrap = false;
//
// Form1
//
this->AutoScaleBaseSize = System::Drawing::Size (5, 13);
this->ClientSize = System::Drawing::Size (448, 174);
this->Controls->Add(this->RichTextBox3);
this->Controls->Add(this->RichTextBox2);
this->Controls->Add(this->RichTextBox1);
this->Name = "Form1";
this->Text = "Form1";
this->ResumeLayout(false);
this->Load += new System::EventHandler (this, Form1_Load);

this->RichTextBox1->VScroll += new System::EventHandler (this,RichTextBox1_VScroll);
this->RichTextBox2->VScroll += new System::EventHandler (this,RichTextBox2_VScroll);
this->RichTextBox3->VScroll += new System::EventHandler (this,RichTextBox3_VScroll);
this->RichTextBox1->HScroll += new System::EventHandler (this,RichTextBox1_HScroll);
this->RichTextBox2->HScroll += new System::EventHandler (this,RichTextBox2_HScroll);
this->RichTextBox3->HScroll += new System::EventHandler (this,RichTextBox3_HScroll);

}
void Form1_Load(System::Object * sender, System::EventArgs * e)
{
sClass1 = new Subclass(RichTextBox1->Handle);
sClass2 = new Subclass(RichTextBox2->Handle);
sClass3 = new Subclass(RichTextBox3->Handle);

this->sClass1->NativeWindowsEvent += new Subclass::WindowsEventHandler (this,sClass_WindowProcedure);
this->sClass2->NativeWindowsEvent += new Subclass::WindowsEventHandler (this,sClass_WindowProcedure);
this->sClass3->NativeWindowsEvent += new Subclass::WindowsEventHandler (this,sClass_WindowProcedure);

for (int i = 0; i <= 100; i++)
{
String * msg;
if (i % 4 == 0)
{
msg = String::Format(" this is a longer string to force HScroll {0} \n", __box(i));
RichTextBox1->AppendText(msg);
RichTextBox2->AppendText(msg);
RichTextBox3->AppendText(msg);
}
else
{
msg = String::Format(" this is a string {0} \n", __box(i));
RichTextBox1->AppendText(msg);
RichTextBox2->AppendText(msg);
RichTextBox3->AppendText(msg);
}
}
}


public: void sClass_WindowProcedure(Object * sender, System::Windows::Forms::Message * uMsg)
{
switch (uMsg->Msg)
{
case (WM_VSCROLL):
case (WM_HSCROLL):

if (uMsg->HWnd == RichTextBox1->Handle)
{
ArrayList * x = new ArrayList(2);
x->Add(RichTextBox2);
x->Add(RichTextBox3);
ThumbScrollHandler(RichTextBox1, x, uMsg);
}
else if (uMsg->HWnd == RichTextBox2->Handle)
{
ArrayList * x = new ArrayList(2);
x->Add(RichTextBox1);
x->Add(RichTextBox3);
ThumbScrollHandler(RichTextBox2, x, uMsg);
}
else if (uMsg->HWnd == RichTextBox3->Handle)
{
ArrayList * x = new ArrayList(2);
x->Add(RichTextBox1);
x->Add(RichTextBox2);
ThumbScrollHandler(RichTextBox3, x, uMsg);
}
break;
}
}



private: void ThumbScrollHandler(RichTextBox * sender, ArrayList* receivers, Message * uMsg)
{
sClass2->NativeWindowsEvent -= new Subclass::WindowsEventHandler(this, sClass_WindowProcedure);
for (int i=0; iCount; i++)
{
RichTextBox * rtbox = dynamic_cast(receivers->get_Item(i));
Message msg = Message::Create(rtbox->Handle, uMsg->Msg, uMsg->WParam, uMsg->LParam);
sClass2->SendWndProc(&msg);
}
sClass2->NativeWindowsEvent += new Subclass::WindowsEventHandler(this, sClass_WindowProcedure);
}

private: void RichTextBox1_VScroll(Object * sender, System::EventArgs * e)
{
ArrayList * richTextBoxes = new ArrayList(2);
richTextBoxes->Add( RichTextBox2);
richTextBoxes->Add( RichTextBox3);
VerticalScroll(RichTextBox1, richTextBoxes);
}

private: void RichTextBox2_VScroll(Object * sender,System::EventArgs * e)
{
ArrayList * richTextBoxes = new ArrayList(2);
richTextBoxes->Add( RichTextBox1);
richTextBoxes->Add( RichTextBox3);
VerticalScroll(RichTextBox2, richTextBoxes);
}

private: void RichTextBox3_VScroll(Object * sender,System::EventArgs * e)
{
ArrayList * richTextBoxes = new ArrayList(2);
richTextBoxes->Add( RichTextBox1);
richTextBoxes->Add( RichTextBox2);
VerticalScroll(RichTextBox3, richTextBoxes);
}

private: void RichTextBox1_HScroll(Object * sender,System::EventArgs * e)
{
ArrayList * richTextBoxes = new ArrayList(2);
richTextBoxes->Add( RichTextBox2);
richTextBoxes->Add( RichTextBox3);
HorizontalScroll(RichTextBox1, richTextBoxes);
}

private: void RichTextBox2_HScroll(Object * sender,System::EventArgs * e)
{
ArrayList * richTextBoxes = new ArrayList(2);
richTextBoxes->Add( RichTextBox1);
richTextBoxes->Add( RichTextBox3);
HorizontalScroll(RichTextBox2, richTextBoxes);
}

private: void RichTextBox3_HScroll(Object * sender,System::EventArgs * e)
{
ArrayList * richTextBoxes = new ArrayList(2);
richTextBoxes->Add( RichTextBox1);
richTextBoxes->Add( RichTextBox2);
HorizontalScroll(RichTextBox3, richTextBoxes);
}



private: void VerticalScroll(RichTextBox * sender, ArrayList * receivers)
{
int position = GetScrollPos(sender->Handle, SBS_VERT);
for (int i=0; iCount; i++)
{
RichTextBox * rtbox = dynamic_cast(receivers->get_Item(i));
PostMessageA(rtbox->Handle, WM_VSCROLL, SB_THUMBPOSITION + 0x10000 * position, 0);
}

}

private: void HorizontalScroll(RichTextBox * sender, ArrayList * receivers)
{
int position = GetScrollPos(sender->Handle, SBS_HORZ);
for (int i=0; iCount; i++)
{
RichTextBox * rtbox = dynamic_cast(receivers->get_Item(i));
PostMessageA(rtbox->Handle, WM_HSCROLL, SB_THUMBPOSITION + 0x10000 * position, 0);
}
}
};
}

//============================================
// Form1.cpp
//============================================

#include "stdafx.h"
#include "Form1.h"
#include

using namespace TestMultScroll;

int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
System::Threading::Thread::CurrentThread->ApartmentState = System::Threading::ApartmentState::STA;
Application::Run(new Form1());
return 0;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by chrisliando
1. Yes, I am porting from standard c++ to c++/clr, or managed c++ to c++/clr.


Ok... so which one? I guess since the "old c++ syntax" code you posted is managed c++, it's the latter. Might I suggest that you read up on the differences between c++, managed c++, and c++/clr? The distinctions are very important.

Quote:
Original post by chrisliando
But in my editor IntelliSense, there is a "namespace" called Project. After I typed Project, I gave ::, it showed ref class Subclass and the last :: is event NativeWindowsEvent.

I also confused here, so the Project is a namespace, Subclass is a ref class ( it said to be NativeWindow subclassing in the hint) and NativeWindowsEvent is an event.


Well, you know what how events work in .net, right? So if I were to guess, NativeWindowEvent is raised whenever a win32 event message is received. If you don't know how .net events work, by all means, look it up.

But the fact remains, this is third party code (and might I say, very ugly code.. who calls a class "Subclass"?). You need to look at it's docs or talk to it's developers. Nobody here is going to be able to pull an answer out of thin air.

Share this post


Link to post
Share on other sites
It's a code to make 2 richTextBoxes scroll at the same time..
Please look here:
http://www.codeproject.com/KB/vb/VbNetScrolling.aspx

The original code is in VB, I've got the C++ from the messageboard at the bottom of the page. The header is C++.

Where can I find the differences between c++, managed c++, and c++/clr? And do you have syntax list of changes from the old syntax to the new one (WHIDBEY)?

I just know the standard event, not such kind of event.

Then, how to modify the code to make it work? I think I just need to repair this part because the other part is standard:

protected: void WndProc( System::Windows::Forms::Message ^uMsg )
{
//__super::WndProc(uMsg);
if ( Project::Subclass::NativeWindowsEvent != nullptr )
Project::Subclass::NativeWindowsEvent(this, uMsg);
}

Can you help me, please? Maybe by looking at the C# or VB you might find out what this is.

Thank you very much..

Share this post


Link to post
Share on other sites
Quote:
Original post by chrisliando
Where can I find the differences between c++, managed c++, and c++/clr? And do you have syntax list of changes from the old syntax to the new one (WHIDBEY)?

Have you tried here.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this