Transcript
1/82
2/82
Windows 7 程式開發入門使用 Windows API Code Pack for
Microsoft .NET Framework
Windows 7 程式開發入門使用 Windows API Code Pack for
Microsoft .NET Framework
曹祖聖台灣微軟資深講師 jimycao@syset.com http://teacher.syset.com MCP, MCP+I, MCSA, MCSE, MCDBA, MCAD, MCSD, MCTS, MCITP, MCT, MVP
曹祖聖台灣微軟資深講師 jimycao@syset.com http://teacher.syset.com MCP, MCP+I, MCSA, MCSE, MCDBA, MCAD, MCSD, MCTS, MCITP, MCT, MVP
3/82
大綱 工作列程式設計 ExplorerBrowser 控制項與通用檔案對話方塊 媒體櫃與搜尋連接器設計 如何用 .NET 程式控制 Windows 7 電源管
理 繪圖 -- Direct2D 、 DirectWrite 應用程式如何支援重新啓動與自我修復功能
4/82
大綱 工作列程式設計 ExplorerBrowser 控制項與通用檔案對話方塊 媒體櫃與搜尋連接器設計 如何用 .NET 程式控制 Windows 7 電源管
理 繪圖 -- Direct2D 、 DirectWrite 應用程式如何支援重新啓動與自我修復功能
5/82
客製化工作列圖示
6/82
工作列進度與圖示顯示// 名稱空間 : Microsoft.WindowsAPICodePack.Shell.Taskbar
Taskbar.ProgressBar.CurrentValue = 10; // 0 ~ 100
Taskbar.ProgressBar.State = TaskbarButtonProgressState.Normal;
// NoProgress ( 不顯示進度 )// Indeterminate ( 跑馬燈方式 )// Normal ( 綠色正常顯示 )// Error ( 顯示紅色 )// Paused ( 暫停進度 )
Taskbar.OverlayImage = new OverlayImage(new Icon("demo.ico"), " 說明文字
“ )
7/82
工作列進度與圖示顯示工作列進度與圖示顯示
8/82
Jump Lists小型的開始功能表
目標
工作
已知類別
自訂類別使用者工作
工作列工作
固定類別
9/82
加入自訂工作string taskPath = "c:\windows\\system32\\notepad.exe";
Taskbar.JumpList.UserTasks.Add( new JumpListLink { Title = " 開啓記事本 ", Path = taskPath, IconReference = new IconReference(taskPath, 0) });
Taskbar.JumpList.RefreshTaskbarList();
10/82
加入自訂類別CustomCategory category1 = new CustomCategory(" 分類 #1");CustomCategory category2 = new CustomCategory(" 分類 #2");
Taskbar.JumpList.CustomCategories.Add(category1);Taskbar.JumpList.CustomCategories.Add(category2);
11/82
在自訂類別中加入自訂連結category1.JumpListItems.Add( new JumpListLink() { Title = " 開啓記事本 ", Path = taskPath, IconReference = new IconReference(taskPath, 0) });
Taskbar.JumpList.RefreshTaskbarList();
12/82
在自訂類別中加入自訂項目 在執行以下程式之前,必須要在 Registry 中
註冊程式與副檔名的對應
category2.JumpListItems.Add(new JumpListItem("c:\\data\\demo.txt")
);
Taskbar.JumpList.RefreshTaskbarList();
13/82
註冊程式與副檔名的對應
14/82
工作列文件與工作清單工作列文件與工作清單
15/82
即時視窗預覽縮圖 Windows Vista: 每 個視窗一個 Windows 7: 使用縮圖群組
16/82
明確顯示部份內容 與其顯示整個畫面,但是卻不清楚 不如只顯示其中最重要的部份
17/82
設定工作列縮圖
// 設定縮圖來源區塊Taskbar.TabbedThumbnail.SetThumbnailClip(
this.Handle, new Rectangle(
pictureBox1.Location, pictureBox1.Size)
);
18/82
工作列縮圖工作列縮圖
19/82
MDI / TDI 視窗切換器
20/82
設定分頁縮圖void UpdatePreviewBitmap(TabPage tabPage) { if (tabPage != null) { TabbedThumbnailPreview preview = Taskbar.TabbedThumbnail.GetThumbnailPreview(tabPage);
if (preview != null) { Bitmap bitmap = ScreenCapture.GrabWindowBitmap( tabPage.Handle, tabPage.Size); preview.SetImage(bitmap); } }}
21/82
分頁事件 Taskbar.TabbedThumbnail 的事件
TabbedThumbnailClosed TabbedThumbnailActivated TabbedThumbnailMaximized TabbedThumbnailMinimized
Taskbar.TabbedThumbnail.TabbedThumbnailClosed += new EventHandler<TabbedThumbnailEventArgs>(
TabbedThumbnail_TabbedThumbnailClosed);
22/82
縮圖工具列 允許直接在工作列上操作應用程式
23/82
定義縮圖工具列按鈕ThumbnailButton thumbButtonBack =
new ThumbnailButton(Properties.Resources.prevArrow, "Back“
);
thumbButtonForward.Click += new EventHandler(thumbButtonForward_Click);
Taskbar.ThumbnailToolbars.AddButtons(newTab.Handle, thumbButtonBack, thumbButtonForward, thumbButtonRefresh
);
24/82
分頁縮圖與縮圖工具列分頁縮圖與縮圖工具列
25/82
大綱 工作列程式設計 ExplorerBrowser 控制項與通用檔案對話方塊 媒體櫃與搜尋連接器設計 如何用 .NET 程式控制 Windows 7 電源管
理 繪圖 -- Direct2D 、 DirectWrite 應用程式如何支援重新啓動與自我修復功能
26/82
ExplorerBrowser 控制項
explorerBrowser1.Navigate(ShellFolder.FromParsingName("C:\\Windows\\System32")
);
27/82
KnownFolder
// Windows FormexplorerBrowser1.Navigate(
KnownFolders.Games as ShellObject );
// WPFexplorerBrowser1.NavigationTarget =
KnownFolders.Games as ShellObject;
28/82
ExplorerBrowser 控制項
CommonOpenFileDialog dialog = new CommonOpenFileDialog();dialog.FoldersOnly = true;if (dialog.ShowDialog().Canceled == false) txtPath.Text = ((ShellFolder)dialog.Item).ParsingName;
29/82
ExplorerBrowser 與新檔案對話方塊ExplorerBrowser – WinForm
ExplorerBrowser – WPF已知資料夾瀏覽
樹狀瀏覽
ExplorerBrowser 與新檔案對話方塊ExplorerBrowser – WinForm
ExplorerBrowser – WPF已知資料夾瀏覽
樹狀瀏覽
30/82
取得檔案屬性
IShellProperty prop = ShellObject.FromParsingName(fileName) .Properties.GetProperty(propertyName);
string value = prop.ValueAsObject == null ? "" : prop.FormatForDisplay(PropertyDescriptionFormat.Default);
Console.WriteLine("{0} = {1}", prop.CanonicalName, value);
31/82
設定檔案屬性
IShellProperty prop = ShellObject.FromParsingName(fileName) .Properties.GetProperty(propertyName);
if (prop.ValueType == typeof(short?)){ (prop as ShellProperty<short?>).Value = short.Parse(value);}
32/82
檔案屬性編輯檔案屬性編輯
33/82
大綱 工作列程式設計 ExplorerBrowser 控制項與通用檔案對話方塊 媒體櫃與搜尋連接器設計 如何用 .NET 程式控制 Windows 7 電源管
理 繪圖 -- Direct2D 、 DirectWrite 應用程式如何支援重新啓動與自我修復功能
34/82
媒體櫃概觀 在之前的 Windows 中,使用者將檔案儲存
在已知路徑的資料夾中, 文件夾、…
至少有兩個資料夾路徑 個人資料夾 公用資料夾
35/82
媒體櫃的定義
“ 媒體櫃是一個集合、集中包含了來自不同位置的文件,使用者可以透過媒體櫃來組織尋找文件。”
36/82
媒體櫃管理介面媒體櫃管理介面
37/82
媒體櫃定義檔 XML 檔案格式 副檔名為 .library-ms 位於以下路徑中
C:\Users\[UserName]\AppData\Roaming\Microsoft\Windows\Libraries
38/82
39/82
建立媒體櫃// 建立新的媒體櫃ShellLibrary library = new ShellLibrary("Hello", true);library.Add(@"D:\MP3");library.Add(@"D:\Source");library.Close();
// 編輯媒體櫃ShellLibrary lib = ShellLibrary.Load("Hello", false);lib.Remove(@"D:\MP3");lib.Add(@"D:\Drivers");lib.Close();
40/82
媒體櫃選取媒體櫃選取
41/82
搜尋連接器跨多重來源的搜尋
SharePoint 文件庫
企業資料來源
企業應用程式
多重搜尋來源
42/82
搜尋連接器運作機制 豐富的搜尋經驗
預覽、 metadata 、拖曳 檔案總管的操作方式
簡單、輕量化整合 不需要安裝任何用戶端程式碼 支援任何平台
支援相關標準 OpenSearch 1.1 RSS, ATOM
身份驗證 整合式、自訂
1. 安裝搜尋連接器 (.osdx)
2. Windows 透過 HTTP 傳送搜尋要求
3. 伺服器傳回 RSS
43/82
Open Search .osdx 檔格式
<?xml version="1.0" encoding="UTF-8"?><OpenSearchDescription xmlns="http://a9.com/- /spec/opensearch/1.1/" xmlns:ms-ose= "http://schemas.microsoft.com/opensearchext/2009/"> <ShortName>Technet</ShortName> <Description>OpenSearch for Technet</Description> <Url type="application/rss+xml“ template="http://social.technet.microsoft.com/Search/ Feed.aspx? Query={searchTerms}"/></OpenSearchDescription>
44/82
搜尋連接器搜尋連接器
45/82
大綱 工作列程式設計 ExplorerBrowser 控制項與通用檔案對話方塊 媒體櫃與搜尋連接器設計 如何用 .NET 程式控制 Windows 7 電源管
理 繪圖 -- Direct2D 、 DirectWrite 應用程式如何支援重新啓動與自我修復功能
46/82
節省 CPU 電力電力狀態讀取 避免讀取電腦電力狀態 – 使用事件 每秒鐘不取得超過一次的電源狀態 不直接取得電源狀態
RegisterPowerSettingNotification 使用聯合計數器 (coalescing timers)
呼叫 SetWaitableTimerEx ( 含 delay tolerance 參數 )
計數器週期15.6 ms
週期性計數器事件
Windows 7
Vista
47/82
電力設定通知
PowerManager.PowerSourceChanged += delegate { // 使用 PowerManager.PowerSource };
/* 其它 PowerManager 方法 / 屬性 : PowerManager.IsBatteryPresent PowerManager.BatteryLifePercent … 等等 */
48/82
聯合計數器 (Coalescing Timer)
CoalescingTimer t = new CoalescingTimer("Name");t.Tick += delegate { ... };
//Due time, period, reason, delayt.Set(0, 1000, "Reason", 50);
49/82
節省 CPU 電力多媒體計數器 多媒體計數器可以透過修改計數器間格來達到更好的效能 在多處理器上預設是 : 15.6ms 視需要調整,可以調到 1ms 、甚至更小
建議不要低於 10ms 否則會嚴重耗損電力 尤其是使用電池供電時
50/82
節省 CPU 電力 動畫處理 動畫會加重 CPU 和 GPU 的電力使用 在系統閒置時避免動畫處理
通知區域的圖示動畫 Flash 撥放器就算最小化,一樣會使用 CPU
當電源計劃啓用、而且是使用電池供電時停用所有動畫
51/82
節省硬碟電力 空轉的硬碟會浪費將近 8% 的電力 在系統閒置時
避免週期性的磁碟存取 避免系統登錄 (registry) 的存取
最佳範例 Office Word 2007 每隔 5~15 分鐘自動存檔一次 如果文件沒有任何變動,就不會進行儲存
52/82
背景活動 將工作使用背景服務來處理 背景活動 ( 工作、服務 ) 可以依照不同環境進行排程 有 AC 電源時才啓動工作 檔使用電池時就暫停工作 有需要服務時才去啓動服務
請參閱 “ Background Services” 投影片
53/82
不要防礙電源轉移 如果使用者觸發睡眠模式
任何程式都不能阻止進入睡眠模式 停止任何硬體動作 (網路傳輸、檔案複製、
… ) 從睡眠模式還原時 :
恢復相關變動 (網路 ) 所有應用程式同時恢復 ( 可能會有資源瓶頸 )
在系統啓動與關機時,使用相同的準則 包含休眠 / 復原
54/82
電力可用性要求 應用程式可以延遲自動睡眠
例如 : 正在下載檔案、錄製電視節目、燒錄光碟
呼叫舊的 API: SetThreadExecutionState
Windows 7 使用新的 API: PowerCreateRequest
參數中多了延遲的原因 IT 人員可以透過以下指令取得相關資訊 :
powercfg.exe /requests
55/82
電力可用性要求 Managed Code
PowerAvailability.CreateRequest( "My Reason", PowerRequestType.DisplayRequired);
56/82
電力使用效率診斷 Windows 效能計數器 Windows Performance Toolkit (xperf) 在 Windows 7 中可以產生電力使用效率報
表 powercfg.exe /energy 系統閒置時會自動執行,並回報給微軟 CEIP
(Customer Experience Improvement Program)
57/82
電源管理電源管理
58/82
大綱 工作列程式設計 ExplorerBrowser 控制項與通用檔案對話方塊 媒體櫃與搜尋連接器設計 如何用 .NET 程式控制 Windows 7 電源管
理 繪圖 -- Direct2D 、 DirectWrite 應用程式如何支援重新啓動與自我修復功能
59/82
Windows 7 繪圖平台
Frameworks
原生平台
Applications
Windows Paint 3rd party
Ribbon Animation
Taskbar
WPF
60/82
Windows 7 Graphic Platform Overview
DXGKernel
DXGI
Win32K
GDI+
USER
Direct3D 11Direct3D 10
DWMDirect2D
DirectWrite
GDI
WIC
DXVA
Direct3D9
Windows Vista
Windows 7 新增
61/82
Direct2D 架構
Direct3D
DXGI
Direct2D
Application Application
Software rasterizer
62/82
Windows 7 繪圖 API 整合
//DX CodeCreateRenderTarget()Draw()Present()
// GDI CodeGetDC()DrawLine()ReleaseDC()
Windows Vista Windows 7
Copy 1
Copy 2
63/82
Direct2D 與 Direct3D 整合 使用 Direct3D 10 或 10Level9 裝置 對應 D3D 平面到 D2D render target
CreateDxgiSurfaceRenderTarget() 在呼叫之前 Present() 呼叫 EndDraw()
在任何硬體組態設定下都可以運作 最好使用 WDDM 1.1 驅動程式和 DX10
GPU
64/82
Direct2D 繪圖D2DFactory d2dFactory = D2DFactory.CreateFactory(D2DFactoryType.SingleThreaded);
SizeU size = new SizeU((uint)renderControl.ClientSize.Width, (uint)renderControl.ClientSize.Height);RenderTargetProperties props = new RenderTargetProperties();HwndRenderTargetProperties hwndProps = new
HwndRenderTargetProperties(renderControl.Handle, size,
PresentOptions.None);HwndRenderTarget renderTarget =
d2dFactory.CreateHwndRenderTarget(props, hwndProps);
65/82
Direct2D 繪圖Point2F p1 = new Point2F(10, 10);Point2F p2 = new Point2F(400, 300);Brush b = renderTarget.CreateSolidColorBrush(
new ColorF(0, 0, 0, 1));
renderTarget.BeginDraw();renderTarget.Clear(new ColorF(1, 1, 1, 0));renderTarget.DrawLine(p1, p2, b, 2);renderTarget.EndDraw();
66/82
Text Positioning Can start glyphs at a sub-pixel boundary
在字母之間 Enables more even spacing between glyphs Smoother scrolling and text rotation
67/82
ClearType在 GDI 中 ClearType 只能用在 X 方向
DirectWrite 可以同時用在 X 和 Y 兩個方向
68/82
DirectWrite
DWriteFactory dwriteFactory = DWriteFactory.CreateFactory();
SizeU size = new SizeU((uint)renderControl.ClientSize.Width, (uint)renderControl.ClientSize.Height);RenderTargetProperties props = new RenderTargetProperties();HwndRenderTargetProperties hwndProps = new
HwndRenderTargetProperties(renderControl.Handle, size,
PresentOptions.None);HwndRenderTarget renderTarget =
d2dFactory.CreateHwndRenderTarget(props, hwndProps);
69/82
DirectWrite
TextFormat textFormat = dwriteFactory.CreateTextFormat( “Arial”, 24, FontWeight.Bold, FontStyle.Italic, FontStretch.Normal, "");TextLayout textLayout = dwriteFactory.CreateTextLayout(
“Hello”, textFormat, 100, 100);Point2F p = new Point2F(10, 10);Brush b = renderTarget.CreateSolidColorBrush(
new ColorF(0, 0, 0, 1));
renderTarget.BeginDraw();renderTarget.Clear(new ColorF(1, 1, 1, 0));renderTarget.DrawTextLayout(p, textLayout, b);renderTarget.EndDraw();
70/82
Direct2D & DirectWriteDirect2D & DirectWrite
71/82
大綱 工作列程式設計 ExplorerBrowser 控制項與通用檔案對話方塊 媒體櫃與搜尋連接器設計 如何用 .NET 程式控制 Windows 7 電源管
理 繪圖 -- Direct2D 、 DirectWrite 應用程式如何支援重新啓動與自我修復功能
72/82
重新啓動與自我修復 當應用程式當掉時, Windows 7 的 WER
(Windows Error Reporting Service) 會進行錯誤回報,並檢查可用的解決方案
72 / 36
73/82
重新啓動與自我修復 應用程式可以事先向 WER 註冊,以便由
WER 來重新啓動應用程式
73 / 36
74/82
註冊自我修復private void RegisterForRecovery(){ // 設定應用程式當掉之前, // WER 要呼叫 RecoveryProcedure RecoveryData data = new RecoveryData(
new RecoveryCallback(RecoveryProcedure), null);
RecoverySettings settings = new RecoverySettings(data, 0);
ApplicationRestartRecoveryManager.RegisterForApplicationRecovery(settings);
}
75/82
註冊自我修復private int RecoveryProcedure(object state){ PingSystem();
// 儲存將來要復原用的資料 // ….
ApplicationRestartRecoveryManager. ApplicationRecoveryFinished(true); return 0;}
76/82
註冊自我修復private void PingSystem(){ // 檢查使用者是否取消重新啓動 bool isCanceled = ApplicationRestartRecoveryManager.
ApplicationRecoveryInProgress();
if (isCanceled) { Environment.Exit(2); }}
77/82
註冊重新啓動 讓 WER 知道要帶什麼參數來重新啓動應
用程式,例如 /restart 指定例外狀況,例如重新開機、應用程
式修正時不要重新啓動應用程式
ApplicationRestartRecoveryManager.RegisterForApplicationRestart(
new RestartSettings("/restart", RestartRestrictions.NotOnReboot |
RestartRestrictions.NotOnPatch));
78/82
註冊重新啓動 應用程式一開始必須檢查是否有帶
/restart 參數,如果有代表是 WER 呼叫 進行修復動作,例如將當掉之前的檔案載入
if (System.Environment.GetCommandLineArgs().Length > 1 && System.Environment.GetCommandLineArgs()[1] == "/restart"){ RecoverLastSession( System.Environment.GetCommandLineArgs()[1] );}
79/82
重新啓動與自我修復重新啓動與自我修復
80/82
總結總結 Windows API Code Pack for Microsoft .NET Framework
簡化 .NET 應用程式與 Windows 7 新功能整合的工作 底層仍是 COM 元件或作業系統 API 呼叫 應用程式要自行檢查目前的環境是不是 Windows 7
預告 – Tech Days 2009 9/22~9/24 Windows 7 多點觸控應用程式設計入門 新世代的 Windows 與 Web 應用程式開發 --
WPF 4.0
Windows API Code Pack for Microsoft .NET Framework 簡化 .NET 應用程式與 Windows 7 新功能整合的工作 底層仍是 COM 元件或作業系統 API 呼叫 應用程式要自行檢查目前的環境是不是 Windows 7
預告 – Tech Days 2009 9/22~9/24 Windows 7 多點觸控應用程式設計入門 新世代的 Windows 與 Web 應用程式開發 --
WPF 4.0
81/82
相關資訊相關資訊 聖哥的資訊站
http://teacher.syset.com
MSDN http://www.microsoft.com/msdn http://www.microsoft.com/taiwan/msdn
Windows 7 API Code Pack http://code.msdn.microsoft.com/WindowsAPICodePack
聖哥的資訊站 http://teacher.syset.com
MSDN http://www.microsoft.com/msdn http://www.microsoft.com/taiwan/msdn
Windows 7 API Code Pack http://code.msdn.microsoft.com/WindowsAPICodePack
82/82© 2001 Microsoft Corporation. All rights reserved.
top related