Transcript
Page 1: Chapter 17 Windows API 程式入門 VC++ 與 WindowsVC++ 與 Windows 建構遊戲設計的舞台 建構遊戲設計的舞台

Chapter 17 Windows API 程式入門

VC+ + 與Windows

建構遊戲設計的舞台

Page 2: Chapter 17 Windows API 程式入門 VC++ 與 WindowsVC++ 與 Windows 建構遊戲設計的舞台 建構遊戲設計的舞台

VC++ 與 Windows API

在 VC++ 的開發環境中,對於撰寫

Windows 作業系統平台的視窗程式有兩種不

同的程式架構:一是微軟在 VC++ 中所加入

的 MFC 架構,一則 Windows API 架構。

Page 3: Chapter 17 Windows API 程式入門 VC++ 與 WindowsVC++ 與 Windows 建構遊戲設計的舞台 建構遊戲設計的舞台

VC++在遊戲程式開發上所具備的優點:

優越的速度表現

彈性管理資源與記憶體

易於使用 Windows API

Page 4: Chapter 17 Windows API 程式入門 VC++ 與 WindowsVC++ 與 Windows 建構遊戲設計的舞台 建構遊戲設計的舞台

優越的速度表現

C++ 程式編譯後的檔案為可直接執行的機器碼。

C++

原始程式

Java

原始程式

機器碼

中間碼

編譯速度慢

編譯速度快

直接執行速度快

透過解譯器解釋執行 ( 速度慢 )

Page 5: Chapter 17 Windows API 程式入門 VC++ 與 WindowsVC++ 與 Windows 建構遊戲設計的舞台 建構遊戲設計的舞台

彈性管理資源與記憶體

在 VC++ 的開發環境中,通常是透過一個資源識別碼,就可以對該項資源來進行運用。

在記憶體管理部份, C/C++ 語言本身就具備記憶體管理的功能,除了可透過指標進行記憶體的存取、配置之外,也提供了完整的記憶體管理相關函式。

Page 6: Chapter 17 Windows API 程式入門 VC++ 與 WindowsVC++ 與 Windows 建構遊戲設計的舞台 建構遊戲設計的舞台

易於使用 Windows API

Windows API(Application Program

Interface) 是 Windows 作業系統所提供的動態連結函式庫, Windows API 中包含了 Windows 核心及所有應用程式所需要的功能。

Page 7: Chapter 17 Windows API 程式入門 VC++ 與 WindowsVC++ 與 Windows 建構遊戲設計的舞台 建構遊戲設計的舞台

Windows API 從 Windows 作業系統發

展至今,主要可分為 Win16(Windows

3.1 以前 ) 以及 Win32(Windows 95 以

後 ) 兩種版本,不同 Windows 系統版本

間 API 的內容或多或少有些許差異 。

Page 8: Chapter 17 Windows API 程式入門 VC++ 與 WindowsVC++ 與 Windows 建構遊戲設計的舞台 建構遊戲設計的舞台

動態連結 (Dynamic Linking )

是指程式在執行階段,真正呼叫到外部函式時才

會去進行連結 ( 註:將程式碼中呼叫函式的指標

指向外部函式所在位址 ) 的動作。

Page 9: Chapter 17 Windows API 程式入門 VC++ 與 WindowsVC++ 與 Windows 建構遊戲設計的舞台 建構遊戲設計的舞台

建構遊戲設計的舞台

建立程式專案

VC++提供專案精靈方便使用智者建立程式

專案。

Page 10: Chapter 17 Windows API 程式入門 VC++ 與 WindowsVC++ 與 Windows 建構遊戲設計的舞台 建構遊戲設計的舞台

1.選擇Win32 Application

3.輸入專案名稱

4.按此鈕

2.選擇儲存路徑

Page 11: Chapter 17 Windows API 程式入門 VC++ 與 WindowsVC++ 與 Windows 建構遊戲設計的舞台 建構遊戲設計的舞台

1.選此項

2.按此鈕

