明輝手游網(wǎng)中心:是一個(gè)免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺(tái)!

VB中使用API函數(shù)完成特殊窗體的2種方法

[摘要]---- 在VB集成開(kāi)發(fā)環(huán)境(IDE)中,設(shè)計(jì)程序時(shí)所新建、添加的窗體都是矩形的。如果出于某種需要,想讓窗體在運(yùn)行時(shí)呈現(xiàn)出特殊的形狀,就必須借助API函數(shù)編寫相應(yīng)的代碼。 ---- [方法一]使用區(qū)域創(chuàng)建函數(shù) ---- 常用的區(qū)域創(chuàng)建函數(shù)有: ---- CreateEllipticRgn ‘創(chuàng)建一...
---- 在VB集成開(kāi)發(fā)環(huán)境(IDE)中,設(shè)計(jì)程序時(shí)所新建、添加的窗體都是矩形的。如果出于某種需要,想讓窗體在運(yùn)行時(shí)呈現(xiàn)出特殊的形狀,就必須借助API函數(shù)編寫相應(yīng)的代碼。

---- [方法一]使用區(qū)域創(chuàng)建函數(shù)

---- 常用的區(qū)域創(chuàng)建函數(shù)有:

---- CreateEllipticRgn ‘創(chuàng)建一個(gè)橢圓或圓形區(qū)域

---- CreateRoundRectRgn ‘創(chuàng)建一個(gè)圓角矩形區(qū)域

---- CreatePolygonRgn ‘創(chuàng)建一個(gè)由一系列點(diǎn)圍成的區(qū)域

---- CombineRgn ‘將兩個(gè)區(qū)域組合為一個(gè)新區(qū)域

---- SetWindowRgn ‘設(shè)置新的窗口區(qū)域。

---- 通過(guò)CombineRgn可以取兩個(gè)區(qū)域的并集、交集等組合,從而創(chuàng)建出復(fù)雜形狀的窗體。

---- 例程1得到的窗體是兩個(gè)相連的月牙形:

---- ‘例程1

Option Explicit
‘API聲明
Private Declare Function CreateEllipticRgn Lib "gdi32" _
(ByVal x1 As Long,  ByVal Y1 As Long, _
  ByVal x2 As Long, ByVal Y2 As Long) As Long
Private Declare Function CombineRgn Lib "gdi32" _
(ByVal hDestRgn As Long, ByVal hSrcRgn1 As Long, _
  ByVal hSrcRgn2 As Long, ByVal nCombineMode As Long) As Long
Private Declare Function SetWindowRgn Lib "user32" _
(ByVal hWnd As Long, ByVal hRgn As Long, _
ByVal bRedraw As Boolean) As Long
‘常數(shù)聲明
Const RGN_XOR = 3

Private Sub Form_Load()
    Dim x1, x2
    x1 = CreateEllipticRgn(100, 100, 400, 400)
    x2 = CreateEllipticRgn(200, 100, 500, 400)
    CombineRgn x1, x1, x2, RGN_XOR
    SetWindowRgn hWnd, x1, 1
End Sub

---- [方法二]使用BeginPath、EndPath、TextOut、PathToRegion等函數(shù)

---- BeginPath函數(shù)調(diào)用啟動(dòng)一個(gè)路徑分支,在這個(gè)命令后執(zhí)行的GDI繪圖命令會(huì)自動(dòng)成為路徑的一部分,Windows95中合法的路徑函數(shù)有文本繪圖函數(shù)TextOut、繪制多邊形函數(shù)Polygon等。

---- EndPath函數(shù)用于結(jié)束定義一個(gè)路徑,如果調(diào)用成功,BeginPath函數(shù)和它之間發(fā)生的所有繪圖操作都將在指定設(shè)備場(chǎng)景的路徑中生效。BeginPath函數(shù)一般與EndPath函數(shù)成對(duì)出現(xiàn)。

---- PathToRegion函數(shù)調(diào)用將當(dāng)前選定的路徑轉(zhuǎn)換到指定區(qū)域中。

---- TextOut函數(shù)的聲明如下:

Declare Function TextOut Lib "gdi32" Alias
"TextOutA" (ByVal hdc As Long, ByVal x As
Long, ByVal y As Long, ByVal lpString As
String, ByVal nCount As Long) As Long

---- 參數(shù)說(shuō)明如下:

---- hdc :設(shè)備場(chǎng)景的句柄 ;

---- x,y :繪圖的起點(diǎn),采用邏輯坐標(biāo) ;

---- lpString:欲繪制的字串 ;

---- nCount:字串中要繪制的字符數(shù)量,一個(gè)漢字的字符數(shù)量為2 。

---- 例程2生成一個(gè)宋體的“國(guó)”字形的窗體:

---- ‘例程2

Option Explicit
‘類型聲明
Private Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type
‘API聲明
Private Declare Function BeginPath Lib "gdi32" _
    (ByVal hdc As Long) As Long
Private Declare Function TextOut Lib "gdi32" _
    Alias "TextOutA" (ByVal hdc As Long, _
    ByVal X As Long, ByVal Y As Long, _
    ByVal lpString As String, _
    ByVal nCount As Long) As Long
