0

我正在使用类构建我的第一个 c++ 项目(试图获得更多经验),现在我被卡住了。我需要确定从我的计算器应用程序中按下了哪个按钮。我设置项目的方式是:

Windows.cpp

// Windows.cpp
#include <Windows.h>
#include <wchar.h>
#include "Resource.h"
#include "Application.h"

int WINAPI wWinMain(...)
{
    // after register class and create/show/update window ( winMain() )
    Application App(hwnd);
    App.Go();

    // Main message loop, etc.
    MSG msg;
    ZeroMemory(&msg,sizeof(msg));
    while(msg.message != WM_QUIT)
    {
      if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
      {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
      }
    }
    return 0;
}

应用程序.h

 #pragma once
 #include "Calculator.h"

class Application
{
public:
    Application(HWND hwnd);
    ~Application();
    void Go();

private:
    void Run();

private:
    Calculator calc;
};

应用程序.cpp:

// Application.cpp

#include "Application.h"

Application::Application(HWND hwnd)
: calc(hwnd)
{}
Application::~Application()
{}
void Application::Go()
{
    calc.Initiate(); // This function shows all my button controls for my calculator
    Run();
}

void Application::Run()
{
    // This is where i want to determine which button was pressed(if any)
    if(buttonONEwasPRESSED) { /* do stuff */ } // etc
}

我考虑向 Calculator 类添加一个函数来确定是否按下了按钮,但我不确定如何访问 wm_command,或者是否有其他方式。然后我可以调用 calc.IsButtonPressed()。

4

1 回答 1

0

你被卡住了,因为你想知道哪个按钮被按下了。这让我想起了一些处理用户输入的控制台程序。

这不是使用 GUI 的方式。您应该做的是编写按下按钮时要做什么的代码。那就是event drived programming

对于标准 Win32 应用程序,按钮按下的“事件”是WM_COMMAND.

对于通过 WM_MESSAGE_X 和 OnMessageX 成员函数之间的简单映射将 HWND 映射到 C++ 类,请参见例如https://stackoverflow.com/a/20356046/1374704

于 2014-02-22T13:45:00.033 回答