Page 1 of 1

wxDC::Blit

Posted: Fri Nov 24, 2017 7:39 pm
by palikem
Hi everybody

Where I make mistakes ?
The program stopped working

Code: Select all

void TestFrm::TestFrmMouseMotion(wxMouseEvent& event)
{
	int x, y;
	
	event.GetPosition(&x, &y);
	
	wxMemoryDC dd;
	wxClientDC dc(this);
	
	dd.SelectObject(bit_maska);
	
	dc.SetPen(*wxWHITE);
	
	if(x >100 && x < 140){
        if(!uzbol){
            uzbol = true;
            dd.Blit(0, 0, 40, 60, &dc, 140, 40);
            dc.DrawRoundedRectangle(100, 40, 140, 100, 1);
            dc.DrawText("N E W", 120, 40);
            dc.DrawText("O P E N", 120, 70);
            
        }
    }else{
        if(uzbol){
            dc.DrawBitmap(bit_maska, 100, 40, false);
            uzbol = false;
        }
    }
    
}

Re: wxDC::Blit

Posted: Fri Nov 24, 2017 8:28 pm
by ONEEYEMAN
Hi,
What do you mean by "stop working"?
What is the intention and what it's doing?

Also, what are you trying to paint here:

Code: Select all

dd.Blit( 0, 0, 40, 60,&dc, 140, 40 );
comparing to else condition where you are blitting bitmap?

Thank you.

Re: wxDC::Blit

Posted: Fri Nov 24, 2017 9:16 pm
by palikem
Program has stopped working and closes the
I'm trying to copy the screen content
then I'll draw something in that place
and finally I want it back to its original state

I do not know if I will speak correctly
something like masking

Re: wxDC::Blit

Posted: Fri Nov 24, 2017 9:29 pm
by ONEEYEMAN
HI,
Which OS/toolkit?
Which wx version?
Do you want something like screen shot?

Thank you.

Re: wxDC::Blit

Posted: Fri Nov 24, 2017 9:39 pm
by palikem
Windows 8.1
WxDev-C++ IDE 7.4.2.569

Not screenshot
only from the program window

Re: wxDC::Blit

Posted: Fri Nov 24, 2017 9:47 pm
by ONEEYEMAN
Hi,
OK.
What do you see on the screen and what do you want to see on the screen?
Moreover, blitting empty dc to another dc - you will have a crash. You need to get the information into the client dc and then blit it into memory. That's if I understand/read the code correctly.

Thank you.

Re: wxDC::Blit

Posted: Fri Nov 24, 2017 10:04 pm
by palikem
I need to copy the contents under File
1.jpg
I'll put it in bitmap
2.jpg
When I want to get it back to its original state
this will happen
3.jpg

Re: wxDC::Blit

Posted: Fri Nov 24, 2017 10:15 pm
by ONEEYEMAN
Hi,
Well, it is not surprising as "bit_maska" object is not initialized.

Can you post the complete code or this is the complete function?

Also, is this handler for the mouse move or for the wxEVT_PAINT? Painting should always be done in EVT_PAINT event.

Thank you.

Re: wxDC::Blit

Posted: Fri Nov 24, 2017 10:31 pm
by palikem
the bit_mask object is initialized

Code: Select all

#ifndef __TESTFRM_H__
#define __TESTFRM_H__

#ifdef __BORLANDC__
	#pragma hdrstop
#endif

#ifndef WX_PRECOMP
	#include <wx/wx.h>
	#include <wx/frame.h>
#else
	#include <wx/wxprec.h>
#endif

//Do not add custom headers between 
//Header Include Start and Header Include End.
//wxDev-C++ designer will remove them. Add custom headers after the block.
////Header Include Start
////Header Include End

////Dialog Style Start
#undef TestFrm_STYLE
#define TestFrm_STYLE wxCAPTION | wxSYSTEM_MENU | wxMINIMIZE_BOX | wxMAXIMIZE_BOX | wxCLOSE_BOX
////Dialog Style End

class TestFrm : public wxFrame
{
	private:
		DECLARE_EVENT_TABLE();
		
	public:
		TestFrm(wxWindow *parent, wxWindowID id = 1, const wxString &title = wxT("Test"), const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = TestFrm_STYLE);
		virtual ~TestFrm();
		void TestFrmPaint(wxPaintEvent& event);
		void TestFrmMouseMotion(wxMouseEvent& event);
		void TestFrmMouseEvents(wxMouseEvent& event);
		bool uzbol;
		wxBitmap bit_maska;
		
	private:
		//Do not add custom control declarations between
		//GUI Control Declaration Start and GUI Control Declaration End.
		//wxDev-C++ will remove them. Add custom code after the block.
		////GUI Control Declaration Start
		////GUI Control Declaration End
		
