wxWindows 第一個Frame程式
聯系我,請發郵件至 huyoo353{at}126{dot}com
前幾天給出了一個最簡單的Hello World 程式, 由于這幾天我還在學習wxWindows, 是以把今天做的第一個Frame架構程式寫出來,給大家分享.
1. 建立一個Win32 Application,名字為Frame,然後選擇Empty Project,點Finish完成.
2. Insert 一個Class, Class Type為Generic Class,名字為 CMyApp ,基類資訊中Deviced From 填入wxApp,類别為Public(預設),彈出對話框點OK.
3. 按照和上面同樣的步驟插入一個新類 CMyFrame, 基類為wxFrame, Public 繼承.
4. 在ClassView中輕按兩下CMyApp的構造函數CMyApp(),在MyApp.cpp中加入#include "wx/wx.h"
5. 同樣在MyFrame.cpp中加入#include "wx/wx.h"
6. 在ClassView中右鍵單擊CMyApp,選擇添加成員函數,加入一個Public的virtual bool OnInit()成員函數.
7. 在ClassView中右鍵單擊CMyFrame,選擇添加成員函數,加入一個Public的MyFrame(const wxChar *title,int xpos,int ypos,int width,int height)的構造函數(無類型的).
8. 修改MyFrame(const wxChar *title,int xpos,int ypos,int width,int height)如下:
MyFrame::MyFrame(const wxChar *title,int xpos,int ypos,int width,int height)
:wxFrame((wxFrame*)NULL,-1,title,wxPoint(xpos,ypos),wxSize(width,height))
{
// 建立狀态欄
CreateStatusBar();
SetStatusText("狀态欄就緒");
}
9. 修改CMyApp的OnInit()如下
bool CMyApp::OnInit()
{
MyFrame* frame=new MyFrame("我的第一個架構視窗",100,100,400,300);
frame->Show(TRUE);
return true;
}
10. 在MyApp.cpp頭部加入#include "MyFrame.h"
在CMyApp:CMyApp(){}前面加入IMPLEMENT_APP(CMyApp),看下面:
IMPLEMENT_APP(CMyApp)
CMyApp::CMyApp()
{
}
10.5 這個時候你如果點編譯按鈕的話,會出現一個緻命錯誤(Fatal Error)
fatal error C1083: Cannot open include file: 'wx/setup.h': No such file or directory
怎麼解決?看下面
11. 設定好Settings
win32 Debug配置
C++頁籤
預處理定義為: _DEBUG,WIN32,_WINDOWS,_MT,WINVER=0x400,_wxUSE_GUI,__WXDEBUG__,WXDEBUG=1
額外包含頭檔案目錄為: $(WXWIN)/lib/mswd
預編譯頭檔案選擇 Automatic use of precompiled headers
Code Generation 中的Use Runtime library 選擇 Debug MultiThreaded DLL
Link頁籤
在對象代碼子產品最後,添加comctl32.lib rpcrt4.lib wsock32.lib wxmswd.lib , 這4個庫
編譯完成,執行程式如下圖:

//源檔案附在後面
//
// MyApp.h: interface for the CMyApp class.
//
//
#if !defined(AFX_MYAPP_H__08DC4B2E_BCC2_48EC_A02A_D82513125055__INCLUDED_)
#define AFX_MYAPP_H__08DC4B2E_BCC2_48EC_A02A_D82513125055__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CMyApp : public wxApp
{
public:
virtual bool OnInit();
CMyApp();
virtual ~CMyApp();
};
#endif // !defined(AFX_MYAPP_H__08DC4B2E_BCC2_48EC_A02A_D82513125055__INCLUDED_)
//-------------------------------------------------------------------------
// MyApp.cpp: implementation of the CMyApp class.
//
//
#include "wx/wx.h"
#include "MyApp.h"
#include "MyFrame.h"
//
// Construction/Destruction
//
IMPLEMENT_APP(CMyApp)
CMyApp::CMyApp()
{
}
CMyApp::~CMyApp()
{
}
bool CMyApp::OnInit()
{
MyFrame* frame=new MyFrame("我的第一個架構視窗",100,100,400,300);
frame->Show(TRUE);
return true;
}
//============================================
// MyFrame.h: interface for the MyFrame class.
//
//
#if !defined(AFX_MYFRAME_H__6DA98C02_589A_44A9_B65A_F0C1941E3AFA__INCLUDED_)
#define AFX_MYFRAME_H__6DA98C02_589A_44A9_B65A_F0C1941E3AFA__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class MyFrame : public wxFrame
{
public:
MyFrame(const wxChar *title,int xpos,int ypos,int width,int height);
MyFrame();
virtual ~MyFrame();
};
#endif // !defined(AFX_MYFRAME_H__6DA98C02_589A_44A9_B65A_F0C1941E3AFA__INCLUDED_)
//-----------------------------------------------------
// MyFrame.cpp: implementation of the MyFrame class.
//
//
#include "wx/wx.h"
#include "MyFrame.h"
//
// Construction/Destruction
//
MyFrame::MyFrame()
{
}
MyFrame::~MyFrame()
{
}
MyFrame::MyFrame(const wxChar *title,int xpos,int ypos,int width,int height)
:wxFrame((wxFrame*)NULL,-1,title,wxPoint(xpos,ypos),wxSize(width,height))
{
// 建立狀态欄
CreateStatusBar();
SetStatusText("狀态欄就緒");
}
//=================================================