C# WinForm 中在窗口標題欄上加按鈕
2024-07-21 02:17:33
供稿:網(wǎng)友
在窗口標題欄上加按鈕本來不是什么新鮮事了,我在vc++下早也實現(xiàn)過了(相信很多人也都實現(xiàn)過了)。今天一個朋友問我c# winform下可否實現(xiàn),我就順便拿c#寫了一個。
原理是一樣的,都是重寫窗口過程(wndproc),處理一些非客戶區(qū)消息(wm_ncxxxx),可以說本來沒有什么新意,可是從寫這個程序的過程中,我也學(xué)到了兩個技巧:
1)、c#中重寫窗口過程不用再調(diào)用setwindowlong api了,直接overide一個wndproc就可以了。
2)、windows api中的hdc可以通過graphics.fromhdc()轉(zhuǎn)換為(創(chuàng)建出)system.drawing.graphics,然后就可以用.net framework (gid+??)提供的繪圖功能方便地進行畫圖了。終于可以拋開討厭的gdi api了(說實在話,在c#中調(diào)用windows api真的太麻煩了:)).
代碼如下:
using system;using system.drawing;using system.drawing.drawing2d;using system.collections;using system.componentmodel;using system.windows.forms;using system.data;using system.runtime.interopservices;using system.diagnostics; namespace windowsapplication2{ /// <summary> /// form1 的摘要說明。 /// </summary> public class form1 : system.windows.forms.form { /// <summary> /// 必需的設(shè)計器變量。 /// </summary> private system.componentmodel.container components = null; public form1() { // // windows 窗體設(shè)計器支持所必需的 // initializecomponent(); // // todo: 在 initializecomponent 調(diào)用后添加任何構(gòu)造函數(shù)代碼 // } /// <summary> /// 清理所有正在使用的資源。 /// </summary> protected override void dispose( bool disposing ) { if( disposing ) { if (components != null) { components.dispose(); } } base.dispose( disposing ); } #region windows 窗體設(shè)計器生成的代碼 /// <summary> /// 設(shè)計器支持所需的方法 - 不要使用代碼編輯器修改 /// 此方法的內(nèi)容。 /// </summary> private void initializecomponent() { // // form1 // this.autoscalebasesize = new system.drawing.size(6, 14); this.clientsize = new system.drawing.size(292, 266); this.name = "form1"; this.text = "form1"; this.sizechanged += new system.eventhandler(this.form1_sizechanged); } #endregion /// <summary> /// 應(yīng)用程序的主入口點。 /// </summary> [stathread] static void main() { application.run(new form1()); } [dllimport ("user32.dll")] private static extern intptr getwindowdc(intptr hwnd); [dllimport ("user32.dll")] private static extern int releasedc(intptr hwnd, intptr hdc); [dllimport ("kernel32.dll")] private static extern int getlasterror(); //標題欄按鈕的矩形區(qū)域。 rectangle m_rect = new rectangle(205, 6, 20, 20); protected override void wndproc(ref message m) { base.wndproc(ref m); switch(m.msg) { case 0x86://wm_ncactivate goto case 0x85; case 0x85://wm_ncpaint { intptr hdc = getwindowdc(m.hwnd); //把dc轉(zhuǎn)換為.net的graphics就可以很方便地使用framework提供的繪圖功能了 graphics gs = graphics.fromhdc(hdc); gs.fillrectangle(new lineargradientbrush(m_rect, color.pink, color.purple, lineargradientmode.backwarddiagonal), m_rect); stringformat strfmt = new stringformat(); strfmt.alignment = stringalignment.center; strfmt.linealignment = stringalignment.center; gs.drawstring("√", this.font, brushes.blanchedalmond, m_rect, strfmt); gs.dispose(); //釋放gdi資源 releasedc(m.hwnd, hdc); break; } case 0xa1://wm_nclbuttondown { point mousepoint = new point((int)m.lparam); mousepoint.offset(-this.left, -this.top); if(m_rect.contains(mousepoint)) { messagebox.show("hello"); } break; } } } //在窗口大小改變時及時更新按鈕的區(qū)域。 private void form1_sizechanged(object sender, system.eventargs e) { m_rect.x = this.bounds.width - 95; m_rect.y = 6; m_rect.width = m_rect.height = 20; } }}