Created
April 6, 2024 23:22
-
-
Save vadz/733e51bb3af649ddcf554d84f375a2fa to your computer and use it in GitHub Desktop.
Example of a "transparent" wxTextCtrl under Windows
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <wx/app.h> | |
#include <wx/artprov.h> | |
#include <wx/custombgwin.h> | |
#include <wx/dcclient.h> | |
#include <wx/frame.h> | |
#include <wx/sizer.h> | |
#include <wx/textctrl.h> | |
#include <wx/msw/private.h> | |
struct CustomBackgroundFrame : public wxCustomBackgroundWindow<wxFrame> { | |
CustomBackgroundFrame() { | |
Create(nullptr, wxID_ANY, "Custom Background Frame"); | |
SetBackgroundBitmap(wxArtProvider::GetBitmap(wxART_QUESTION)); | |
} | |
// This shouldn't be necessary in the future, but for now override it to | |
// fix a bug with brush positioning in the current wxWidgets version. | |
WXHBRUSH MSWGetBgBrushForChild(WXHDC hDC, wxWindowMSW *child) override | |
{ | |
WXHBRUSH hbrush = MSWGetCustomBgBrush(); | |
if ( !hbrush ) | |
return 0; | |
RECT rc; | |
::GetWindowRect(GetHwnd(), &rc); | |
wxMapWindowPoints(GetHwnd(), GetHwndOf(child), &rc); | |
::SetBrushOrgEx((HDC)hDC, rc.left, rc.top, nullptr); | |
return hbrush; | |
} | |
}; | |
struct TransparentTextCtrl : public wxTextCtrl { | |
explicit TransparentTextCtrl(wxWindow* parent) : | |
wxTextCtrl(parent, wxID_ANY, {}, {}, parent->FromDIP(wxSize(300, -1)), wxBORDER_NONE) { | |
SetFont(wxFontInfo(20).Bold()); | |
SetForegroundColour(*wxYELLOW); | |
Bind(wxEVT_SIZE, [this](wxSizeEvent& event) { | |
auto size = event.GetSize(); | |
SetValue(wxString::Format("Size: %d x %d", size.x, size.y)); | |
Refresh(); | |
event.Skip(); | |
}); | |
} | |
bool HasTransparentBackground() override { return true; } | |
}; | |
struct TestApp : public wxApp { | |
bool OnInit() override { | |
auto f = new CustomBackgroundFrame(); | |
auto s = new wxBoxSizer(wxVERTICAL); | |
s->AddStretchSpacer(); | |
s->Add(new TransparentTextCtrl(f), wxSizerFlags().Center()); | |
s->AddStretchSpacer(); | |
f->SetSizer(s); | |
f->SetClientSize(f->FromDIP(wxSize(500, 300))); | |
f->Show(); | |
return true; | |
} | |
}; | |
wxIMPLEMENT_APP(TestApp); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment