Calling over from C++ to C# with C# objects?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Calling over from C++ to C# with C# objects?

  1. #1
    Join Date
    Dec 2010
    Posts
    92

    Calling over from C++ to C# with C# objects?

    How do I call this function from C++?

    I can either hook the DrawThemeBackground etc in the program and call over to onPaint method or directly go into DrawButton or DrawThemeButton.
    But what are "Graphics" and "PaintEventArgs" in C++ perspectives? C++ doesn't have such things in the SDK....

    Code:
    protected override void OnPaint(PaintEventArgs e)
    		{
    
                Application.EnableVisualStyles();
    
                this.components = new System.ComponentModel.Container();
                file = new DotNetActiveX.VisualStyleFile(this.components);
    
    
                BackColor = System.Drawing.Color.Empty;
                ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(0)), ((System.Byte)(0)), ((System.Byte)(0)));
                Image = (System.Drawing.Image)Image.FromFile("ball.png", false);
                Location = new System.Drawing.Point(120, 40);
                Name = "button1";
                Size = new System.Drawing.Size(96, 24);
                TabIndex = 3;
                Text = "button1";
                VisualStyle = file;
    
    
                file.ThemeFile = "C:\\WINDOWS\\Resources\\Themes\\Luna.theme";
    
    			base.OnPaint(e);
    
                
    			//if(!Disposing && !Parent.Disposing)
    			{
    				if(this.file != null && this.file.StyleFile!=String.Empty)
    				{
    					ButtonRenderer.DrawThemeButton(this.file, e.Graphics, ClientRectangle, Text, Font, TextFormat, Image, ImageRectangle, (DisplayFocus & Focused), State);
    				}
    				else
    			    {
    					ButtonRenderer.DrawButton(e.Graphics, ClientRectangle, Text, Font, TextFormat, Image, ImageRectangle, (DisplayFocus & Focused), State);
    				}
    			}
                
    		}

  2. #2
    Join Date
    Feb 2017
    Posts
    147

    Re: Calling over from C++ to C# with C# objects?

    You certainly must use C++/CLI for this to work.

    http://pragmateek.com/using-c-from-n...lp-of-ccli-v2/

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,711

    Re: Calling over from C++ to C# with C# objects?

    [Thread moved to c++/cli]
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++17 Compiler: Microsoft VS2017 (15.3.4)

  4. #4
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,665

    Re: Calling over from C++ to C# with C# objects?

    You can create a Graphics from a Win32 GDI object by calling its static FromHdc() or FromHwnd() method and in turn use that to construct the PaintEventArgs. However, to be able to use an OnPaint() override, in the first place you need an instance of your own class derived from System.Windows.Forms.Control (directly or indirectly), and that's something you usually don't have in a Win32 application.

    As wolle already suggested, C++/CLI is the standard tool for writing an interface layer beween native C++ Win32 code and managed C# code. Another option for interfacing is to expose a COM object from a managed C# DLL.
    Last edited by Eri523; September 11th, 2017 at 07:41 AM.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)