Page 12: Chapter 17 Windows API 程式入門 VC++ 與 WindowsVC++ 與 Windows 建構遊戲設計的舞台 建構遊戲設計的舞台

4.按此鈕完成

Page 13: Chapter 17 Windows API 程式入門 VC++ 與 WindowsVC++ 與 Windows 建構遊戲設計的舞台 建構遊戲設計的舞台

在專案建立完成後,檢視工作區中的檔案列表,將可看到以下的內容:

Page 14: Chapter 17 Windows API 程式入門 VC++ 與 WindowsVC++ 與 Windows 建構遊戲設計的舞台 建構遊戲設計的舞台

檔案名稱 說明

canvas.cpp

主程式檔,其中包含整個專案的主程式WinMain

canvas.rc

資源檔,定義了整個專案所使用的資源

StdAfx.h 標頭檔,其中引用整個專案所需的標頭檔

Page 15: Chapter 17 Windows API 程式入門 VC++ 與 WindowsVC++ 與 Windows 建構遊戲設計的舞台 建構遊戲設計的舞台

您可以直接按 F5 執行這個專案,執行畫面如下:

Page 16: Chapter 17 Windows API 程式入門 VC++ 與 WindowsVC++ 與 Windows 建構遊戲設計的舞台 建構遊戲設計的舞台

Exercise 01

請利用 Visual C++ 精靈新增一個Win32 Application 的 Hello World 視窗程式

專案名稱為『學號 _01 』 ( 例: 8223582_01)

執行完成後使用 ftp 上傳至192.192.155.217 的 EX01 目錄下,帳號: student5 ,密碼: student5

Page 17: Chapter 17 Windows API 程式入門 VC++ 與 WindowsVC++ 與 Windows 建構遊戲設計的舞台 建構遊戲設計的舞台

修改後執行時的視窗畫面

Page 18: Chapter 17 Windows API 程式入門 VC++ 與 WindowsVC++ 與 Windows 建構遊戲設計的舞台 建構遊戲設計的舞台

程式架構說明

函式名稱 說明

WinMain 主程式,程式進入點

WndProc 自訂函式,處理程式訊息

MyRegisterClass

自訂函式,註冊視窗類別

InitInstance 自訂函式,建立及顯示視窗的初始函式

Page 19: Chapter 17 Windows API 程式入門 VC++ 與 WindowsVC++ 與 Windows 建構遊戲設計的舞台 建構遊戲設計的舞台

向系統註冊視窗類別

取得程式訊息

訊息轉換

將控制權交給系統,再由系統決定處理訊息的程式

Page 20: Chapter 17 Windows API 程式入門 VC++ 與 WindowsVC++ 與 Windows 建構遊戲設計的舞台 建構遊戲設計的舞台

宣告視窗類別WNDCLASSEX結構 wcex

訊息處理函式

類別名稱

Page 21: Chapter 17 Windows API 程式入門 VC++ 與 WindowsVC++ 與 Windows 建構遊戲設計的舞台 建構遊戲設計的舞台

建立一視窗物件,第一個參數是視窗建立的類別名稱

設定視窗顯示的位置及視窗大小設定視窗顯示時的狀態

將視窗繪製時顯示設備上

Page 22: Chapter 17 Windows API 程式入門 VC++ 與 WindowsVC++ 與 Windows 建構遊戲設計的舞台 建構遊戲設計的舞台
Page 23: Chapter 17 Windows API 程式入門 VC++ 與 WindowsVC++ 與 Windows 建構遊戲設計的舞台 建構遊戲設計的舞台

Exercise 02

請利用 Visual C++ 精靈新增一個 Win32 Application 的 Simple Win32 Application 視窗程式

專案名稱為『學號 _02 』 ( 例: 8223582_02)

下載 ch17_02.cpp並將其內容取代『學號_02.cpp 』

編譯及執行無誤後,使用 ftp 將『學號_02.cpp』上傳至 192.192.155.217 的 EX02目錄下,帳號: student5 ,密碼: student5


Top Related