Microsoft PowerPoint - gp3.ppt

Similar documents
Microsoft PowerPoint - gp2.ppt

d2.doc

Microsoft PowerPoint - directx01.ppt

BOOL EnumWindows(WNDENUMPROC lparam); lpenumfunc, LPARAM (Native Interface) PowerBuilder PowerBuilder PBNI 2

int *p int a 0x00C7 0x00C7 0x00C int I[2], *pi = &I[0]; pi++; char C[2], *pc = &C[0]; pc++; float F[2], *pf = &F[0]; pf++;

ebook50-11

概述

mfc.doc

1. 注册自己的控件类 我把控件类名称定义为 "HyperLinkCtrl", 还要为窗口额外分配空间, 这样才能迚行更多的控制 // 注册控件类 ATOM WINAPI RegisterHyperLinkCtrl(HINSTANCE hins) WNDCLASSEX wndclass; ZeroM

Bus Hound 5

epub83-1

Microsoft Word - 把时间当作朋友(2011第3版)3.0.b.06.doc

The golden pins of the PCI card can be oxidized after months or years

INTRODUCTION TO COM.DOC

Guide to Install SATA Hard Disks

untitled

CC213

CC213

K7VT2_QIG_v3

Important Notice SUNPLUS TECHNOLOGY CO. reserves the right to change this documentation without prior notice. Information provided by SUNPLUS TECHNOLO

FY.DOC

1.ai

ebook140-8

概述

untitled

C 1

Programming Microsoft Windows CE .NET.doc

Windows XP

ebook140-9

Gerolor Motors Series Dimensions A,B C T L L G1/2 M8 G1/ A 4 C H4 E

Gerotor Motors Series Dimensions A,B C T L L G1/2 M G1/ A 4 C H4 E

Fun Time (1) What happens in memory? 1 i n t i ; 2 s h o r t j ; 3 double k ; 4 char c = a ; 5 i = 3; j = 2; 6 k = i j ; H.-T. Lin (NTU CSIE) Referenc

Microsoft Word - 105碩博甄簡章.doc

Microsoft PowerPoint - OPVB1基本VB.ppt

Microsoft Word - 正文.doc

WinMDI 28

