Winforms 更改按钮背景色,C++;VisualStudio2010

Winforms 更改按钮背景色,C++;VisualStudio2010,winforms,visual-studio-2010,c++-cli,Winforms,Visual Studio 2010,C++ Cli,我的第一篇文章。 我正在试用Windows窗体和C++。 我有一些(基本的)问题。到目前为止,我的目标是编写一个程序,检查某个硬件是否通过USB端口插入,但在开始实际工作之前,我想完成GUI,该GUI将通知是否找到了硬件。 因此,我有一个标准表单(Form1.h),然后我有一个主方法所在的cpp类(usbStatus.cpp)和另一个cpp文件(connection.cpp),我计划在其中放置代码以搜索所需的硬件。在启动时,表单显示一个背景色为红色的状态按钮集和一个状态标签,上面写着“搜索硬件”

我的第一篇文章。 我正在试用Windows窗体和C++。 我有一些(基本的)问题。到目前为止,我的目标是编写一个程序,检查某个硬件是否通过USB端口插入,但在开始实际工作之前,我想完成GUI,该GUI将通知是否找到了硬件。
因此,我有一个标准表单(Form1.h),然后我有一个主方法所在的cpp类(usbStatus.cpp)和另一个cpp文件(connection.cpp),我计划在其中放置代码以搜索所需的硬件。在启动时,表单显示一个背景色为红色的状态按钮集和一个状态标签,上面写着“搜索硬件”。然后,我希望当其他代码(尚未编写)完成硬件搜索时,按钮背景颜色变为绿色(如果找到硬件),标签变为“找到硬件”。好。。。什么也没发生。我已经检查了跟踪,代码已处理,但没有可见的结果。我尝试了Invalidate()和Refresh(),但没有成功

我现在仅有的一点点看起来很像这样:

表格1.h

namespace usbStatus{

    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;

    /// <summary>
    /// Summary for Form1
    /// </summary>
    public ref class Form1 : public System::Windows::Forms::Form
    {
    public:
        Form1(void)
        {
            InitializeComponent();
            //
            //TODO: Add the constructor code here
            //

        }
    ...

    void InitializeComponent(void)
        {

    ...
    this->statusButton = (gcnew System::Windows::Forms::ToolStripButton());
    this->statusLabel = (gcnew System::Windows::Forms::ToolStripLabel());
    ....

    void InitializeComponent(void)
    {
    ...
    }
usbStatus.cpp

#include "stdafx.h"
#include "Form1.h"
#include "Connection.h"

using namespace UsbStatus;


[STAThreadAttribute]
int main(array<System::String ^> ^args)
{

    // Enabling Windows XP visual effects before any controls are created
    Application::EnableVisualStyles();
    Application::SetCompatibleTextRenderingDefault(false); 

    // Create the main window and run it
    //Application::Run(gcnew Form1());
    Form1 ^mainWindow = gcnew Form1();
    Application::Run(mainWindow);

if(Connection::GetStatus()) 
    mainWindow->UpdateStatusElements(System::Drawing::Color::Green);
else
    //mainWindow->UpdateStatusElements(System::Drawing::Color::Red);
delete mainWindow;
return 0;
}
连接.cpp

#include <windows.h>
#include "Connection.h"

using namespace UsbStatus;

...

bool Connection::GetStatus()
{
    CheckStatus();
    return true;
    //return hwConnected;
}
#包括
#包括“Connection.h”
使用名称空间UsbStatus;
...
bool连接::GetStatus()
{
检查状态();
返回true;
//返回连接;
}

调用
Application::Run(主窗口)是一个阻塞调用。这意味着在对话框终止之前它不会返回。它将在内部运行。所以后续调用将在Dialog关闭后执行。。。我认为这不是故意的

您需要将支票转入Form1类。使用(或覆盖此或您),以便定期检查状态并更新您的UI。您可以通过将计时器从UI元素拖到表单中,在对话框编辑器中添加计时器


另请参见:

Hi Jochen,感谢您抽出时间。您说得对,我在Run命令之前移动了if/else(只是为了测试您的建议)我有一种感觉,我不应该在表单类中添加任何逻辑,而不仅仅是更新GUI所需的真正必要的逻辑,是没有必要这样想,还是我应该坚持这样做?更好的解决方案是通过接口分离逻辑!然后你可以在你自己的类和ju中实现逻辑st将接口传递到表单中。
#pragma once
#include "Form1.h"



ref class Connection
{
    private:
        static void CheckStatus();
        static void SetStatus(bool connected);
        static bool hwConnected;

    public:
        static bool GetStatus();

};
#include <windows.h>
#include "Connection.h"

using namespace UsbStatus;

...

bool Connection::GetStatus()
{
    CheckStatus();
    return true;
    //return hwConnected;
}