求助,Linux下如何用C++编程创建一个窗体

求助,Linux下如何用C++编程创建一个窗体,第1张

界面,你要学习界面库,linux 下比较出名的界面库就是GTK,QT和wxWidgets,自己google下吧,建议学习GTk,整个gnome就是用它做出来的。

本人学的是wxWidgets,就以它为例了。

#include <wx/app.h>

#include <wx/image.h>

#include <wx/msgdlg.h>

#include <wx/string.h>

#include <wx/intl.h>

#include <wx/menu.h>

#include <wx/statusbr.h>

#include <wx/frame.h>

//*)

class SampleApp : public wxApp

{

public:

virtual bool OnInit()

}

IMPLEMENT_APP(SampleApp)

bool SampleApp::OnInit()

{

//(*AppInitialize

bool wxsOK = true

wxInitAllImageHandlers()

if ( wxsOK )

{

SampleFrame* Frame = new SampleFrame(0)

Frame->Show()

SetTopWindow(Frame)

}

return wxsOK

}

class SampleFrame: public wxFrame

{

public:

SampleFrame(wxWindow* parent,wxWindowID id = -1)

virtual ~SampleFrame()

private:

//(*Handlers(SampleFrame)

void OnQuit(wxCommandEvent&event)

void OnAbout(wxCommandEvent&event)

//*)

//(*Identifiers(SampleFrame)

static const long idMenuQuit

static const long idMenuAbout

static const long ID_STATUSBAR1

//*)

//(*Declarations(SampleFrame)

wxStatusBar* StatusBar1

//*)

DECLARE_EVENT_TABLE()

}

//(*IdInit(SampleFrame)

const long SampleFrame::idMenuQuit = wxNewId()

const long SampleFrame::idMenuAbout = wxNewId()

const long SampleFrame::ID_STATUSBAR1 = wxNewId()

//*)

BEGIN_EVENT_TABLE(SampleFrame,wxFrame)

//(*EventTable(SampleFrame)

//*)

END_EVENT_TABLE()

SampleFrame::SampleFrame(wxWindow* parent,wxWindowID id)

{

//(*Initialize(SampleFrame)

wxMenuItem* MenuItem2

wxMenuItem* MenuItem1

wxMenu* Menu1

wxMenuBar* MenuBar1

wxMenu* Menu2

Create(parent, id, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE, _T("id"))

MenuBar1 = new wxMenuBar()

Menu1 = new wxMenu()

MenuItem1 = new wxMenuItem(Menu1, idMenuQuit, _("Quit\tAlt-F4"), _("Quit the application"), wxITEM_NORMAL)

Menu1->Append(MenuItem1)

MenuBar1->Append(Menu1, _("&File"))

Menu2 = new wxMenu()

MenuItem2 = new wxMenuItem(Menu2, idMenuAbout, _("About\tF1"), _("Show info about this application"), wxITEM_NORMAL)

Menu2->Append(MenuItem2)

MenuBar1->Append(Menu2, _("Help"))

SetMenuBar(MenuBar1)

StatusBar1 = new wxStatusBar(this, ID_STATUSBAR1, 0, _T("ID_STATUSBAR1"))

int __wxStatusBarWidths_1[1] = { -1 }

int __wxStatusBarStyles_1[1] = { wxSB_NORMAL }

StatusBar1->SetFieldsCount(1,__wxStatusBarWidths_1)

StatusBar1->SetStatusStyles(1,__wxStatusBarStyles_1)

SetStatusBar(StatusBar1)

Connect(idMenuQuit,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&SampleFrame::OnQuit)

Connect(idMenuAbout,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&SampleFrame::OnAbout)

//*)

}

SampleFrame::~SampleFrame()

{

//(*Destroy(SampleFrame)

//*)

}

void SampleFrame::OnQuit(wxCommandEvent&event)

{

Close()

}

void SampleFrame::OnAbout(wxCommandEvent&event)

{

wxString msg = wxT("This is a sample windows!")

wxMessageBox(msg, _("Welcome to..."))

}

windows下通过调用API来创建窗口:

#include<windows.h>

int main()

{

MessageBox(NULL,"Hello World!","C图形程序",MB_OK)

return 0

}

linux下通过调用图形库来创建窗口。

楼主如果是学C的话,先不要急于搞这些东西,把基础打扎实才是最重要的,GUI可以后学。基础扎实了,这些只是很简单的东西。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/8564671.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-18
下一篇 2023-04-18

发表评论

登录后才能评论

评论列表(0条)

保存