Private Declare Function EndPath Lib "gdi32" _
    (ByVal hdc As Long) As Long
Private Declare Function PathToRegion Lib "gdi32" _
    (ByVal hdc As Long) As Long
Private Declare Function GetRgnBox Lib "gdi32" _
    (ByVal hRgn As Long, lpRect As RECT) As Long
Private Declare Function CreateRectRgnIndirect Lib "gdi32" _
    (lpRect As RECT) As Long
Private Declare Function CombineRgn Lib "gdi32" _
    (ByVal hDestRgn As Long, ByVal hSrcRgn1 As Long, _
    ByVal hSrcRgn2 As Long, _
    ByVal nCombineMode As Long) As Long
Private Const RGN_AND = 1
Private Declare Function DeleteObject Lib "gdi32" _
    (ByVal hObject As Long) As Long
Private Declare Function SetWindowRgn Lib "user32" _
    (ByVal hwnd As Long, ByVal hRgn As Long, _
    ByVal bRedraw As Boolean) As Long

Private Declare Function ReleaseCapture Lib "user32" _
    () As Long
Private Declare Function SendMessage Lib "user32" _
    Alias "SendMessageA" (ByVal hwnd As Long, _
    ByVal wMsg As Long, ByVal wParam As Long, _
    lParam As Any) As Long
Private Const WM_NCLBUTTONDOWN = &HA1
Private Const HTCAPTION = 2

‘窗體代碼
Private Sub Form_Load()
    Dim hRgn1, hRgn2 As Long
    Dim rct As RECT
    With Me
      .Font.Name = "宋體"
      .Font.Size = 200
      .FontTransparent=true    
    ‘讀者可設(shè)置為False觀察其效果
    End With
BeginPath hdc               
‘為窗體形狀產(chǎn)生路徑
    TextOut hdc, 10, 10, "國(guó)", 2
    EndPath hdc
hRgn1 = PathToRegion(hdc)   
‘將指定路徑轉(zhuǎn)換為區(qū)域
GetRgnBox hRgn1, rct        
‘獲取完全包含指定區(qū)域的最小矩形
hRgn2 = CreateRectRgnIndirect(rct)
  ‘創(chuàng)建rct確定的矩形區(qū)域
    CombineRgn hRgn2, hRgn2, hRgn1, RGN_AND
DeleteObject hRgn1        
   ‘刪除GDI對(duì)象,釋放占用的系統(tǒng)資源
    SetWindowRgn hwnd, hRgn2, 1
End Sub

Private Sub Form_MouseDown
(Button As Integer, Shift _
    As Integer, X As Single, Y As Single)
    ‘移動(dòng)窗體
    ReleaseCapture
SendMessage hwnd, WM_NCLBUTTONDOWN,
HTCAPTION, 0
End Sub

Private Sub Form_DblClick()
    ‘卸載窗體
    Unload Me
End Sub

---- 如果想得到各種圖案窗體,可以將窗體字體屬性設(shè)置為Webdings、Wingdings、Wingdings1、Wingdings2、Wingdings3、Monotype Sorts等,這些字體中包含大量的圖形字符,例如,Webdings字體下,代碼“TextOut hdc, 10, 10, "J", 1”可以得到風(fēng)景畫輪廓的窗體;Windings字體下,代碼“TextOut hdc, 10, 10, "(", 1 ” 可以得到電話形狀的窗體。通過(guò)Windows附件中的字符映射表能夠方便地瀏覽或選擇、復(fù)制適用的字符。需要提醒的是,程序運(yùn)行的機(jī)器中必須裝有該字體。

---- 對(duì)比兩種方法,方法一適合于創(chuàng)建幾何形狀簡(jiǎn)單的窗體,復(fù)雜的窗體理論上雖然可以實(shí)現(xiàn),但操作起來(lái)比較困難。方法二簡(jiǎn)單易行,雖然有一定的局限性,但能夠使窗體具有各種字體中各個(gè)字符的形狀(你甚至可以利用造字程序自己“畫”一些圖形),還是很令人興奮的。

---- 創(chuàng)建特殊窗體時(shí),需要注意以下幾點(diǎn): 1.如果窗體的Borderstyle屬性沒(méi)有設(shè)置為None,即使運(yùn)行時(shí)標(biāo)題欄不可見(jiàn),但相應(yīng)的鍵盤操作如“Alt+空格鍵”、“Alt+F4”等依然有效;如果Borderstyle屬性為None,最小化、窗體移動(dòng)、退出等功能就必須編寫相應(yīng)的代碼來(lái)實(shí)現(xiàn)。當(dāng)然,使用特殊窗體制做軟件封面就不需要考慮那么多了。 2.因?yàn)闆](méi)有常規(guī)矩形窗體的立體邊框效果,窗體的背景色應(yīng)盡量采用醒目的顏色。 3.特殊窗體的使用必須得當(dāng),用的好自然錦上添花,否則給人以嘩眾取寵之感。