私には解決策があり、2つのプロジェクトがあります。私がコードを入手したとき、彼らは一方のプロジェクトが視覚的な部分を処理し、もう一方が論理的な部分を持っていると言いました。次に、ウィンドウに1つのボタンを追加しました。そのために、ビジュアル部分を扱うプロジェクトを編集しました。私はこれに非常に慣れていませんが、Visual Studio 2010ではボタンの作成と追加はかなり簡単です。問題は、ボタンが他のプロジェクトから押されているかどうかを検出したいということです。プロジェクトがいくつかのデータを共有していることは確かですが、それをキャプチャすることはできません。今のところ、ファイルの値を変更し、他のプロジェクトから同じデータを読み取って、ボタンが押されているかどうかを確認しています。しかし、私はそれを行うためのより良い方法があると思います。誰か助けてもらえますか?
2713 次
1 に答える
2
2つのプロジェクトが自動的に共有されているとは思いません。2つのプロジェクトが通信するインターフェースを定義する必要があります。たとえば、上記のソリューションでは、「ファイル内の値」は、定義した「インターフェース」です。達成しようとしているように聞こえるのは、コントローラー(ロジック部分)とビュー(ビジュアル部分)を別々に分離することです。これは、プロジェクトがMVCモデルを使用していることを示しているようです。
2つのプロジェクト間で必要な相互作用を定義する抽象クラス(インターフェース)を定義することをお勧めします。彼らが共有しなければならないのは、単一のヘッダーファイルだけです。
例えば:
// 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に定義した任意のパラメーターを渡すことができます。
于 2012-07-02T06:42:28.217 に答える