0
我正在构建我的第一个使用类的C++项目(试图获得更多的经验),现在我被卡住了。我需要确定从我的计算器应用程序中按下哪个按钮。我有我的项目设立的方式是:如何从窗口函数外部访问wm_command
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;
}
Application.h
#pragma once
#include "Calculator.h"
class Application
{
public:
Application(HWND hwnd);
~Application();
void Go();
private:
void Run();
private:
Calculator calc;
};
Application.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()。
哪里是你的消息循环? – crashmstr
在我的wWinMain函数()中。我编辑了我的帖子。 – Vince
嗯,'Run'消息只被你的程序调用一次。当时任何按钮都不可能被按下。如果用户稍后按下按钮会怎么样?答案:窗口过程得到一个“WM_COMMAND”消息。所以在你的窗口程序中回应它。 –