	private:
		//Note: if you receive any error with these enum IDs, then you need to
		//change your old form code that are based on the #define control IDs.
		//#defines may replace a numeric value for the enum names.
		//Try copy and pasting the below block in your old form header files.
		enum
		{
			////GUI Enum Control ID Start
			////GUI Enum Control ID End
			ID_DUMMY_VALUE_ //don't remove this value unless you have other enum values
		};
		
	private:
		void OnClose(wxCloseEvent& event);
		void CreateGUIControls();
};

#endif
Then I call the paint
When the drawing is done in paint

Re: wxDC::Blit

Posted: Fri Nov 24, 2017 10:42 pm
by ONEEYEMAN
Hi,
I don't see any initialization of that object. I do see however the header for the class declaration.

Can you post the complete code for that handler please?

Also the function you posted:

Code: Select all

void TestFrm::TestFrmMouseMotion(wxMouseEvent& event)
is for the mouse event. It is strongly advisable not to do any painting in that event. Call Refresh()/Update() and move your code in the EVT_PAINT handler.

Thank you.

Re: wxDC::Blit

Posted: Fri Nov 24, 2017 10:48 pm
by palikem

Code: Select all

#include "TestFrm.h"

//Do not add custom headers between
//Header Include Start and Header Include End
//wxDev-C++ designer will remove them
////Header Include Start
////Header Include End



//----------------------------------------------------------------------------
// TestFrm
//----------------------------------------------------------------------------
//Add Custom Events only in the appropriate block.
//Code added in other places will be removed by wxDev-C++
////Event Table Start
BEGIN_EVENT_TABLE(TestFrm,wxFrame)
	////Manual Code Start
	////Manual Code End
	
	EVT_CLOSE(TestFrm::OnClose)
	EVT_MOTION(TestFrm::TestFrmMouseMotion)
	EVT_PAINT(TestFrm::TestFrmPaint)
	EVT_MOUSE_EVENTS(TestFrm::TestFrmMouseEvents)
END_EVENT_TABLE()
////Event Table End

TestFrm::TestFrm(wxWindow *parent, wxWindowID id, const wxString &title, const wxPoint &position, const wxSize& size, long style)
: wxFrame(parent, id, title, position, size, style)
{
    wxInitAllImageHandlers();
    this->SetBackgroundColour(wxColour(247, 247, 247));
    bit_maska.SetWidth(40);
    bit_maska.SetHeight(60);
    
    
	CreateGUIControls();
}

TestFrm::~TestFrm()
{
}

void TestFrm::CreateGUIControls()
{
	//Do not add custom code between
	//GUI Items Creation Start and GUI Items Creation End
	//wxDev-C++ designer will remove them.
	//Add the custom code before or after the blocks
	////GUI Items Creation Start

	SetTitle(_("Test"));
	SetIcon(wxNullIcon);
	SetSize(8,8,1000,800);
	Center();
	
	////GUI Items Creation End
}

void TestFrm::OnClose(wxCloseEvent& event)
{
	Destroy();
}

/*
 * TestFrmPaint
 */
