I have this WxWidgets test source code that compiles, and when run, it shows a simple frame:
/*
* hworld.cpp
* Hello world sample by Robert Roebling
*/
#include "wx-2.8/wx/wx.h"
class MyApp: public wxApp
{
virtual bool OnInit();
};
class MyFrame: public wxFrame
{
public:
MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
void OnQuit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
DECLARE_EVENT_TABLE()
};
enum
{
ID_Quit = 1,
ID_About,
};
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(ID_Quit, MyFrame::OnQuit)
EVT_MENU(ID_About, MyFrame::OnAbout)
END_EVENT_TABLE()
IMPLEMENT_APP(MyApp)
bool MyApp::OnInit()
{
MyFrame *frame = new MyFrame( _T("Hello World"), wxPoint(50,50), wxSize(450,340) );
frame->Show(TRUE);
SetTopWindow(frame);
return TRUE;
}
MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
: wxFrame((wxFrame *)NULL, -1, title, pos, size)
{
wxMenu *menuFile = new wxMenu;
menuFile->Append( ID_About, _T("&About...") );
menuFile->AppendSeparator();
menuFile->Append( ID_Quit, _T("E&xit") );
wxMenuBar *menuBar = new wxMenuBar;
menuBar->Append( menuFile, _T("&File") );
SetMenuBar( menuBar );
CreateStatusBar();
SetStatusText( _T("Welcome to wxWindows!") );
}
void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
{
Close(TRUE);
}
void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
{
wxMessageBox(_T("This is a wxWindows Hello world sample"),
_T("About Hello World"), wxOK | wxICON_INFORMATION, this);
}
Created with this simple SCons script:
env = Environment()
env.ParseConfig('wx-config --cxxflags --libs')
env.Program(target='wxTest/wxTest.exe',source=['src/Wxwidgets.cpp'])
The problem: it wont focus when I run it. The only thing I can focus is the red, yellow and green buttons in the upper left corner.
I use Eclipse as my IDE and run scons as an external tool when I build it.
Is there someone out there that know what Im doing wrong? How can I get the frame to focus?
Hope there is someone out the who can help me.
I assume you start the raw executable that is created? This does not work on Mac OS X, see My app can't be brought to the front!
You will have to create an application bundle for your app to work properly on Mac OS X. I don't know anything about SCons, but maybe the wiki does help?
Related
I made a GUI application in wxWidgets and kept receiving anti virus alerts by various users. I spent a great amount of time commenting out code and re-uploading the EXE to VirusTotal. It turns out, none of this was my code. Using the wxWidgets framework alone will cause plenty of detections. I tried again by compiling a simple hello world application and sure enough, this is the result:
Full source code:
// wxWidgets "Hello world" Program
// For compilers that support precompilation, includes "wx/wx.h".
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
// Required for static linking
#pragma comment(lib, "comctl32")
#pragma comment(lib, "Rpcrt4")
class MyApp : public wxApp
{
public:
virtual bool OnInit();
};
class MyFrame : public wxFrame
{
public:
MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
private:
void OnHello(wxCommandEvent& event);
void OnExit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
wxDECLARE_EVENT_TABLE();
};
enum
{
ID_Hello = 1
};
wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(ID_Hello, MyFrame::OnHello)
EVT_MENU(wxID_EXIT, MyFrame::OnExit)
EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
wxEND_EVENT_TABLE()
wxIMPLEMENT_APP(MyApp);
bool MyApp::OnInit()
{
MyFrame* frame = new MyFrame("Hello World", wxPoint(50, 50), wxSize(450, 340));
frame->Show(true);
return true;
}
MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
: wxFrame(NULL, wxID_ANY, title, pos, size)
{
wxMenu* menuFile = new wxMenu;
menuFile->Append(ID_Hello, "&Hello...\tCtrl-H",
"Help string shown in status bar for this menu item");
menuFile->AppendSeparator();
menuFile->Append(wxID_EXIT);
wxMenu* menuHelp = new wxMenu;
menuHelp->Append(wxID_ABOUT);
wxMenuBar* menuBar = new wxMenuBar;
menuBar->Append(menuFile, "&File");
menuBar->Append(menuHelp, "&Help");
SetMenuBar(menuBar);
CreateStatusBar();
SetStatusText("Welcome to wxWidgets!");
}
void MyFrame::OnExit(wxCommandEvent& event)
{
Close(true);
}
void MyFrame::OnAbout(wxCommandEvent& event)
{
wxMessageBox("This is a wxWidgets' Hello world sample",
"About Hello World", wxOK | wxICON_INFORMATION);
}
void MyFrame::OnHello(wxCommandEvent& event)
{
wxLogMessage("Hello world from wxWidgets!");
}
How can these be fixed without contacting each AV vendor? Clearly, the code is not malicious and wxWidgets is a popular cross-platform GUI framework which shouldn't cause any AV detections on its own. The static EXE however is quite big and pulls in a lot of Windows API functions so it's hard to even pinpoint anything.
I have made an application using wxWidgets 3.1.5 in C++ and everything is working fine except a test button that I have on my main window.
Here's a pic:
The menubar, menus and their functions work perfectly but the button covers the entire client area.
Here's the code:
main.h
#pragma once
#include <wx\wx.h>
#include "mainFrame.h"
class main : public wxApp
{
private:
mainFrame* frame;
public:
virtual bool OnInit();
};
main.cpp
#include "main.h"
wxIMPLEMENT_APP(main);
bool main::OnInit()
{
frame = new mainFrame("Kill Me", wxPoint(15, 10), wxSize(640, 480));
frame->Show();
return true;
}
mainFrame.h
#pragma once
#include "About.h"
using str = std::string;
class mainFrame : public wxFrame
{
public:
mainFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
~mainFrame();
private:
About* abtF = NULL;
wxButton* hewwo = NULL;
wxMenuBar* mbar = NULL;
wxMenu* sett = NULL;
wxMenu* quitApp = NULL;
wxMenu* abt = NULL;
void onHewwo(wxCommandEvent& evt);
void onSett(wxCommandEvent& evt);
void quit(wxCommandEvent& evt);
void about(wxCommandEvent& evt);
wxDECLARE_EVENT_TABLE();
};
enum {
ID_SETT = 1,
ID_BTN = 2
};
mainFrame.cpp
#include "mainFrame.h"
wxBEGIN_EVENT_TABLE(mainFrame, wxFrame)
EVT_BUTTON(ID_BTN, onHewwo)
EVT_MENU(ID_SETT, onSett)
EVT_MENU(wxID_EXIT, quit)
EVT_MENU(wxID_ABOUT, about)
wxEND_EVENT_TABLE()
mainFrame::mainFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
:
wxFrame(nullptr, wxID_ANY, title, pos, size) {
hewwo = new wxButton(this, ID_BTN, "Hewwo World", wxPoint(15, 15), wxSize(70, 20));
sett = new wxMenu();
sett->AppendSeparator();
sett->Append(ID_SETT, "&Settings");
quitApp = new wxMenu();
quitApp->AppendSeparator();
quitApp->Append(wxID_EXIT, "&Quit this crap");
abt = new wxMenu();
abt->AppendSeparator();
abt->Append(wxID_ABOUT, "&About");
mbar = new wxMenuBar();
mbar->Append(sett, "&Settings");
mbar->Append(abt, "&About");
mbar->Append(quitApp, "&Quit");
SetMenuBar(mbar);
}
void mainFrame::onHewwo(wxCommandEvent& evt) {
wxMessageBox("Hewwo", "Hewwo", wxOK | wxICON_INFORMATION, this);
}
void mainFrame::onSett(wxCommandEvent& evt) {
wxMessageBox("Settings", "Settings", wxOK | wxICON_INFORMATION, this); // Just a test
}
void mainFrame::about(wxCommandEvent& evt) {
abtF = new About(wxPoint(10, 10), wxSize(480, 320));
abtF->Show();
}
void mainFrame::quit(wxCommandEvent& evt) {
Close(true);
}
mainFrame::~mainFrame() {
delete abtF;
}
I'm using Visual Studio 2019.
(I followed OneLoneCoder's (javidx9) youtube video on wxWidgets)
That is how a wxFrame with only one child behaves.
If you don't want that, use a wxSizer to layout your button (position, align, expand etc).
Reference:
if the frame has exactly one child window, not counting the status and toolbar, this child is resized to take the entire frame client area. If two or more windows are present, they should be laid out explicitly either by manually handling wxEVT_SIZE or using sizers
wxFrame docs -> Default event processing -> wxEVT_SIZE
i just started to learn wxWidgets and I'm trying to create some kind of Image Opener.
But i cant understand how to add an image...
i seen some stuff about wxImage, wxBitMap,wxStaticBitmap, but i couldn't understand any of them or the difference between them.
#include <wx/wx.h>
#include <wx/filedlg.h>
#include <wx/wfstream.h>
class MyApp : public wxApp
{
public:
virtual bool OnInit();
};
class MyFrame : public wxFrame
{
public:
MyFrame (const wxString& event);
void OnOpen(wxCommandEvent& event);
void OnQuit(wxCommandEvent& event);
private:
DECLARE_EVENT_TABLE();
};
DECLARE_APP(MyApp);
IMPLEMENT_APP(MyApp);
bool MyApp::OnInit()
{
MyFrame *frame = new MyFrame(wxT("Minimal wxWidgets App"));
frame->Show(true);
return true;
}
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(wxID_OPEN, MyFrame::OnOpen)
EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
END_EVENT_TABLE()
void MyFrame::OnOpen(wxCommandEvent& event)
{
wxFileDialog openFileDialog(this, ("Open JPEG file"), "", "", "JPEG files (*.jpg)|*jpg", wxFD_OPEN|wxFD_FILE_MUST_EXIST);
if(openFileDialog.ShowModal() == wxID_CANCEL)
return;
wxFileInputStream input_stream(openFileDialog.GetPath());
if(!input_stream.IsOk())
{
wxLogError("Cannot Open File '%s'.", openFileDialog.GetPath());
return;
}
}
void MyFrame::OnQuit(wxCommandEvent& event)
{
Close(true);
}
MyFrame::MyFrame(const wxString& title) : wxFrame(NULL, wxID_ANY, title)
{
wxMenuBar * menuBar = new wxMenuBar;
wxMenu * exitMenu = new wxMenu;
wxMenu * openMenu = new wxMenu;
exitMenu->Append(wxID_EXIT);
openMenu->Append(wxID_OPEN);
menuBar->Append(openMenu, "&Open");
menuBar->Append(exitMenu, "&Exit");
SetMenuBar(menuBar);
CreateStatusBar(2);
SetStatusText(wxT("Image Opener !"));
}
can someone show an example of adding an image on wxwidgets ? or adding it to my code .
thanks for help !
wxImage is a portable representation of an image, wxBitmap is a platform-specific but more efficient equivalent. Generally speaking, wxImage is convenient for working with image data, but it needs to be converted to wxBitmap to be displayed.
Both of these classes are just abstract images, while wxStaticBitmap is a control showing an image, i.e. something you can really see on screen.
The image sample (under samples directory of your wxWidgets distribution) shows how to use the different classes and draw images directly.
wxImage and wxBitmap are almost same and conversion between them is easy. wxStaticBitmap is used to display a wxBitmap.it can be done by following example(StaticBitmap1 is your wxStaticBitmap and btmp is your wxBitmap):
StaticBitmap1->SetBitmap(btmp);
I am using wxTreeListCtrl with wxTL_CHECKBOX style, but for some reason I am not able to check checkbox using mouse click, though I can do it by pressing SPACE. Is this normal, or am I missing something ?
My code:
// .h file
#include <wx/wx.h>
#include <wx/treelist.h>
class MyApp: public wxApp
{
public:
virtual bool OnInit();
};
class MyFrame: public wxFrame
{
public:
MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
private:
void OnTreeLeftDown (wxMouseEvent& evt);
private:
enum
{
TreeListId = 1
};
wxTreeListCtrl* m_treeList;
wxDECLARE_EVENT_TABLE();
};
// .cpp file
wxBEGIN_EVENT_TABLE (MyFrame, wxFrame)
wxEND_EVENT_TABLE()
wxIMPLEMENT_APP(MyApp);
bool MyApp::OnInit()
{
MyFrame *frame = new MyFrame ("Hello World", wxPoint(50, 50), wxSize(600, 400));
frame->Show (true);
return true;
}
MyFrame::MyFrame (const wxString& title, const wxPoint& pos, const wxSize& size)
: wxFrame (NULL, wxID_ANY, title, pos, size)
{
m_treeList = new wxTreeListCtrl (this, TreeListId, wxPoint (10, 10), wxSize(600, 400), wxTL_CHECKBOX);
m_treeList->AppendColumn (L"Item Name");
m_treeList->AppendItem (m_treeList->GetRootItem(), L"Test");
m_treeList->AppendItem (m_treeList->GetRootItem(), L"Another one");
CenterOnScreen ();
}
As someone pointed out in wxWidgets forum it is a bug in 3.0.0
Here is bug url http://trac.wxwidgets.org/ticket/15731
I am trying to deal with wxWidgets for the first time, I tried to compile the below Hello World program:
/*
* hworld.cpp
*/
#include "wx/wx.h"
class MyApp: public wxApp
{
virtual bool OnInit();
};
class MyFrame: public wxFrame
{
public:
MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
void OnQuit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
DECLARE_EVENT_TABLE()
};
enum
{
ID_Quit = 1,
ID_About,
};
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(ID_Quit, MyFrame::OnQuit)
EVT_MENU(ID_About, MyFrame::OnAbout)
END_EVENT_TABLE()
IMPLEMENT_APP(MyApp)
bool MyApp::OnInit()
{
MyFrame *frame = new MyFrame( _("Hello World"), wxPoint(50, 50),
wxSize(450,340) );
frame->Show(true);
SetTopWindow(frame);
return true;
}
MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
: wxFrame( NULL, -1, title, pos, size )
{
wxMenu *menuFile = new wxMenu;
menuFile->Append( ID_About, _("&About...") );
menuFile->AppendSeparator();
menuFile->Append( ID_Quit, _("E&xit") );
wxMenuBar *menuBar = new wxMenuBar;
menuBar->Append( menuFile, _("&File") );
SetMenuBar( menuBar );
CreateStatusBar();
SetStatusText( _("Welcome to wxWidgets!") );
}
void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
{
Close(TRUE);
}
void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
{
wxMessageBox( _("This is a wxWidgets Hello world sample"),
_("About Hello World"),
wxOK | wxICON_INFORMATION, this);
}
The compiler showed:
Error 1 error LNK2001: unresolved external symbol _WinMainCRTStartup C:\Users\550\documents\visual studio 11\Projects\wxWidgitExample\wxWidgitExample\LINK
Error 2 error LNK1120: 1 unresolved externals C:\Users\550\documents\visual studio 11\Projects\wxWidgitExample\Debug\wxWidgitExample.exe 1
What is the problem ? I use MS Visual Studio, I guess I need to use #pragma directive?
Try putting wxDECLARE_APP(MyApp); in the header file where MyApp is defined and wxIMPLEMENT_APP(MyApp); in the cpp file where it is implemented. The description of these macros is avaliable in the documentation.
Check your property sheets under Linker->System->SubSystem. Make sure the subsystem is Windows and not Console.
That linker error you're getting usually indicates a mismatch in your project configuration settings between ansi vs unicode setup. If you're building your wxApplication with unicode enabled make sure UNICODE and _UNICODE are defined.
The entry point for a windows unicode application is wWinMainCRTStartup where as a non-unicode one has WinMainCRTStartup as its entry point. Normally wxWidgets should have that setup correctly for you when you built the framework.
To help troubleshoot your unresolved error I would try passing this switch to the linker when you attempt to build.
If you're example is not using unicode then use /ENTRY:WinMainCRTStartup.
If you're example is using unicode then use /ENTRY:wWinMainCRTStartup.