Drawing a window to another window

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
beneficii
Earned some good credits
Earned some good credits
Posts: 111
Joined: Fri Nov 27, 2009 2:49 am

Drawing a window to another window

Post by beneficii »

I'm trying to draw a frame on another frame, to make it like those visual IDE's where you have the frame in front of you and you can edit it. But I'm not sure how to do it.

I tried this, but all I got was a blank window:

Code: Select all

class Frame2 : public wxFrame {
public:
	Frame2() : wxFrame(nullptr, wxID_ANY, "Drawer2") {}

};

MyFrame::MyFrame()
	: wxFrame(nullptr, wxID_ANY, "Drawer")
{
	Frame2 *frame = new Frame2;
	frame->Show(true);
	wxWindowDC dc(frame);
	wxClientDC mydc(this);
	mydc.Blit(wxPoint(0, 0), wxSize(300, 300), &dc, wxPoint(0, 0));
	frame->Destroy();
}
What can I do to draw a frame as graphics onto another frame?
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4193
Joined: Sun Jan 03, 2010 5:45 pm

Re: Drawing a window to another window

Post by PB »

For starters, drawing anything in the constructor is not a good idea. All drawing needs to be done in response to the paint event.

Have you tried to draw that other frame to a bitmap and then blit the saved bitmap during painting the main frame?
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4193
Joined: Sun Jan 03, 2010 5:45 pm

Re: Drawing a window to another window

Post by PB »

Just out of curiosity, I have tried to draw "live" another frame inside a frame. I could not get it done, the window decorations are not being drawn properly. I may/must have made a mistake somewhere in the code, assuming Windows 10 is not to "blame".

Code: Select all

#include <wx/wx.h>

class OtherFrame : public wxFrame
{
public:	
	OtherFrame() : wxFrame(NULL, wxID_ANY, "Other Frame", wxDefaultPosition, wxSize(300,300))
	{
		wxTextCtrl* text =  new wxTextCtrl(this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE);
		text->SetValue("Hello world");
	}
};

class MainFrame : public wxFrame
{
public:
    MainFrame() : wxFrame(NULL, wxID_ANY, "Test", wxDefaultPosition, wxSize(600,600), wxDEFAULT_FRAME_STYLE | wxFULL_REPAINT_ON_RESIZE)
    {
		m_otherFrame = new OtherFrame();
		m_otherFrame->Show();
		m_otherFrame->Bind(wxEVT_DESTROY, &MainFrame::OnOtherFrameDestroyed, this);

		m_timer.SetOwner(this);
		m_timer.Start(1000);
		Bind(wxEVT_TIMER, &MainFrame::OnTimer, this);
		Bind(wxEVT_PAINT, &MainFrame::OnPaint, this);                    
    }	
private:
	wxTimer     m_timer;
	OtherFrame* m_otherFrame;

	void OnPaint(wxPaintEvent&) 
	{ 
		wxPaintDC dc(this);
				
		dc.Clear();

		if ( m_otherFrame && m_otherFrame->IsShown() )
		{
			wxWindowDC wdc(m_otherFrame);
		
			dc.Blit(wxPoint(0,0), wdc.GetSize(), &wdc, wxPoint(0,0));
			dc.SetPen(*wxRED_PEN);
			dc.SetBrush(*wxTRANSPARENT_BRUSH);
			dc.DrawRectangle(wxPoint(0,0), wdc.GetSize());		
		}
	}

	void OnTimer(wxTimerEvent&)
	{               
		Refresh();
		Update();
	}

	void OnOtherFrameDestroyed(wxWindowDestroyEvent&)
	{
		m_otherFrame = NULL;
		Refresh();
		Update();		
	}
};

class MyApp : public wxApp
{
public:
    virtual bool OnInit()
    {     
        (new MainFrame())->Show();       
        return true;
    }
}; wxIMPLEMENT_APP(MyApp);
Edit: FWIW, I have tried wxFormBuilder and CodeLite's wxCrafter (on Windows 10): Neither draws native window decorations (frame title etc.). wxRendererNative does not provide methods to draw (all) window decorations either.
Post Reply