Trying to pass a form by reference to a function

Hi.
I am trying to pass a Form to a function that will change the forms Appearance.


code:

void ChangeBackground(Form sld,String ^pict)
{
sld.BackgroundImage = Image::FromFile(pict);
sld.BackgroundImageLayout = System::Windows::Forms::ImageLayout::Stretch;
}

int main()
{
Project2::MyForm slide1;

System::String^ test;

test = "C:\\Users\\IT\\Pictures\\Tiger.jpg";

ChangeBackground(slide1,test);

Application::Run(% slide1);
}

How do i pass slide1 to Function ChangeBackground() ?


Last edited on
This looks like that Managed C+, C++/CLI thing.

In C++, you'd use a reference:

1
2
3
4
5
void ChangeBackground(Form& sld,String pict)
{
sld.BackgroundImage = Image::FromFile(pict);
sld.BackgroundImageLayout = System::Windows::Forms::ImageLayout::Stretch;
}


Last edited on
How do i pass slide1 to Function ChangeBackground() ?

The same way you're passing the (System::)String -- via a handle (^), which is more or less the managed equivalent to a pointer.

1
2
3
void ChangeBackground(Form^ sld, String^ pict)
{
    // Etc 


Regards

Andy

Handle to Object Operator (^) (C++/CLI and C++/CX)
https://docs.microsoft.com/en-us/cpp/extensions/handle-to-object-operator-hat-cpp-component-extensions

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <windows.h>

#using <System.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>

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

public ref class ExampleForm : public Form
{
public:
    ExampleForm()
    {
        InitializeComponent();
    }

    void InitializeComponent()
    {
        // TODO
    }
};

void ChangeBackground(Form^ slide, String^ pict)
{
    slide->BackgroundImage = Image::FromFile(pict);
    slide->BackgroundImageLayout = ImageLayout::Stretch;
}

int APIENTRY
WinMain(HINSTANCE /*hInstance*/, HINSTANCE /*hPrevInstance*/,  LPSTR /*lpCmdLine*/, int /*nCmdShow*/)
{
    String^ imgPath = "C:\\Users\\IT\\Pictures\\Tiger.jpg";

    ExampleForm^ mainForm = gcnew ExampleForm();

    ChangeBackground(mainForm, imgPath);

    Application::Run(mainForm);

    return 0;
}

Last edited on
Topic archived. No new replies allowed.