Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 具有多个窗口作为类C++;_C++_Visual Studio_Object_Instance - Fatal编程技术网

C++ 具有多个窗口作为类C++;

C++ 具有多个窗口作为类C++;,c++,visual-studio,object,instance,C++,Visual Studio,Object,Instance,我写了一份申请书。有一个名为APP的类,其中包含窗口句柄、消息循环以及所有这些内容 它旨在“运行”这个类的一些对象,每个对象都有自己的基于标准窗口所需的一组变量的窗口 消息循环允许公共使用,它由RunMessageLoop方法运行。 int nCmdShow-当然,它用于告诉如何显示窗口 现在,当我创建一些像这样的对象时: vector <APP *> App; for (int i=0; i<3; i++) { App.push_back(&APP(nCmdS

我写了一份申请书。有一个名为APP的类,其中包含窗口句柄、消息循环以及所有这些内容

它旨在“运行”这个类的一些对象,每个对象都有自己的基于标准窗口所需的一组变量的窗口

消息循环允许公共使用,它由RunMessageLoop方法运行。 int nCmdShow-当然,它用于告诉如何显示窗口

现在,当我创建一些像这样的对象时:

vector <APP *> App;
for (int i=0; i<3; i++)
{
    App.push_back(&APP(nCmdShow))
    App[i]->RunMessageLoop();
}
vector应用程序;
for(int i=0;iRunMessageLoop();
}
程序等待每个消息循环结束,然后再启动另一个消息循环

我想这样做:

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();
}
vector应用程序;

对于(int i=0;i创建线程,使
\u beginthreadex
等于需要运行的窗口数。然后,在线程过程中运行消息循环,并等待所有线程被
WaitForMultipleObjects

终止。我知道您现在正在尝试做什么,我已经在名为Lu的应用程序框架中实现了这一点cid(仍在进行中)。为了得到答案,您的窗口类将被称为
window
,而不是
APP

这是通过向您创建的每个窗口传递一个全局过程来完成的。所有窗口共享同一个过程。每次任何窗口收到消息时,该消息都会发送到全局过程,全局过程会检查
HWND
是否属于您创建的
窗口,如果是,则将消息发送到该
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);
        }
    }
};

您的代码具有未定义的行为。执行此操作时:
App.push_back(&App(nCmdShow))
您正在存储指向立即被销毁的临时对象的指针。在我看来,它似乎没有被销毁,因为我可以看到窗口,并且它正在响应消息。如果它的编码不正确,请提供一些建议,如果可以的话。@bercik“未定义的行为”可能看起来它正在工作,并且在以后的某个时间,当它的未定义性由于某种原因被暴露时,它会困扰你好几个星期。我很惊讶
&APP(nCmdShow)
甚至可以编译。你应该使用
new
,而不是
&
。我只知道指针用法的基础;我不熟练使用运算符new。它在代码中会是什么样子?当你不使用
new
创建对象时,你就在堆栈上创建它。因此,一旦创建
应用程序的函数返回<代码>应用程序
被销毁,从而导致指向存储在向量中的
应用程序
的指针无效。使用
应用程序。推回(新应用程序(nCmdShow))
应用程序
将存储在动态内存中,防止其被破坏,然后指针将是有效的。顺便说一句,无论您是否向其创建了无效指针,窗口仍将出现。感谢您的响应。我将尝试实现它并查看结果,然后给您反馈。谢谢。这也是非常有用的有趣。我会尝试一下,并发表我的想法。
#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.
}