event核心[wxWidgets]_[简单应用看wx的核心原理]

首先声明,我是一个菜鸟。一下文章中出现技术误导情况盖不负责

    event和核心

    

    1.wx的核心道理和MFC差不多,其中的一部分是menu,event,thread,dc,control.

    2.以下是使用和wx这些功能的例子,在MinGW(g++),msys(make)下编译.g++ 4.4,我用的eclipse CDT建的工程,单是编译的话直接用make就好了,不须要eclipse.

    3.用的wxWidgets-2.9.2,核心的dll只须要3个,由于是编译了基本上全体功能和控件,所以这些库还是比较大的,不过有很大的压缩余地。也和gcc编译有关。

wxbase292u_gcc_custom.dll (6,461 kb)
wxmsw292u_adv_gcc_custom.dll (5,498 kb)
wxmsw292u_core_gcc_custom.dll (12,237 kb)

    4.应用程序入口文件:

    my_app.h

/*
 * my_app.h
 *
 *  Created on: 2013-5-30
 *  Author: Sai
 */

#ifndef MY_APP_H_
#define MY_APP_H_

#include "wx/wx.h"

class MyApp: public wxApp
{
public:
	MyApp();
	virtual ~MyApp();

	bool OnInit();
};

#endif /* MY_APP_H_ */

    my_app.cpp

/*
 * my_app.cpp
 *
 *  Created on: 2013-5-30
 *  Author: Sai
 */

#include "my_app.h"
#include "my_frame.h"

IMPLEMENT_APP(MyApp)

MyApp::MyApp()
{

}

MyApp::~MyApp()
{

}

bool MyApp::OnInit()
{
	MyFrame* frame = new MyFrame();
	frame->Create(wxID_ANY,wxT("infoworld"));

	SetTopWindow(frame);
	frame->Show(true);
	return true;
}

    5.主窗口文件

    my_frame.h

/*
 * my_frame.h
 *
 *  Created on: 2013-5-30
 *  Author: Sai
 */

#ifndef MY_FRAME_H_
#define MY_FRAME_H_

#include "wx/wx.h"

/**
 * Show Example
 *
 * 1.menu
 * 2.event
 * 3.thread
 * 4.dc
 * 5.control
 */
class MyFrame : public wxFrame
{
	DECLARE_EVENT_TABLE()
	DECLARE_DYNAMIC_CLASS(MyFrame)
public:
	MyFrame();
	virtual ~MyFrame();
	virtual bool Create(wxWindowID id, const wxString& title);

protected:
	void OnQuit(wxCommandEvent& event);
	void OnThread(wxCommandEvent& event);
	void OnThreadUpdate(wxThreadEvent& event);
	void OnPaintPanel(wxPaintEvent& event);
private:
	wxGauge* gauge_;
	wxPanel *panel_;
};

#endif /* MY_FRAME_H_ */

    my_frame.cpp

/*
 * my_frame.cpp
 *
 *  Created on: 2013-5-30
 *  Author: Sai
 */

#include "my_frame.h"

namespace
{
enum
{
	kGauge = 1, kButton, kThreadUpdateId
};
}

class MyThread: public wxThread
{
public:
	MyThread(MyFrame *handler) :
			wxThread(wxTHREAD_DETACHED)
	{
		m_pHandler = handler;
	}
	~MyThread()
	{
	}
	void Execute()
	{
		Create();
		Run();
	}

protected:
	virtual ExitCode Entry()
	{
		for (int i = 1; i < 11; ++i)
		{
			//1.asynchronous update,communicate with main thread.
			wxThreadEvent* event = new wxThreadEvent(wxEVT_THREAD,
					kThreadUpdateId);
			event->SetInt(i * 10);
			wxQueueEvent(m_pHandler->GetEventHandler(), event);
		}
		return (wxThread::ExitCode) 0;
	}
	MyFrame *m_pHandler;
};
BEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
EVT_BUTTON(kButton, MyFrame::OnThread)
EVT_THREAD(kThreadUpdateId, MyFrame::OnThreadUpdate)
END_EVENT_TABLE()

IMPLEMENT_DYNAMIC_CLASS(MyFrame, wxFrame)

MyFrame::MyFrame()
{

}

MyFrame::~MyFrame()
{

}

