用多个窗口作为一个类的实例的应用程序,c++
Application with multiple windows as instances of a class, C++
我写了一个应用程序。有一个名为APP的类,具有窗口的句柄,以及其中的消息循环,以及所有这些东西。
它旨在"运行"该类的一些对象,每个对象都有自己的基于一组标准窗口所需的变量的窗口。
消息循环允许公共使用,它是由runmessagelloop方法运行的。int nCmdShow——当然,它是用来告诉如何显示一个窗口的。
现在,当我创建一些像这样的对象:
vector <APP *> App;
for (int i=0; i<3; i++)
{
App.push_back(&APP(nCmdShow))
App[i]->RunMessageLoop();
}
程序在开始另一个消息循环之前等待每个消息循环结束。
我想这样做:
vector <APP *> App;
for (int i=0; i<3; i++)
{
App.push_back(&APP(nCmdShow))
}
for (int i=0; i<3; i++)
{
App[i]->RunMessageLoop();
}
当我知道启动时要运行多少个窗口时,似乎没问题。
但是我不知道如何动态地创建新的窗口,与其他窗口完全独立。它应该调用消息循环并立即返回WinMain(),而不结束消息循环。
我考虑过多线程应用,每个线程对应一个app类的实例。但是我不知道如何构建多线程应用。
有什么解决方案吗?
我知道你现在想做什么,我已经在我的应用程序框架Lucid中实现了这一点(它仍然是一个正在进行的工作)。为了得到答案,您的窗口类将被称为Window
而不是APP
。
这是通过向创建的每个窗口传递一个全局过程来完成的。所有的窗口共享这个相同的过程。每当任何窗口获得消息时,该消息被发送到全局过程,全局过程检查HWND
是否属于您创建的Window
,如果是,则将消息发送到该 Window
s的过程。下面是它是如何工作的概述。
class Window
{
public:
// The contents of this function can vary from window to window
// provided that you make a subclass and override this method.
virtual LRESULT procedure(HWND wnd, UINT msg, WPARAM wp, LPARAM lp);
// When you create a Window object, add a pointer to it in this map.
// Eg. if (this->hwnd != NULL) createdWindows[this->hwnd] = this;
static map<HWND, Window*> createdWindows;
// When you create a window, make this its procedure.
static LRESULT CALLBACK routeMessage(HWND wnd, UINT msg, WPARAM wp, LPARAM lp)
{
if (createdWindows.find(wnd) != createdWindows.end()) {
// Message belongs to one of our 'Window' objects.
// Pass the message to that window and return the result.
return createdWindows[wnd]->procedure(wnd, msg, wp, lp);
} else {
// It seems you made 'routeMessage' the procedure
// of a window that doesn't belong in the map. Go ahead
// and process the message in the default manner.
return DefWindowProc(wnd, msg, wp, lp);
}
}
};
现在您只需要一个消息循环和一个线程。我有一个使用Lucid的测试项目,它在一个带有单个消息循环的线程上创建了两个具有不同过程的窗口:
#include "Lucid.h"
using namespace Lucid;
void sayBye(MessageEventArgs& e)
{
MessageBox(NULL, "Goodbye!", "Form 2", MB_OK);
e.handled = true;
}
void Program::onStart()
{
Form* myForm1 = new Form("Hello World!");
myForm1->show();
Form* myForm2 = new Form("Hello World!");
myForm2->addMessageHandler(WM_CLOSE, sayBye);
myForm2->show();
// This Program::onStart() function is called
// immediately before the single message loop is entered.
}
创建_beginthreadex
等于您需要运行的窗口数的线程。然后,在线程过程中运行消息循环,直到使用WaitForMultipleObjects
终止所有线程。
- 从C++实例化QML
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- OpenGL - 在抛出"__gnu_cxx::recursive_init_error"实例后终止调用?
- 如何在c++中为模板函数实例创建快捷方式
- 在C++中,是否可以基于给定的标识符创建基类的新实例,反之亦然
- 设计一个只能由特定类实例化的类(如果可能的话,通过make_unique)
- 如何创建一个空的全局类并在启动时实例化它
- 无法创建抽象类的实例
- 多个文件的内存分配错误"在抛出 'std :: bad_alloc' what (): std :: bad_alloc 的实例后终止调用" [C++]
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 我收到以下错误:抛出'std::bad_alloc'实例后终止调用
- 建议在运行时将带有类实例的列表从c++导入qml
- 约束和显式模板实例化
- 通过实例理解std::move及其目的
- 为什么包含windows.h会产生语法错误,从而阻止类的实例化?(C2146,C2065)
- 有没有一种"cleaner"的方法可以在指向基的指针向量中找到派生类的第一个实例?
- 正在生成未知类实例
- 从DLL中删除类的实例
- 在std::vector上存储带有模板的类实例
- 为什么创建友元类的实例会导致"undefined reference to"错误?