void TestFrm::TestFrmPaint(wxPaintEvent& event)
{
    int x, y;
    
    this->GetSize(&x, &y);
    
	wxPaintDC dc(this);
	
	wxBitmap bitmap;
	wxPen pen;
	wxBrush brush;
	
	dc.SetPen(*wxWHITE);
	dc.DrawRoundedRectangle(0, 0, x, 40, 1);
	
	dc.SetPen(*wxBLACK);
	dc.DrawText("F I L E", 120, 10);
	
	brush.SetColour(253,253,253);
	dc.SetBrush(brush);
	
	/*bitmap.LoadFile("tienL.png", wxBITMAP_TYPE_PNG);
	
	dc.DrawBitmap(bitmap, 40, 50, true);
	dc.DrawBitmap(bitmap, 40, 100, true);
	
	bitmap.LoadFile("tienD.png", wxBITMAP_TYPE_PNG);
	
	dc.DrawBitmap(bitmap, 40, 187, true);
	dc.DrawBitmap(bitmap, 160, 187, true);
	
	bitmap.LoadFile("tienP.png", wxBITMAP_TYPE_PNG);
	
	dc.DrawBitmap(bitmap, 347, 50, true);
	dc.DrawBitmap(bitmap, 347, 100, true);
	
	pen.SetColour(*wxBLACK);
	pen.SetWidth(1);
	dc.SetPen(pen);
	dc.DrawRoundedRectangle(50, 50, 300, 140, 5);
	dc.DrawText("1", 190, 110);
	
//*****************************************************************************

    bitmap.LoadFile("tienL.png", wxBITMAP_TYPE_PNG);
	
	dc.DrawBitmap(bitmap, 390, 50, true);
	dc.DrawBitmap(bitmap, 390, 100, true);
	
	bitmap.LoadFile("tienD.png", wxBITMAP_TYPE_PNG);
	
	dc.DrawBitmap(bitmap, 390, 187, true);
	dc.DrawBitmap(bitmap, 510, 187, true);
	
	bitmap.LoadFile("tienP.png", wxBITMAP_TYPE_PNG);
	
	dc.DrawBitmap(bitmap, 697, 50, true);
	dc.DrawBitmap(bitmap, 697, 100, true);
	
	pen.SetColour(*wxBLACK);
	pen.SetWidth(2);
	dc.SetPen(pen);
	dc.DrawRoundedRectangle(400, 50, 300, 140, 5);
	dc.DrawText("2", 550, 110);*/
	
//*****************************************************************************

    bitmap.LoadFile("tienL.png", wxBITMAP_TYPE_PNG);
	
	dc.DrawBitmap(bitmap, 42, 50, true);
	dc.DrawBitmap(bitmap, 42, 130, true);
	dc.DrawBitmap(bitmap, 42, 210, true);
	dc.DrawBitmap(bitmap, 42, 280, true);
	dc.DrawBitmap(bitmap, 42, 360, true);
	
	bitmap.LoadFile("tienD.png", wxBITMAP_TYPE_PNG);
	
	dc.DrawBitmap(bitmap, 42, 446, true);
	dc.DrawBitmap(bitmap, 220, 446, true);
	dc.DrawBitmap(bitmap, 400, 446, true);
	dc.DrawBitmap(bitmap, 580, 446, true);
	dc.DrawBitmap(bitmap, 715, 446, true);
	
	bitmap.LoadFile("tienP.png", wxBITMAP_TYPE_PNG);
	
	dc.DrawBitmap(bitmap, 902, 50, true);
	dc.DrawBitmap(bitmap, 902, 130, true);
	dc.DrawBitmap(bitmap, 902, 210, true);
	dc.DrawBitmap(bitmap, 902, 280, true);
	dc.DrawBitmap(bitmap, 902, 360, true);
	
	pen.SetColour(*wxBLACK);
	pen.SetWidth(1);
	dc.SetPen(pen);
	dc.DrawRoundedRectangle(50, 50, 857, 400, 5);
	//dc.DrawText("3", 190, 320);
	
//*****************************************************************************

    /*bitmap.LoadFile("tienL.png", wxBITMAP_TYPE_PNG);
	
	dc.DrawBitmap(bitmap, 392, 260, true);
	dc.DrawBitmap(bitmap, 392, 310, true);
	
	bitmap.LoadFile("tienD.png", wxBITMAP_TYPE_PNG);
	
	dc.DrawBitmap(bitmap, 390, 395, true);
	dc.DrawBitmap(bitmap, 510, 395, true);
	
	bitmap.LoadFile("tienP.png", wxBITMAP_TYPE_PNG);
	
	dc.DrawBitmap(bitmap, 695, 260, true);
	dc.DrawBitmap(bitmap, 695, 310, true);
	
	pen.SetColour(*wxBLACK);
	pen.SetWidth(2);
	dc.SetPen(pen);
	dc.DrawRoundedRectangle(400, 260, 300, 140, 5);
	dc.DrawText("4", 550, 320);*/
	
}

/*
 * TestFrmMouseMotion
 */
void TestFrm::TestFrmMouseMotion(wxMouseEvent& event)
{
	int x, y;
	
	event.GetPosition(&x, &y);
	
	wxMemoryDC dd;
	wxClientDC dc(this);
	
	dd.SelectObject(bit_maska);
	
	dc.SetPen(*wxWHITE);
	
	if(x >100 && x < 140){
        if(!uzbol){
            uzbol = true;
            dc.Blit(0, 0, 40, 60, &dd, 140, 40);
            dc.DrawRoundedRectangle(100, 40, 140, 100, 1);
            dc.DrawText("N E W", 120, 40);
            dc.DrawText("O P E N", 120, 70);
            
        }
    }else{
        if(uzbol){
            dc.DrawBitmap(bit_maska, 100, 40, false);
            uzbol = false;
        }
    }
    
}

/*
 * TestFrmMouseEvents
 */
void TestFrm::TestFrmMouseEvents(wxMouseEvent& event)
{
	
}


Re: wxDC::Blit

Posted: Fri Nov 24, 2017 11:00 pm
by ONEEYEMAN
Hi,
OK, but you didn't save anything into this bitmap.
You need to save some data in it and then you will be able to draw that back on the screen.

As it stands right now this bitmap is invalid.

Also as I said, painting code needs to be in the EVT_PAINT as the next painting message will overwrite what you are drawing.

Set the flag in the mouse handler and call Refresh()/Update(). And move the painting code to the EVT_PAINT handler.

Thank you.

Re: wxDC::Blit

Posted: Sat Nov 25, 2017 7:19 am
by palikem
why invalid bitmap

Save it command Blit(.....

Re: wxDC::Blit

Posted: Sat Nov 25, 2017 12:24 pm
by doublemax

Code: Select all

wxBitmap bit_maska;
This line doesn't create a valid wxBitmap.

Writing into a bitmap with Blit() doesn't create a wxBitmap either. You first need to create a wxBitmap with width, height and depth.