bool MyFrame::Create(wxWindowID id, const wxString& title)
{
	if (!wxFrame::Create(NULL, id, title, wxDefaultPosition, wxSize(400, 400)))
	{
		return false;
	}
	panel_ = new wxPanel(this, wxID_ANY, wxPoint(0, 0));
	panel_->SetBackgroundColour(*wxBLUE);
	panel_->Connect(wxEVT_PAINT, wxPaintEventHandler(MyFrame::OnPaintPanel),
			NULL, this);

	//1.menubar and statusbar
	wxMenu *fileMenu = new wxMenu();

	(*fileMenu).Append(wxID_EXIT, wxT("&Exit\tAlt-X"),
			wxT("Quit this program"));

	wxMenuBar *bar = new wxMenuBar();
	bar->Append(fileMenu, wxT("&File"));

	SetMenuBar(bar);
	CreateStatusBar();
	SetStatusText(wxT("welcome to WxWidget"));

	//2.button,
	wxButton* button = new wxButton(panel_, kButton, wxT("wxThread"),
			wxPoint(20, 20));
	gauge_ = new wxGauge(panel_, kGauge, 100, wxPoint(20, 60), wxSize(200, 40),
			wxGA_HORIZONTAL);
	gauge_->SetValue(0);

	return true;
}

void MyFrame::OnPaintPanel(wxPaintEvent& event)
{
	wxPaintDC dc(panel_);
	dc.SetPen(wxPen(*wxRED));
	wxSize size = panel_->GetSize();
	dc.DrawRectangle(wxRect(20, 20, size.x - 40, size.y - 40));
}

void MyFrame::OnThreadUpdate(wxThreadEvent& event)
{
	gauge_->SetValue(event.GetInt());
}

void MyFrame::OnThread(wxCommandEvent& event)
{
	wxMessageBox(wxT("Start a wxThread"), wxT("Good evening."));
	MyThread* thread = new MyThread(this);
	thread->Execute();
}

void MyFrame::OnQuit(wxCommandEvent& event)
{
	Close();
}
    每日一道理
如果人类不好好保护我们这个赖以生存的地球,终有一天,风沙的肆虐与垃圾的堆积会吞没我们美丽的家园。我向全世界的人们呼吁:让我们从现在开始,从我做起,手挽手,肩并肩共同保护建设我们的家园吧!

    6.值的一提的是如果要用vista以上的好看的控件样式,须要资源文件定义,再把它链接到exe里。

windres --input-format=rc -O coff -i resources/style.rc -o build/style.o --define __WXMSW__ --include-dir E:/software/Lib/gui/wxWidgets-2.9.2/include

    style.rc

#include "winresrc.h"

// set this to 1 if you don't want to use manifest resource (manifest resource
// is needed to enable visual styles on Windows XP - see docs/msw/winxp.txt
// for more information)
#define wxUSE_NO_MANIFEST 0

// this is not always needed but doesn't hurt (except making the executable
// very slightly larger): this file contains the standard icons, cursors, ...
#include "wx/msw/wx.rc"

LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US

//wx.rc contain .manifest,so override it.
1 RT_MANIFEST "uac.manifest"

    uac.manifest

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity version="0.64.1.0" processorArchitecture="x86" name="Controls" type="win32"></assemblyIdentity>
<description></description>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3"> 
		<security> 
			<requestedPrivileges> 
				<requestedExecutionLevel level="asInvoker" uiAccess="false"></requestedExecutionLevel> 
			</requestedPrivileges> 
		</security> 
</trustInfo>
<dependency>
    <dependentAssembly>
        <assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" processorArchitecture="X86" publicKeyToken="6595b64144ccf1df" language="*"></assemblyIdentity>
    </dependentAssembly>
</dependency>
</assembly>

    完全程序下载地址:

    http://download.csdn.net/download/infoworld/5487427

    

文章结束给大家分享下程序员的一些笑话语录: 神灯新篇
一个程序员在海滩上发现了一盏神灯。他在灯上擦了几下,一个妖怪就从灯里跳出来说:“我是世界上法术最强的妖怪。我可以实现你的任何梦想,但现在,我只能满足你一个愿望。”程序员摊开了一幅中东地图说:“我想让中东得到永久的和平。”妖怪答道:“哦,我没办法。自打创世纪以来,那里的战火就没有停息过。这世上几乎没有我办不到的事,但这件事除外。”程序员于是说:“好吧,我是一个程序员,为许多用户编写过程序。你能让他们把需求表述得更清楚些,并且让我们的软件项目有那么一两次按进度按成本完成吗?”妖怪说:“唔,我们还是来看中东地图吧。”

--------------------------------- 原创文章 By
event和核心
---------------------------------

原文地址:https://www.cnblogs.com/jiangu66/p/3111487.html