problem with wxDC rectangle lines with width 2 and 4 Topic is solved

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
ioki9
Earned a small fee
Earned a small fee
Posts: 21
Joined: Tue Mar 09, 2021 5:25 pm

problem with wxDC rectangle lines with width 2 and 4

Post by ioki9 »

I am trying to make custom checkbox class, i am drawing a rectangle for it with wxGCDC, but if I set wxPen width to 2 or 4 it is being displayed weirdly. Can someone help me out with this please
width 4
width 4
4.png (1.14 KiB) Viewed 1447 times
width 3
width 3
3.png (1.14 KiB) Viewed 1447 times
width 2
width 2
Screenshot 2021-07-31 014652.png (1.35 KiB) Viewed 1447 times
here is the code of my paint event

Code: Select all

void CustomCheckBox::OnPaint(wxPaintEvent& evt)
{
	wxPaintDC dc(this);
	wxGCDC gcdc(dc);

	if (status & flag_active)
	{
		wxPoint yLeftMid{ 2, 7 };
		wxPoint xBotMid{ 5 , 11 };
		if(status & flag_motionIN)
			gcdc.SetPen(wxPen(m_checkBoxColour.ChangeLightness(60),4));
		else
			gcdc.SetPen(wxPen(m_checkBoxColour));

		if(status & flag_leftKeyDown)
			gcdc.SetBrush(wxBrush(m_checkBoxColour.ChangeLightness(130)));
		else
			gcdc.SetBrush(wxBrush(m_checkBoxColour));
		gcdc.DrawRectangle(m_rect);
		gcdc.SetPen(wxPen(*wxWHITE, 2));
		gcdc.DrawLine(yLeftMid, xBotMid);
		gcdc.DrawLine(xBotMid, wxPoint(12, 3));
	}
	else
	{
		if(status & flag_motionIN)
			gcdc.SetPen(wxPen(m_checkBoxColour, 4));
		else
			gcdc.SetPen(wxPen(*wxBLACK, 4));

		if (status & flag_leftKeyDown)
			gcdc.SetBrush(wxBrush(m_checkBoxColour.ChangeLightness(145)));
		gcdc.DrawRectangle(m_rect);
	}
}
sorry if I'm just being stupid, I'm kinda new to wxWidgets and C++ in general...
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: problem with wxDC rectangle lines with width 2 and 4

Post by doublemax »

Normal wxDC coordinates are integers, wxGraphicsContext/wxGCDC uses float coordinates with subpixel accuracy. If you pass integer coordinates to wxGCGC, an offset of 0.5 will be added to the coordinates which can lead to some unexpected results. You can disable the offset with wxGraphicsContext::DisableOffset()
https://docs.wxwidgets.org/trunk/classw ... 20aacd1f0e

In your case i'd just use wxDC to draw the rectangle.

Edit: I found a page that shows the difference between working with integer and float coordinates (In general, not wxWidgets specific).
https://wiki.freepascal.org/BGRABitmap_tutorial_13
Use the source, Luke!
ioki9
Earned a small fee
Earned a small fee
Posts: 21
Joined: Tue Mar 09, 2021 5:25 pm

Re: problem with wxDC rectangle lines with width 2 and 4

Post by ioki9 »

doublemax wrote: Fri Jul 30, 2021 11:09 pm Normal wxDC coordinates are integers, wxGraphicsContext/wxGCDC uses float coordinates with subpixel accuracy. If you pass integer coordinates to wxGCGC, an offset of 0.5 will be added to the coordinates which can lead to some unexpected results. You can disable the offset with wxGraphicsContext::DisableOffset()
https://docs.wxwidgets.org/trunk/classw ... 20aacd1f0e

In your case i'd just use wxDC to draw the rectangle.

Edit: I found a page that shows the difference between working with integer and float coordinates (In general, not wxWidgets specific).
https://wiki.freepascal.org/BGRABitmap_tutorial_13
thanks a lot, it works with wxGraphicsContext::DisableOffset(), but if I draw with wxPaintDC i get the exact same problem, does wxPaintDC also have an offset?
Post Reply