CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 4 of 4

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

  1. #1
    Join Date
    Dec 2010

    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....

    protected override void OnPaint(PaintEventArgs e)
                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";
    			//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);
    					ButtonRenderer.DrawButton(e.Graphics, ClientRectangle, Text, Font, TextFormat, Image, ImageRectangle, (DisplayFocus & Focused), State);

  2. #2
    Join Date
    Feb 2017

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

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

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012

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

    [Thread moved to c++/cli]
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. 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 and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.7.3)

  4. #4
    Join Date
    Jun 2010

    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

On-Demand Webinars (sponsored)