You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

wxWidgets中wxStaticBitmap背景下按钮无法正常加载问题求助

问题:wxStaticBitmap作为背景时按钮初始不显示,仅鼠标悬停后可见

我用C++开发基于wxWidgets的GUI程序,遇到一个问题:当使用wxStaticBitmap作为背景时,按钮无法正常加载。我调整过代码顺序以确保按钮位于背景图像上方,但程序启动时仅显示背景位图和wxStaticText,只有将鼠标悬停在按钮上时,按钮才会显示,运行.exe文件时该现象仍存在。我尝试过调用Raise()和Update()方法,但均无效。按钮在可见后的功能一切正常,只是无法在框架加载时立即显示。

以下是MainFrame的代码:

#define _CRT_SECURE_NO_WARNINGS // 消除阻止编译的警告
#include "MainFrame.h"
#include <wx/wx.h>
#include "TopicQuestionsFrame.h"
#include "FlashcardsFrame.h"
#include "PopQuizFrame.h"
#include "LeaderboardFrame.h"
#include "CustomisationFrame.h"
#include "ClassroomFrame.h"
#include "FriendsFrame.h"
#include <windows.h>
#include "FrameUtilities.h"

MainFrame::MainFrame(const wxString& title) : wxFrame(nullptr, wxID_ANY, title) {

    wxPanel* panel = new wxPanel(this);     // 创建承载控件的面板

    this->SetClientSize(800, 800);
    this->SetMinSize(GetSize());
    this->SetMaxSize(GetSize());

    // 图像处理

    wxImage::AddHandler(new wxPNGHandler());

    wxBitmap image_bitmap("E:/Coding/Pro 7/Images/backgroundfinal.png", wxBITMAP_TYPE_PNG);
                                                                                                // 指定图像文件路径
    wxBitmap friends_button_bitmap("E:/Coding/Pro 7/Images/friendstab.png", wxBITMAP_TYPE_PNG);

    wxBitmap classroom_button_bitmap("E:/Coding/Pro 7/Images/classroomtab.png", wxBITMAP_TYPE_PNG);

    this->SetBackgroundColour("#c2cbc6"); // 设置窗口背景色为指定十六进制颜色

    // 背景和按钮图像

    wxStaticBitmap* background_gui = new wxStaticBitmap(this, wxID_ANY, image_bitmap, wxPoint(0, 0));

    wxBitmapButton* friends_tab = new wxBitmapButton(panel, wxID_ANY, friends_button_bitmap, wxPoint(660, 10), wxSize(102, 92));

    wxBitmapButton* classroom = new wxBitmapButton(panel, wxID_ANY, classroom_button_bitmap, wxPoint(60, 10), wxSize(102, 92));

    // 字体

    wxFont streak_font(36, wxFONTFAMILY_SWISS, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD);

    // 按钮

    wxButton* topic_questions = new wxButton(panel, wxID_ANY, "Topic Questions", wxPoint(300, 125), wxSize(245, 85));

    wxButton* flashcards = new wxButton(panel, wxID_ANY, "Flashcards", wxPoint(300, 245), wxSize(245, 85));

    wxButton* pop_quiz = new wxButton(panel, wxID_ANY, "Pop Quiz", wxPoint(300, 365), wxSize(245, 85));

    wxButton* customisation = new wxButton(panel, wxID_ANY, "Customisation", wxPoint(300, 485), wxSize(245, 85));

    wxButton* leaderboard = new wxButton(panel, wxID_ANY, "Leaderboard", wxPoint(300, 605), wxSize(245, 85));
    
    // 事件绑定

    topic_questions->Bind(wxEVT_BUTTON, [this](wxCommandEvent& evt) {
        FrameUtilities::CreateFrame(this, evt, 1);  // 调用CreateFrame,传入当前框架、事件和唯一标识ID 1
        });                                         // FrameUtilities:: 指定函数所在文件

    flashcards->Bind(wxEVT_BUTTON, [this](wxCommandEvent& evt) {
        FrameUtilities::CreateFrame(this, evt, 2);      
        });

    pop_quiz->Bind(wxEVT_BUTTON, [this](wxCommandEvent& evt) {
        FrameUtilities::CreateFrame(this, evt, 2);
        });

    customisation->Bind(wxEVT_BUTTON, [this](wxCommandEvent& evt) {
        FrameUtilities::CreateFrame(this, evt, 2);
        });

    leaderboard->Bind(wxEVT_BUTTON, [this](wxCommandEvent& evt) {
        FrameUtilities::CreateFrame(this, evt, 2);
        });

    classroom->Bind(wxEVT_BUTTON, [this](wxCommandEvent& evt) {
        FrameUtilities::CreateFrame(this, evt, 2);
        });

    friends_tab->Bind(wxEVT_BUTTON, [this](wxCommandEvent& evt) {
        FrameUtilities::CreateFrame(this, evt, 2);
        });

    // 文本控件

    wxStaticText* daily_streak = new wxStaticText(this, wxID_ANY, "52", wxPoint(395, 10));

    daily_streak->SetFont(streak_font);

}

框架初始加载时的显示效果:
框架初始加载时的显示效果

鼠标悬停所有按钮后的显示效果:
鼠标悬停按钮后的显示效果

内容的提问来源于stack exchange,提问作者Calax

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 15:15:10