2012-07-02 2 views
2

나는 솔루션을 가지고 있으며 두 개의 프로젝트가 있습니다. 코드를 얻었을 때 한 프로젝트에서 시각적 부분을 처리하고 다른 프로젝트는 논리 부분을 처리한다고 말했습니다. 이제 창에 버튼 하나를 추가했습니다. 그렇게하기 위해 시각적 부분을 처리하는 프로젝트를 편집했습니다. 저는이 작업에 익숙하지 않지만 버튼을 만들고 추가하는 작업은 Visual Studio 2010에서 매우 간단합니다. 이제 문제는 다른 프로젝트에서 버튼을 눌렀는지 여부를 감지하고자하는 것입니다. 프로젝트에서 일부 데이터를 공유하고 있지만 데이터를 캡처 할 수는 없습니다. 지금은 파일의 값을 변경하고 다른 프로젝트에서 동일한 데이터를 읽어 버튼이 눌러져 있는지 확인하고 있습니다. 그러나 나는 그것을 할 수있는 더 좋은 방법이 있다고 생각합니다. 누구든지 도와 줄 수 있습니까?Visual Studio에서 하나의 프로젝트에서 다른 프로젝트로 변수에 액세스하기 C++

답변

1

저는 두 프로젝트가 자동으로 공유하고 있다고 생각하지 않습니다. 두 프로젝트가 통신하는 인터페이스를 정의해야합니다. 예를 들어, 위의 솔루션에서 "파일의 값"은 정의한 "인터페이스"입니다. 당신이 달성하려고하는 소리는 컨트롤러 (로직 부분)와 뷰 (시각적 부분)를 따로 따로 분리하는 것인데, 이것은 프로젝트가 MVC 모델을 사용하고있는 것으로 보인다.

두 프로젝트간에 원하는 상호 작용을 정의하는 추상 클래스 (인터페이스)를 정의하는 것이 좋습니다. 공유해야하는 것은 하나의 헤더 파일입니다. 예를 들어

:

// 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); 

그런 다음,이 인터페이스를 구현

:

// 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 단순히 이미 로직의 일부를 처리하는 기존의 클래스가 될 수 있습니다.

솔루션 B에서 일부 초기화 함수 (예 : UI 클래스의 컨트롤러)에서 getMyUIHandler를 호출하여 멤버로 저장해야합니다. 그런 다음 VS에서 생성 한 "버튼 클릭"이벤트 처리기.

// 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(); 
    ... 
} 

그러면 단추를 클릭하자마자 onButtonPressed 함수에 정의한 매개 변수를 전달할 수 있습니다.

+0

멋진 답변을 주셔서 대단히 감사합니다. – user1494936

관련 문제