27 :OPC 45 [4] (Automation Interface Standard), (Costom Interface Standard), OPC 2,,, VB Delphi OPC, OPC C++, OPC OPC OPC, [1] 1 OPC 1.1 OPC OPC(OLE f

Microsoft Word - 01.DOC

C C C The Most Beautiful Language and Most Dangerous Language in the Programming World! C 2 C C C 4 C Project 30 C Project 3 60 Project 40

國家圖書館典藏電子全文

ch_code_infoaccess

張 良 靜, 一 點 的 空 間, 和 一 個 廣 闊 的 胸 襟 有 點 禪 在 這 多 事 之 秋, 實 在 需 要 一 刻 的 寧 涼 風 吹 響 了 風 鈴, 清 脆 的 聲 音, 感 覺 齊 蛙 哇 月 刊 e-magazine 十 月 三 十 日 秋 涼 封 面 故 事 心 中 尺 二

3.1 num = 3 ch = 'C' 2

PowerPoint Presentation

Microsoft Word - (web)_F.1_Notes_&_Application_Form(Chi)(non-SPCCPS)_16-17.doc

Microsoft Word - CIN-DLL.doc

C/C++语言 - 分支结构

C/C++语言 - C/C++数据

Microsoft Word - HC20138_2010.doc

, 即 使 是 在 昏 暗 的 灯 光 下, 她 仍 然 可 以 那 么 耀 眼 我 没 有 地 方 去, 你 会 带 着 我 么 杜 晗 像 是 在 嘲 笑 一 般, 嘴 角 的 一 抹 冷 笑 有 着 不 适 合 这 个 年 龄 的 冷 酷 和 无 情, 看 着 江 华 的 眼 神 毫 无 温

声 明 本 人 郑 重 声 明 : 此 处 所 提 交 的 硕 士 学 位 论 文 基 于 等 级 工 鉴 定 的 远 程 考 试 系 统 客 户 端 开 发 与 实 现, 是 本 人 在 中 国 科 学 技 术 大 学 攻 读 硕 士 学 位 期 间, 在 导 师 指 导 下 进 行 的 研 究

Microsoft Word - ICF的編碼指引-new sjl.doc

K301Q-D VRT中英文说明书141009

Microsoft Word - ch04三校.doc

威 福 髮 藝 店 桃 園 市 蘆 竹 區 中 山 里 福 祿 一 街 48 號 地 下 一 樓 50,000 獨 資 李 依 純 105/04/06 府 經 登 字 第 號 宏 品 餐 飲 桃 園 市 桃 園 區 信 光 里 民

TX-NR3030_BAS_Cs_ indd

新版 明解C++入門編

Microsoft Word - 3D手册2.doc

TC35短信发送程序设计

EJB-Programming-4-cn.doc

运动员治疗用药豁免申报审批办法

Microsoft PowerPoint - STU_EC_Ch08.ppt

BC04 Module_antenna__ doc

LSC操作说明

Logitech Wireless Combo MK45 English

73.06% 14A 0.1% 5% 73.06% % 100% 51% 100% 100% 51% 73.06% 100% 100% 51% - 2 -

Windows 2000 Server for T100

RUN_PC連載_10_.doc

coverage2.ppt

游戏厅捕鱼技巧_天天酷跑游戏技巧 2048游戏技巧,游戏厅打鱼技巧_

謙卑的小巨人 文 / 林士涵 印製見證文集是父親在生病後就有的想法 目的是希望更多親朋好友能透 過這些見證認識主耶穌 一起享受屬耶穌那好得無比的生命 我的父親林進聰 民國 42 年 9 月 18 日生於台中縣大肚 鄉 退伍後輾轉來到工業技術研究院化工所上班 認識了他生 命中兩個最愛 信仰耶穌基督以及

科学计算的语言-FORTRAN95

藍牙網路在資訊家電的應用

Microsoft PowerPoint - os_4.ppt

目 錄 摘 要... 1 壹 研 究 動 機... 1 貳 研 究 目 的... 1 參 文 獻 探 討... 1 肆 研 究 設 備 及 材 料... 2 伍 研 究 過 程 及 方 法... 2 陸 研 究 結 果... 6 柒 討 論 捌 結 論 玖 參 考 資 料 及

EJB-Programming-3.PDF

Microsoft Word - 澎湖田調報告_璉謙組.doc

詞 彙 表 編 號 詞 彙 描 述 1 預 約 人 資 料 中 文 姓 名 英 文 姓 名 身 份 證 字 號 預 約 人 電 話 性 別 2 付 款 資 料 信 用 卡 別 信 用 卡 號 信 用 卡 有 效 日 期 3 住 房 條 件 入 住 日 期 退 房 日 期 人 數 房 間 數 量 入

提纲 1 2 OS Examples for 3

Strings

01何寄澎.doc

Microsoft Word - 把时间当作朋友(2011第3版)3.0.b.07.doc

2/80 2

r_09hr_practical_guide_kor.pdf


<4D F736F F F696E74202D20C6F3D2B5BCB0B2FAC6B7BCF2BDE92DD6D0D3A2CEC420C1F5B9FAD3B1205BBCE6C8DDC4A3CABD5D>

6-1 Table Column Data Type Row Record 1. DBMS 2. DBMS MySQL Microsoft Access SQL Server Oracle 3. ODBC SQL 1. Structured Query Language 2. IBM

Microsoft PowerPoint - 07-overloaded.ppt

Microsoft PowerPoint - ds-1.ppt [兼容模式]

硕 士 学 位 论 文 论 文 题 目 : 北 岛 诗 歌 创 作 的 双 重 困 境 专 业 名 称 : 中 国 现 当 代 文 学 研 究 方 向 : 中 国 新 诗 研 究 论 文 作 者 : 奚 荣 荣 指 导 老 师 : 姜 玉 琴 2014 年 12 月

Microsoft Word - 物件導向編程精要.doc

C/C++ 语言 - 循环

untitled

第7章-并行计算.ppt

全国计算机技术与软件专业技术资格(水平)考试

ebook 86-15

3戴文鋒-人文.indd

Microsoft Word - PHP7Ch01.docx

RunPC2_.doc

CC213

Transcription:

Windows 視窗程式設計 (2) 靜宜大學資訊管理學系蔡奇偉副教授 大綱 視窗的結構 Painting and Repainting GDI Device Context 視窗版的 Hello, world! 程式 取得裝置的功能資訊 版權所有 : 靜宜大學資訊管理學系蔡奇偉副教授 1

視窗的結構 標題列 (title) 工具列 (tools) 功能表 (menu) 工作區 (client) 狀態列 (status) Painting and Repainting 你的程式必須負責繪製視窗工作區的內容 通常的作法是在訊息處理函式中加入處理 WM_PAINT 訊息的繪圖程式碼 下列幾種情況會產生 WM_PAINT 訊息 : 視窗提到螢幕前, 而顯露出原本隱藏的部分 使用者縮放視窗的大小 使用者捲動視窗的內容 在程式中呼叫函式 InvalidateRect 或 InvalidateRgn 版權所有 : 靜宜大學資訊管理學系蔡奇偉副教授 2

下面幾種狀況可能會產生 WM_PAINT: 使用者在工作區上拖曳對話框或訊息框 拉下和放開選單 顯示工具提示 下面幾種狀況的視窗工作區內容更新是由視窗系統負責, 而不需要你寫程式碼來處理 : 滑鼠游標在工作區移動 拖曳小圖像通過工作區 Valid & Invalid Rectangle A 無效矩形 (Invalid Rectangle) 當視窗 A 被提到前面時, 原本被遮住的區域浮現出來, 這些區域稱 無效區域, 包含無效區域的最小矩形稱為 無效矩形 無效區域 (Invalid Regions) 版權所有 : 靜宜大學資訊管理學系蔡奇偉副教授 3

GDI Application GDI Functions Device Driver Hardware Windows 設計了 Graphic Device Interface (GDI) 的介面來規定使用繪圖硬體的方式 繪圖硬體製造廠商必須提供適當的 GDI 驅動程式, 才能給 Windows 使用 你可以用 Windows API 中的 GDI 函式在視窗中繪圖或列印至印表機 Device Context 在繪圖輸出裝置 ( 如顯示卡或印表機 ) 上面繪圖之前, 我們必須先取得代表這個裝置的 Device Context ( 簡稱 DC)handle DC 本質上是一個存在 GDI 內的一個資料結構, 其中儲存了繪圖裝置特性與繪圖狀態的資訊 當你從 Windows 取得一個 DC handle 時,Windows 會把這資料結構先設好預設值 你可以用一些 GDI 函式來讀取或修改這些值 呼叫 GDI 函式時,DC handle 通常是你第一個代入的引數值 版權所有 : 靜宜大學資訊管理學系蔡奇偉副教授 4

以下我們說明取得 DC handle 的幾種方法 方法一 : BeginPaint 和 EndPaint 處理 WM_PAINT 時, 我們可以用 BeginPaint 和 EndPaint 來取得和釋放代表視窗工作區的 DC handle 程式碼如下: PAINTSTRUCT ps; HDC hdc; // handle to the device context case WM_PAINT: hdc = BeginPaint(hwnd, &ps // 把 hdc 傳入 GDI 函式來進行繪圖 EndPaint(hwnd, &ps return 0; PAINTSTRUCT 資料結構 typedef struct tagpaintstruct { HDC hdc; BOOL ferase; RECT rcpaint; BOOL frestore; BOOL fincupdate; BYTE rgbreserved[32]; } PAINTSTRUCT, *PPAINTSTRUCT; // handle to DC // 視窗背景是否要重繪 // 需要重繪的矩形區域 // 保留供內部使用 // 保留供內部使用 // 保留供內部使用 通常 ferase 的值是 FALSE, 表示 Windows 已經幫你重繪視窗的背景了 如果你想自行重繪的話, 請加上處理 WM_ERASEBKGND 訊息的程式碼 版權所有 : 靜宜大學資訊管理學系蔡奇偉副教授 5

rcpaint 是需要重繪的矩形區域, 其座標是定義在視窗工作區之上, 如下圖所示 : typedef struct _RECT { LONG left; LONG top; LONG right; LONG bottom; } RECT, *PRECT; 0 0 left right top rcpaint bottom BeginPaint 函式 HDC BeginPaint ( HWND hwnd, LPPAINTSTRUCT lppaint // handle to window // paint information The BeginPaint function prepares the specified window for painting and fills a PAINTSTRUCT structure with information about the painting. 版權所有 : 靜宜大學資訊管理學系蔡奇偉副教授 6

EndPaint 函式 BOOL EndPaint ( HWND hwnd, CONST PAINTSTRUCT *lppaint // handle to window // paint data The EndPaint function marks the end of painting in the specified window. This function is required for each call to the BeginPaint function, but only after painting is complete. 方法二 :GetDC 與 ReleaseDC 大部份的情況下, 我們應該在 WM_PAINT 訊息下繪製圖形, 並用 BeginPaint/EndPaint 來取得 / 釋放 DC 不過在一些特殊情況下 ( 如需要立即處理繪圖或想取得繪圖裝置的一些資訊 ), 我們可以用下列的方法來取得 / 釋放 DC: hdc = GetDC(hwnd // 把 hdc 傳入 GDI 函式來進行繪圖 ReleaseDC(hwnd, hdc BeginPaint 把剪裁區域設成無效矩形區域, 而 GetDC 把剪裁區域設成整個視窗工作區, 因此你可以畫在視窗工作區的任何位置 版權所有 : 靜宜大學資訊管理學系蔡奇偉副教授 7

GetDC 函式 HDC GetDC ( HWND hwnd // handle to window The GetDC function retrieves a handle to a display device context (DC) for the client area of a specified window or for the entire screen. You can use the returned handle in subsequent GDI functions to draw in the DC. The GetDCEx function is an extension to GetDC, which gives an application more control over how and whether clipping occurs in the client area. ReleaseDC 函式 int ReleaseDC ( HWND hwnd, // handle to window HDC hdc // handle to DC The ReleaseDC function releases a device context (DC), freeing it for use by other applications. The effect of the ReleaseDC function depends on the type of DC. It frees only common and window DCs. It has no effect on class or private DCs. 版權所有 : 靜宜大學資訊管理學系蔡奇偉副教授 8

除了上述兩個常用方法外, 你還可以用下面兩個方法來取得 / 釋放 DC: // 取得代表整個視窗 ( 含標題列 選單 捲軸 工作區 ) 的 DC hdc = GetWindowDC(hwnd // 把 hdc 傳入 GDI 函式來進行繪圖 ReleaseDC(hwnd, hdc // 取得指定裝置的 DC hdc = CreateDC(pszDriver, pszdevice, pszoutput, pdata // 把 hdc 傳入 GDI 函式來進行繪圖 DeleteDC(hdc 視窗版的 Hello, world 介紹完上述的基本概念後, 我們來看視窗版的 Hello, world! 程式 這個程式絕大部分和前一講的視窗程式骨架相同, 只不過增加了一段處理 WM_PAINT 訊息的程式碼 這段程式在視窗的中間用 DrawTexT GDI 函式輸出 Hello, world! 字串 版權所有 : 靜宜大學資訊管理學系蔡奇偉副教授 9

視窗版 Hello, world!: #include <windows.h> LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ; int WINAPI WinMain (HINSTANCE hinstance, HINSTANCE hprevinstance, PSTR szcmdline, int icmdshow) { char szappname[] = "HelloWin"; HWND hwnd ; MSG msg ; WNDCLASSEX wndclass ; wndclass.cbsize = sizeof(wndclassex) ; wndclass.style = CS_HREDRAW CS_VREDRAW ; wndclass.lpfnwndproc = WndProc ; wndclass.cbclsextra = 0 ; wndclass.cbwndextra = 0 ; wndclass.hinstance = hinstance ; wndclass.hicon = LoadImage (0, IDI_APPLICATION, IMAGE_ICON, 0, 0, 0 wndclass.hcursor = LoadImage (0, IDC_ARROW, IMAGE_CURSOR, 0, 0, 0 wndclass.hbrbackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ; wndclass.lpszmenuname = NULL ; wndclass.lpszclassname = szappname ; wndclass.hiconsm = 0 ; RegisterClassEx (&wndclass hwnd = CreateWindowEx ( 0, szappname, // window class name The Hello Program", // window caption WS_OVERLAPPEDWINDOW, // window style CW_USEDEFAULT, // initial x position CW_USEDEFAULT, // initial y position CW_USEDEFAULT, // initial x size CW_USEDEFAULT, // initial y size NULL, // parent window handle NULL, // window menu handle hinstance, // program instance handle NULL) ; // creation parameters 版權所有 : 靜宜大學資訊管理學系蔡奇偉副教授 10

ShowWindow (hwnd, icmdshow) ; UpdateWindow (hwnd) ; } while (GetMessage (&msg, NULL, 0, 0)) { TranslateMessage (&msg) ; DispatchMessage (&msg) ; } return msg.wparam ; LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) { HDC hdc; PAINTSTRUCT ps; RECT rect; switch (message) { case WM_CREATE: return 0 ; case WM_DESTROY: PostQuitMessage (0) ; return 0 ; case WM_PAINT: hdc = BeginPaint(hwnd, &ps GetClientRect(hwnd, &rect DrawText(hdc, "Hello, world!", -1, &rect, DT_SINGLELINE DT_CENTER DT_VCENTER EndPaint(hwnd, &ps return 0; } return DefWindowProc (hwnd, message, wparam, lparam) ; } 版權所有 : 靜宜大學資訊管理學系蔡奇偉副教授 11

GetClientRect 函式 BOOL GetClientRect ( HWND hwnd, LPRECT lprect // handle to window // client coordinates The GetClientRect function retrieves the coordinates of a window s client area. The client coordinates specify the upperleft and lower-right corners of the client area. Because client coordinates are relative to the upper-left corner of a window's client area, the coordinates of the upper-left corner are (0,0). DrawText 函式 int DrawText ( HDC hdc, LPCTSTR lpstring, int ncount, LPRECT lprect, UINT uformat // handle to DC // text to draw // text length // formatting dimensions // text-drawing options The DrawText function draws formatted text in the specified rectangle. It formats the text according to the specified method (expanding tabs, justifying characters, breaking lines, and so forth). To specify additional formatting options, use the DrawTextEx function. 版權所有 : 靜宜大學資訊管理學系蔡奇偉副教授 12

取得裝置的功能資訊 你可以呼叫 GetDeviceCaps 函式取得繪圖裝置所具備的特性與功能 int GetDeviceCaps ( HDC hdc, // handle to DC int nindex // index of capability 細節請參考 MSDN 的線上手冊 版權所有 : 靜宜大學資訊管理學系蔡奇偉副教授 13