none
How to create CLR(C++/CLI) override onPaint RRS feed

  • Question

  • I want to use CLR(C++/CLI) to create a project like C# Form.

    But I don't know how to do same thing like C# do.

    For example C# I want to override Panel

    But I don't know how to do same thing in  CLR

    how to do this?

    I want to create a CLR project like my c# windowns form they both has Form.

    Thanks.




    • Edited by CY_Chen Monday, July 29, 2019 8:35 AM
    Friday, July 19, 2019 1:36 AM

Answers

  • When both C++ and C# projects are included in solution, you can add a reference from one project to another and use the C# classes and controls in C++, or vice versa.

    If you want to convert the P class to C++, check this:

    // P.h file:
    
    #pragma once
    
    using namespace System;
    using namespace System::Drawing;
    using namespace System::Windows::Forms;
    using namespace System::Drawing::Drawing2D;
    using namespace System::Collections::Generic;
    
    
    namespace test
    {
    	namespace F = System::Windows::Forms;
    
    	public ref class P abstract : public Panel
    	{
    
    	protected:
    
    		virtual property F::CreateParams^ CreateParams
    		{
    			F::CreateParams^ get( ) override
    			{
    				F::CreateParams^ cp = __super::CreateParams;
    				cp->ExStyle |= 0x00000020; // TODO: use 'WS_EX_TRANSPARENT' from <Windows.h>
    
    				return cp;
    			}
    		}
    
    		virtual void OnPaintBackground( PaintEventArgs^ e ) override
    		{
    		}
    
    		virtual void OnPaint( PaintEventArgs^ e ) override
    		{
    			__super::OnPaint( e );
    
    			Graphics^ graphics = e->Graphics;
    
    			graphics->InterpolationMode = InterpolationMode::HighQualityBilinear;
    			graphics->PixelOffsetMode = PixelOffsetMode::HighQuality;
    			graphics->SmoothingMode = SmoothingMode::HighQuality;
    
    			OnDrawR( graphics );
    		}
    
    
    		virtual void OnDrawR( Graphics^ g ) abstract;
    
    	};
    
    
    
    	ref class SamplePanel : public P
    	{
    	protected:
    
    		virtual void OnDrawR( Graphics^ g ) override
    		{
    			g->DrawString( "Example", SystemFonts::DialogFont, Brushes::Red, 10, 12 );
    		}
    	};
    }
    
    

     

    But, the User Interface can be made easier in C#, I think.

    Friday, July 19, 2019 4:58 AM

All replies

  • When both C++ and C# projects are included in solution, you can add a reference from one project to another and use the C# classes and controls in C++, or vice versa.

    If you want to convert the P class to C++, check this:

    // P.h file:
    
    #pragma once
    
    using namespace System;
    using namespace System::Drawing;
    using namespace System::Windows::Forms;
    using namespace System::Drawing::Drawing2D;
    using namespace System::Collections::Generic;
    
    
    namespace test
    {
    	namespace F = System::Windows::Forms;
    
    	public ref class P abstract : public Panel
    	{
    
    	protected:
    
    		virtual property F::CreateParams^ CreateParams
    		{
    			F::CreateParams^ get( ) override
    			{
    				F::CreateParams^ cp = __super::CreateParams;
    				cp->ExStyle |= 0x00000020; // TODO: use 'WS_EX_TRANSPARENT' from <Windows.h>
    
    				return cp;
    			}
    		}
    
    		virtual void OnPaintBackground( PaintEventArgs^ e ) override
    		{
    		}
    
    		virtual void OnPaint( PaintEventArgs^ e ) override
    		{
    			__super::OnPaint( e );
    
    			Graphics^ graphics = e->Graphics;
    
    			graphics->InterpolationMode = InterpolationMode::HighQualityBilinear;
    			graphics->PixelOffsetMode = PixelOffsetMode::HighQuality;
    			graphics->SmoothingMode = SmoothingMode::HighQuality;
    
    			OnDrawR( graphics );
    		}
    
    
    		virtual void OnDrawR( Graphics^ g ) abstract;
    
    	};
    
    
    
    	ref class SamplePanel : public P
    	{
    	protected:
    
    		virtual void OnDrawR( Graphics^ g ) override
    		{
    			g->DrawString( "Example", SystemFonts::DialogFont, Brushes::Red, 10, 12 );
    		}
    	};
    }
    
    

     

    But, the User Interface can be made easier in C#, I think.

    Friday, July 19, 2019 4:58 AM
  • Hi,

    Thank you for posting here.

    >>How to create CLR(C++/CLI) override onPaint 

    If you want to override the onPaint function which can be override often follow the name of "OnPaint(System::Windows::Forms::PaintEventArgs ^ e)"

    For more details you could refer to the link: https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.control.onpaint?view=netframework-4.8

    virtual void OnPaint( PaintEventArgs^ e ) override
    		{
                            // Repeat the native behavior of .NET
    			__super::OnPaint( e );
    
    
                            // Do whatever you want
    
    			Graphics^ graphics = e->Graphics;
    
    			graphics->InterpolationMode = System::Drawing::Drawing2D::InterpolationMode::HighQualityBilinear;
    			graphics->PixelOffsetMode = System::Drawing::Drawing2D::PixelOffsetMode::HighQuality;
    			graphics->SmoothingMode = System::Drawing::Drawing2D::SmoothingMode::HighQuality;
    
    			OnDrawR( graphics );
    		}
    
    
    

    Best Regards,

    Jeanine Zhang

    Friday, July 19, 2019 5:15 AM
    Moderator
  • Hi Viorel

    Thanks for answering.

    Yes C# UI is more convience than CLR

    But I want to try GDI to see C# and C++ which one is better for  image overlay.

    So I try c++ window desktop application but it doesn't have UI Form so I change to CLR.




    • Edited by CY_Chen Monday, July 29, 2019 8:35 AM
    Friday, July 19, 2019 5:42 AM