Skip to content

Instantly share code, notes, and snippets.

@vadz
Created April 6, 2024 23:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vadz/733e51bb3af649ddcf554d84f375a2fa to your computer and use it in GitHub Desktop.
Save vadz/733e51bb3af649ddcf554d84f375a2fa to your computer and use it in GitHub Desktop.
Example of a "transparent" wxTextCtrl under Windows
#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