C_and_CPP 板


LINE

我举个基本小程式当范例说明好了 一个基本的wx程式开起来会有两个类别 一个是App类别 另一个是Frame类别 观念是视窗只是个互动式的介面 在视窗上面做的所有事情都叫做事件(event) 包括你可能用滑鼠点了file或open 这都是event 当某个event被触发 程式就会做下个动作 例如 你点了open 跑出点选档案视窗 然後你选了某个档案按下确定 而这些event会有一个处理event的机制去管理 这叫event handling 视窗跟使用者还有视窗跟视窗本身就是靠着这个东西来互动... 每一个wx程式一定都会有一个App类别(也只能有一个) 而你所想知道的程式进入点就是那个OnInit(); App类别架构如下 // 宣告 class mainloopApp : public wxApp { public: virtual bool OnInit(); }; OnInit()的实作内容就是等效於main()里面的内容 稍微看一下内容大概可以知道 OnInit() new一个Frame 然後秀出来 return值为真 程式开始进入event loop 若return为否 就中止程式 这跟main()的差别很大 // 实作 IMPLEMENT_APP(mainloopApp); bool mainloopApp::OnInit() { //(*AppInitialize bool wxsOK = true; wxInitAllImageHandlers(); if ( wxsOK ) { mainloopFrame* Frame = new mainloopFrame(0); Frame->Show(); SetTopWindow(Frame); } //*) return wxsOK; } 接下来看的是视窗的骨架 举凡你看的到的看不到的元件都要写在这里面 按钮啊 功能表啊 图框啊 什麽的都在这边规划 当然一个一个自己刻很累人 所以有工具 code::blocks 有内建 wxsmith 可以让你作视窗 并且把程式码一并写入 这很方便 不过你也要了解他帮你写了什麽东西比较容易进步 // Frame宣告部份 class mainloopFrame: public wxFrame { public: mainloopFrame(wxWindow* parent,wxWindowID id = -1); virtual ~mainloopFrame(); private: //(*Handlers(mainloopFrame) void OnQuit(wxCommandEvent& event); void OnAbout(wxCommandEvent& event); //*) //(*Identifiers(mainloopFrame) static const long idMenuQuit; static const long idMenuAbout; static const long ID_STATUSBAR1; //*) //(*Declarations(mainloopFrame) wxStatusBar* StatusBar1; //*) DECLARE_EVENT_TABLE() }; 从这Frame的建构子可以了解 整个视窗的架构 我不做解释了 // 实作部份 不过之前还有一小段event table 不过我不常用 我都用connect() mainloopFrame::mainloopFrame(wxWindow* parent,wxWindowID id) { //(*Initialize(mainloopFrame) 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)&mainloopFrame::OnQuit); Connect(idMenuAbout,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&mainloopFrame::OnAbout); //*) } mainloopFrame::~mainloopFrame() { //(*Destroy(mainloopFrame) //*) } void mainloopFrame::OnQuit(wxCommandEvent& event) { Close(); } void mainloopFrame::OnAbout(wxCommandEvent& event) { wxString msg = wxbuildinfo(long_f); wxMessageBox(msg, _("Welcome to...")); } 以上两个OnXxxxxx method是按钮的实作 一个实作了关闭视窗 一个秀出wx的资讯 当然你也可以做一个按钮 用connect去撷取当滑鼠按下按钮的event 按下按钮的执行内容就简单多了阿 就跟一般写程式一样 看你是要计算1+1=2 还是要Hello world之类的 咳咳 小结论来了 因为这种event loop 的架构 很多地方都可以是entry 视窗这种是跟使用者互动的程式 在OnInit写太多东西也没什麽好处啦 个人小经验 有说错的话请不吝指正 reference wxwidgets的官方文件 --



※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.118.7.163
1F:推 Alexboo:你真是个好人 03/22 15:59
2F:→ kvykn:楼上这样说我就想删掉这一篇欸 03/22 16:22







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:e-shopping站内搜寻

TOP