Creating a tiled menu
Re: Creating a tiled menu
I have a problem with text transparency.
I will be very grateful for your help.
Experiment 1
//-- Take a bitmap *.png image
//-- Print the yellow text to the bitmap
//-- Save to a debug file.
//---I see yellow text in the file
//-- Display the bitmap on the form with a red background
//---I see red text for some reason
Experiment 2
//-- Take a bitmap *.png image
//-- Display the bitmap on the form with a red background
//-- Print the yellow text to the bitmap
//---I see the yellow text (as it should be)
Simplified code to understand the sequence of actions:
//**********************************************************
Experiment 1
//-- Take a bitmap *.png image
wxImage img = wxImage("VFD.png", wxBITMAP_TYPE_PNG);
img.Rescale(141, 182);
*bm = new wxBitmap(img);
//-- Print the yellow text to the bitmap
wxMemoryDC mdc;
mdc.SelectObject(*bm);
mdc.SetTextForeground(*wxYELLOW); //цвет шрифта
mdc.SetFont(wxFont(18, wxFONTFAMILY_SWISS, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD));
mdc.DrawText("VFD", 10,10);
//-- Save to a debug file.
bm->SaveFile("ResultDraw.bmp", wxBITMAP_TYPE_BMP);
//---I see yellow text in the file
//-- Display the bitmap on the form with a red background
wxBitmap bitmap(1024, 768);
wxMemoryDC mdc2;
mdc2.SelectObject(*bm);
mdc2.SetBackground(*wxRED);
mdc2.Clear();
mdc2.DrawBitmap(*bm, 50, 50);
//---I see red text for some reason
//**********************************************************
Experiment 2
//-- Take a bitmap *.png image
wxImage img = wxImage("VFD.png", wxBITMAP_TYPE_PNG);
img.Rescale(141, 182);
*bm = new wxBitmap(img);
//-- Display the bitmap on the form with a red background
wxMemoryDC mdc;
wxBitmap bitmap(1024, 768);
wxMemoryDC mdc2;
mdc2.SelectObject(*bm);
mdc2.SetBackground(*wxRED);
mdc2.Clear();
mdc2.DrawBitmap(*bm, 50, 50);
//-- Print the yellow text to the bitmap
mdc.SetTextForeground(*wxYELLOW); //цвет шрифта
mdc.SetFont(wxFont(18, wxFONTFAMILY_SWISS, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD));
mdc.DrawText("VFD", 10,10);
//---I see the yellow text (as it should be)
//**********************************************************
I will be very grateful for your help.
Experiment 1
//-- Take a bitmap *.png image
//-- Print the yellow text to the bitmap
//-- Save to a debug file.
//---I see yellow text in the file
//-- Display the bitmap on the form with a red background
//---I see red text for some reason
Experiment 2
//-- Take a bitmap *.png image
//-- Display the bitmap on the form with a red background
//-- Print the yellow text to the bitmap
//---I see the yellow text (as it should be)
Simplified code to understand the sequence of actions:
//**********************************************************
Experiment 1
//-- Take a bitmap *.png image
wxImage img = wxImage("VFD.png", wxBITMAP_TYPE_PNG);
img.Rescale(141, 182);
*bm = new wxBitmap(img);
//-- Print the yellow text to the bitmap
wxMemoryDC mdc;
mdc.SelectObject(*bm);
mdc.SetTextForeground(*wxYELLOW); //цвет шрифта
mdc.SetFont(wxFont(18, wxFONTFAMILY_SWISS, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD));
mdc.DrawText("VFD", 10,10);
//-- Save to a debug file.
bm->SaveFile("ResultDraw.bmp", wxBITMAP_TYPE_BMP);
//---I see yellow text in the file
//-- Display the bitmap on the form with a red background
wxBitmap bitmap(1024, 768);
wxMemoryDC mdc2;
mdc2.SelectObject(*bm);
mdc2.SetBackground(*wxRED);
mdc2.Clear();
mdc2.DrawBitmap(*bm, 50, 50);
//---I see red text for some reason
//**********************************************************
Experiment 2
//-- Take a bitmap *.png image
wxImage img = wxImage("VFD.png", wxBITMAP_TYPE_PNG);
img.Rescale(141, 182);
*bm = new wxBitmap(img);
//-- Display the bitmap on the form with a red background
wxMemoryDC mdc;
wxBitmap bitmap(1024, 768);
wxMemoryDC mdc2;
mdc2.SelectObject(*bm);
mdc2.SetBackground(*wxRED);
mdc2.Clear();
mdc2.DrawBitmap(*bm, 50, 50);
//-- Print the yellow text to the bitmap
mdc.SetTextForeground(*wxYELLOW); //цвет шрифта
mdc.SetFont(wxFont(18, wxFONTFAMILY_SWISS, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD));
mdc.DrawText("VFD", 10,10);
//---I see the yellow text (as it should be)
//**********************************************************
Re: Creating a tiled menu
I would need compilable code including the loaded image file for testing.
BTW: The "Experiment 2" code makes no sense as it is, probably a bad case of copy-and-pasting it together
But one general remark:
When you select a bitmap into a wxMemoryDC, you need to unselect it, before using the bitmap for anything else, like saving it to a file, or drawing it into another wxDC.
https://docs.wxwidgets.org/trunk/classw ... y_d_c.html
BTW: The "Experiment 2" code makes no sense as it is, probably a bad case of copy-and-pasting it together
But one general remark:
When you select a bitmap into a wxMemoryDC, you need to unselect it, before using the bitmap for anything else, like saving it to a file, or drawing it into another wxDC.
https://docs.wxwidgets.org/trunk/classw ... y_d_c.html
Use the source, Luke!
Re: Creating a tiled menu
Why is the text transparent in the first experiment?
It turns out the alpha channel in the text that we write stands with a transparency of 100%
If we save in bmp, which does not support transparency, then we see yellow
If we display it in the window, we see 100% transparent text
How to properly prepare (insert text) into a bitmap?
It turns out the alpha channel in the text that we write stands with a transparency of 100%
If we save in bmp, which does not support transparency, then we see yellow
If we display it in the window, we see 100% transparent text
How to properly prepare (insert text) into a bitmap?
Re: Creating a tiled menu
The function of printing text on pictures.
Didn't think of it better
wxMemoryDC mdc;
wxSize sz;
wxBitmap * bm;
mdc.SetTextForeground(*ThemeColor.GetColor(Theme::Txt_Ctheme));
mdc.SetFont(wxFont(18, wxFONTFAMILY_SWISS, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD));
mdc.SetBackground(*ThemeColor.GetColor(Theme::BgAll_Ctheme));
for (int i = 0; i < 4; i++) {
bm = ThemeColor.GetPict(Num);
sz = bm->GetSize();
wxBitmap bitmap(sz.x, sz.y);
mdc.SelectObject(bitmap);
mdc.DrawBitmap(*bm, 0, 0);
DrawTxtAlign(&mdc, &OpisCoordTbl, Str);
*bm = bitmap;
}
Didn't think of it better
wxMemoryDC mdc;
wxSize sz;
wxBitmap * bm;
mdc.SetTextForeground(*ThemeColor.GetColor(Theme::Txt_Ctheme));
mdc.SetFont(wxFont(18, wxFONTFAMILY_SWISS, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD));
mdc.SetBackground(*ThemeColor.GetColor(Theme::BgAll_Ctheme));
for (int i = 0; i < 4; i++) {
bm = ThemeColor.GetPict(Num);
sz = bm->GetSize();
wxBitmap bitmap(sz.x, sz.y);
mdc.SelectObject(bitmap);
mdc.DrawBitmap(*bm, 0, 0);
DrawTxtAlign(&mdc, &OpisCoordTbl, Str);
*bm = bitmap;
}
Re: Creating a tiled menu
I made some tests myself. The problem is that the standard wxDC under Windows does not work properly in all cases when transparency is involved.
Then it switched to use wxGraphicsContext instead, but it turned out that with the default GDI+ renderer the font rendering onto transparent bitmaps is not 100% perfect either. So i switched to the Direct2D renderer which worked fine.
Here's the code i used for testing. It contains some hardcoded paths you'll have to adjust.
Then it switched to use wxGraphicsContext instead, but it turned out that with the default GDI+ renderer the font rendering onto transparent bitmaps is not 100% perfect either. So i switched to the Direct2D renderer which worked fine.
Here's the code i used for testing. It contains some hardcoded paths you'll have to adjust.
Code: Select all
// code to be pasted into the end of the MyFrame constructor of the "minimal" sample.
// wxBitmap m_composed_bmp; // member variable of MyFrame
wxPanel *panel = new wxPanel(this, wxID_ANY);
panel->SetBackgroundStyle(wxBG_STYLE_PAINT);
panel->Bind(wxEVT_PAINT, [=] (wxPaintEvent &evt) {
wxPaintDC dc(panel);
dc.SetBackground(wxColor("#dddddd"));
dc.Clear();
dc.DrawBitmap(m_composed_bmp, 20, 20, true);
});
::wxInitAllImageHandlers();
wxImage toucan("d:\\toucan.png", wxBITMAP_TYPE_PNG);
if( toucan.IsOk() ) {
m_composed_bmp = toucan;
wxMemoryDC mdc(m_composed_bmp);
#ifdef __WXMSW__
wxGraphicsContext *gc = wxGraphicsRenderer::GetDirect2DRenderer()->CreateContext(mdc);
#else
wxGraphicsContext *gc = wxGraphicsContext::Create(mdc);
#endif
gc->SetFont( wxFont(36, wxFONTFAMILY_SWISS, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD), *wxGREEN );
gc->DrawText("VFD", 10, 10);
delete gc;
mdc.SelectObject(wxNullBitmap);
m_composed_bmp.SaveFile("d:\\_bitmap_test.png", wxBITMAP_TYPE_PNG);
}
Use the source, Luke!
Re: Creating a tiled menu
I have a grayscale picture on a white background.
I need to recolor it red on a yellow background.
1. Is there a ready-made class for this?
2. If not, how to draw a pixel of a given color in wxMemoryDC
3. Or how to draw a buffer wxUint32 Bff[800][480] (each word in the buffer is a pixel)
I need to recolor it red on a yellow background.
1. Is there a ready-made class for this?
2. If not, how to draw a pixel of a given color in wxMemoryDC
3. Or how to draw a buffer wxUint32 Bff[800][480] (each word in the buffer is a pixel)
Re: Creating a tiled menu
The easiest way for direct pixel manipulation is to use wxImage. It uses an platform independent 24bit RGB buffer with optional 8bit alpha channel. So unfortunately you can't write 32bit RGBA data directly into it.
https://docs.wxwidgets.org/trunk/classw ... f16129e6c3
https://docs.wxwidgets.org/trunk/classw ... f16129e6c3
Use the source, Luke!
Re: Creating a tiled menu
Can you please tell me if there are ready-made tools for drawing rounded buttons with a color gradient in the wxWidget?
Now I have to upload images from Figma.
This creates some problems when resizing.
Now I have to upload images from Figma.
This creates some problems when resizing.
Re: Creating a tiled menu
No.
But i found some older code here, maybe you can use it as a base:
https://wxwidgets.info/howto_draw_gradient_buttons/
Also note that wxWidgets has a (simple) SVG renderer since 3.1.6, accessible through wxBitmapBundle
https://docs.wxwidgets.org/trunk/classw ... undle.html
Use the source, Luke!
Re: Creating a tiled menu
Pressing the button increments the displayed value.
With quick mouse clicks, skipping is visible (and this only appears under Windows).
Under Linux everything is fast.
For comparison, I opened the Windows calculator and saw that all clicks were processed in it.
If we put a render function with a runtime of about 5-10ms in the EVT_KEY_DOWN handler, then the delays become up to 200-300ms.
Can a widget lose EVT_KEY_DOWN events?
With quick mouse clicks, skipping is visible (and this only appears under Windows).
Under Linux everything is fast.
For comparison, I opened the Windows calculator and saw that all clicks were processed in it.
If we put a render function with a runtime of about 5-10ms in the EVT_KEY_DOWN handler, then the delays become up to 200-300ms.
Can a widget lose EVT_KEY_DOWN events?
- Attachments
-
- main.cpp
- (3.84 KiB) Downloaded 33 times
- doublemax@work
- Super wx Problem Solver
- Posts: 474
- Joined: Wed Jul 29, 2020 6:06 pm
- Location: NRW, Germany
Re: Creating a tiled menu
Maybe i can't click fast enough, but i don't see anything wrong under Windows.
Re: Creating a tiled menu
@OP,
Are you testing Debug build?
Thank you.
Are you testing Debug build?
Thank you.
Re: Creating a tiled menu
Yes. Debug.
In the Windows settings, I increased the speed of double-clicking. The gaps have decreased, but not disappeared.
Maybe they are related to the fact that the system catches both single and double clicks and then passes single clicks to the application. And if you click too fast, then some clicks are perceived as double. even though they are not used.
In the Windows settings, I increased the speed of double-clicking. The gaps have decreased, but not disappeared.
Maybe they are related to the fact that the system catches both single and double clicks and then passes single clicks to the application. And if you click too fast, then some clicks are perceived as double. even though they are not used.
Re: Creating a tiled menu
In that case catching wxEVT_LEFT_DCLICK and binding it to the wxEVT_LEFT_DOWN handler should do the trick.Andrey wrote: ↑Tue May 31, 2022 4:28 pm Yes. Debug.
In the Windows settings, I increased the speed of double-clicking. The gaps have decreased, but not disappeared.
Maybe they are related to the fact that the system catches both single and double clicks and then passes single clicks to the application. And if you click too fast, then some clicks are perceived as double. even though they are not used.
Use the source, Luke!
Re: Creating a tiled menu
Hi,
Try it on the Release build.
NS uses different CRT for Debug and Release and this might play a significant role here.
Thank you.
Try it on the Release build.
NS uses different CRT for Debug and Release and this might play a significant role here.
Thank you.