c++ - 在 Visual Studio C++ 中从一个项目访问变量到另一个项目

标签 c++ visual-studio share solution projects

我有一个解决方案,其中有两个项目。当我拿到代码时,他们告诉我一个项目处理视觉部分,另一个项目处理逻辑部分。现在我在窗口中添加了一个按钮。为此,我编辑了处理视觉部分的项目。我对此很陌生,但在 visual studio 2010 中创建和添加按钮非常简单。现在的问题是我想检测是否从其他项目按下了按钮。我确信这些项目正在共享一些数据,但我无法捕获它。现在我正在更改文件中的值并从另一个项目读取相同的数据以检查按钮是否被按下。但我认为有更好的方法来做到这一点。谁能帮忙?

最佳答案

我认为这两个项目不会自动共享。您将必须定义两个项目通信的接口(interface)。例如,在您上面的解决方案中,“文件中的值”是您定义的“接口(interface)”。听起来你试图实现的是将 Controller (逻辑部分)和 View (可视部分)分开,这似乎表明你的项目正在使用 MVC 模型。

我建议定义一个抽象类(接口(interface))来定义您希望在两个项目之间进行的交互。他们所需要共享的只是一个头文件。

例如:

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

然后实现这个接口(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 可以只是您现有的已经处理了一些逻辑的类。

在解决方案 B 中,您应该在某些初始化函数中调用 getMyUIHandler,例如 UI 类的 Controller ,将其保存为您的成员。然后是 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 定义的任何参数。

关于c++ - 在 Visual Studio C++ 中从一个项目访问变量到另一个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11286821/

相关文章:

c++ - 服务器/客户端聊天

c++ - 将 EXPECT_NE、EXPECT_EQ 包装到验证函数中

c# - 添加引用对话框中的程序集是否因使用的 .Net 框架而异?

c++ - MSYS2 MinGW64 在 Windows 上构建 GMP/MPFR 作为静态库,并将它们链接到用 CL 编译的 MSVC 项目中

c# - 如何访问网络中的 C$ 份额?

c++ - Visual Studio 2013 C++ 概述/折叠 if/else/while 代码区域

c++ - 从逗号分隔的文本文件中创建有意义数据 vector 的最佳方法是什么

javascript - 删除 Visual Studio Web Developer Express 2012 中的注释代码

twitter - 在 Twitter 上共享带有查询字符串的 URL

android - 如果通过 ACTION_SEND 文件附件失败,则仅共享文本