VB中利用API函數實現特殊窗體的兩種方法

發表于:2007-07-14來源:作者:點擊數: 標簽:
---- 在 VB 集成 開發 環境(IDE)中,設計程序時所新建、添加的窗體都是矩形的。如果出于某種需要,想讓窗體在運行時呈現出特殊的形狀,就必須借助API函數編寫相應的代碼。 ---- [方法一]使用區域創建函數 ---- 常用的區域創建函數有: ---- CreateElliptic
---- 在VB集成開發環境(IDE)中,設計程序時所新建、添加的窗體都是矩形的。如果出于某種需要,想讓窗體在運行時呈現出特殊的形狀,就必須借助API函數編寫相應的代碼。

---- [方法一]使用區域創建函數

---- 常用的區域創建函數有:

---- CreateEllipticRgn ‘創建一個橢圓或圓形區域

---- CreateRoundRectRgn ‘創建一個圓角矩形區域

---- CreatePolygonRgn ‘創建一個由一系列點圍成的區域

---- CombineRgn ‘將兩個區域組合為一個新區域

---- SetWindowRgn ‘設置新的窗口區域。

---- 通過CombineRgn可以取兩個區域的并集、交集等組合,從而創建出復雜形狀的窗體。

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

---- ‘例程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
‘常數聲明
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等函數

---- BeginPath函數調用啟動一個路徑分支,在這個命令后執行的GDI繪圖命令會自動成為路徑的一部分,Windows95中合法的路徑函數有文本繪圖函數TextOut、繪制多邊形函數Polygon等。

---- EndPath函數用于結束定義一個路徑,如果調用成功,BeginPath函數和它之間發生的所有繪圖操作都將在指定設備場景的路徑中生效。BeginPath函數一般與EndPath函數成對出現。

---- PathToRegion函數調用將當前選定的路徑轉換到指定區域中。

---- TextOut函數的聲明如下:

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

---- 參數說明如下:

---- hdc :設備場景的句柄 ;

---- x,y :繪圖的起點,采用邏輯坐標 ;

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

---- nCount:字串中要繪制的字符數量,一個漢字的字符數量為2 。

---- 例程2生成一個宋體的“國”字形的窗體:

---- ‘例程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    
    ‘讀者可設置為False觀察其效果
    End With
BeginPath hdc               
‘為窗體形狀產生路徑
    TextOut hdc, 10, 10, "國", 2
    EndPath hdc
hRgn1 = PathToRegion(hdc)   
‘將指定路徑轉換為區域
GetRgnBox hRgn1, rct        
‘獲取完全包含指定區域的最小矩形
hRgn2 = CreateRectRgnIndirect(rct)
  ‘創建rct確定的矩形區域
    CombineRgn hRgn2, hRgn2, hRgn1, RGN_AND
DeleteObject hRgn1        
   ‘刪除GDI對象,釋放占用的系統資源
    SetWindowRgn hwnd, hRgn2, 1
End Sub

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

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

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

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

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

原文轉自:http://www.anti-gravitydesign.com

国产97人人超碰caoprom_尤物国产在线一区手机播放_精品国产一区二区三_色天使久久综合给合久久97