0

in order to separate the User Interface class (a Windows Forms class) from the rest of the program, I am trying to call windows forms methods from int WINAPI WinMain ()

for instance, I am trying to do the following:

int WINAPI WinMain(HINSTANCE hInstance, 
    HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{   
    Application::EnableVisualStyles();
    UserInterface1 ^myInterface = gcnew UserInterface1();   

    String ^ picture1 = "img1";
    String ^ picture2 = "img2";

    Application::Run(myInterface); 

    Bitmap^ B = gcnew Bitmap (512,512);

    //user defined class that reads an image from a file
    PImage PI(picture1);

    //a Windows Forms method that updates the image displayed in a pictureBox element
    myInterface->ModifyDisplay (B, PI);

    //user defined class that reads an image from a file
    PImage PI2(picture2);

    //a Windows Forms method that updates the image displayed in a pictureBox element
    myInterface->ModifyDisplay (B, PI2);

    return 0;
}

Needless to say, it is not working the way it is now and I am not sure if what I am trying to do is feasible or not, as I am fairly new to .Net programming.

elrim
  • 55
  • 1
  • 2
  • 6
  • *How* is it not working? Are you getting an exception? Is there no exception, but the window isn't showing? Is it reformatting your computer each time you try to run it? – David Yaw Jul 27 '13 at 12:38
  • there is no exception, but the interface does not do anything. I sort of expected it because it looks like 'Application::Run(myInterface);' does not return until it is done doing what it has to do, which is nothing. The problem is that I am not sure how to make it accept inputs from the WinMain at runtime (if it is possible at all). – elrim Jul 27 '13 at 15:48
  • This is pretty fundamental, be sure to check out a book about Winforms programming from your local library. If you want other code to run, in addition to the UI, then you'll need to use a Thread. – Hans Passant Jul 29 '13 at 14:09

2 Answers2

1

it looks like 'Application::Run(myInterface);' does not return until it is done doing what it has to do, which is nothing

It's not nothing. Application::Run most importantly starts the Windows event message pump. This event loop is what keeps your application alive and running. Application::Run will return only when this loop exits.

The event loop generally exits when you close your main form or try to close the process from task manager etc.

Which means by the time Application::Run returns, your myInterface form is already closed - which makes rest of the code below it useless. You can move that code to something like Form's Load event.

Community
  • 1
  • 1
YK1
  • 7,327
  • 1
  • 21
  • 28
  • thanks for the explanation. I am definitely going to do more study on Windows Forms before going any further with my project. – elrim Jul 30 '13 at 09:02
0

Exactly, the codes that are after "Application::Run()" will be run when the form closed. I recommend you to create two forms for example "Form1" and "Form2" (using threads is another way but this one's easier).

Use "Form1" as the base form that is hidden and it is used for updating the application by getting image from file.

Use "Form2" as UserInterface1. For doing this you should run "Form2" inside "Form1" like this if you're new:

// Inside Form1.
#include "Form2.h"
Form2 ^openForm2 = gcnew Form2();
openForm2->Show();

And at last you can run the ModifyDisplay function in Form1.

openForm->ModifyDisplay();