Domanda

I have a solution and it has two projects in it. When I got the code they told one project handles the visual part and the other has the logic part. Now I added one button to the window. To do that i edited the project which handles the visual part. I am very new to this but creating and adding buttons is fairly straightforward in visual studio 2010. Now the problem is I want to detect if the button is pressed from the other project. I am sure that the projects are sharing some data, but I am not being able to capture it. For now I am changing a value in a file and reading the same data from the other project to check if the button is pressed. But I think there is a better way to do it. Can anyone help?

È stato utile?

Soluzione

I don't think the two projects are sharing automatically. You will have to define the interface that the two projects communicates. For instance, in your solution above the "a value in a file" is the "interface" you have defined. What sounds like you are trying to achieve is to separate the controller (logic part) and view (visual part) separately, which seems to indicate that your project is using MVC model.

I would suggest defining an abstract class (interface) that defines the interaction you want between the two projects. All they have to share is a single header file.

For example:

// Solution A (Controller - logic part)
// MyUIHandler.h
class IMyUIHandler //You can also use microsoft's interface keyword for something similar.
{
public:
    HRESULT onButtonPressed() = 0; // Note that you can also add parameters to onButtonPressed.
};

HRESULT getMyUIHandler(IMyUIHandler **ppHandler);

Then implement this interface:

// Solustion A (Controller - logic part)
// MyUIHandler.cpp
#include "MyUIHandler.h"
class CMyUIHandler : public IMyUIHandler
{
private:
   // Add your private parameter here for anything you need
public:
    HRESULT onButtonPressed();
}

HRESULT getMyUIHandler(IMyUIHandler **ppHandler)
{
    // There are many ways to handle it here:
    // 1. Define a singleton object CMyUIHandler in your project A. Just return a pointer 
    //    to that object here. This way the client never releases the memory for this 
    //    object.
    // 2. Create an instance of this object and have the client release it. The client 
    //    would be responsible for releasing the memory when it's done with the object. 
    // 3. Create an instance of this object and have a class/method in Solution A release
    //    the memory. 
    // 4. Reference count the object, for example, make IUnknown the parent class of 
    //    IMyUIHandler, and implement the IUnknown interace (which is boiler plate code).
    // Since I don't know your project it's hard for me to pick the most suitable one. 
    ...
    *ppHandler = myNewHandler;
    ...
    return S_OK;
}

CMyUIHandler can simply be your existing class that already handles some of the logic.

In solution B you should will call getMyUIHandler in some initialize function, for example the controller of the UI class, save that as your member. Then "Button clicked" event handler that VS creates for you.

// Solution B (View - visual part)
// MyUIView.h
class MyUIView
{
protected:
    IMyUIHandler *m_pHandler;
}

// MyUIView.cpp
CMyUIView::CMyUIView(...) 
{
    ...
    hr = getMyUIHandler(&m_pHandler);
    // error handler, etc...   
    ...
}

// In the function that is created for you when button is clicked (I'm not sure if I get the signature right below.
void OnClick(EventArgs^ e) 
{
    ...
    hr = m_pHandler->onButtonPressed();
    ...
}

Then you can pass any parameter you define for the function onButtonPressed as soon as the button is clicked.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top