我們知道,通常WINDOWS的窗體大都是矩形的。當然也可以使用API函數(shù)制作橢圓型的、三角型的窗體,還可以制作圖片窗體。下面我給大家介紹一種制作文字、字符窗體的方法,希望能對大家有所幫助。 要想制作文字窗體就要用到WINDOWS的幾個API函數(shù),首先我們來看看這幾個函數(shù)的功能: 1、 GetPixel (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) 函數(shù) 函數(shù)功能:取得一個像素的RGB值 參數(shù):hdc ,設備場景的句柄 x,y ,邏輯坐標中的點 2: CreateRectRgn (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) 函數(shù)功能:創(chuàng)建一個由點X1,Y1和X2,Y2描述的矩形區(qū)域 參數(shù):x1,y1,矩形左上角X,Y坐標 x2,y2,矩形右下角X,Y坐標 3:Private Declare Function CombineRgn Lib "gdi32" (ByVal hDestRgn As Long, ByVal hSrcRgn1 As Long, ByVal hSrcRgn2 As Long, ByVal nCombineMode As Long) As Long 函數(shù)功能:將兩個區(qū)域組合為一個新區(qū)域 參數(shù):hDestRgn,結(jié)果區(qū)域句柄 hSrcRgn1,源區(qū)域1 hSrcRgn2,源區(qū)域2 nCombineMode,合并模式 4:Declare Function SetWindowRgn Lib "user32" Alias "SetWindowRgn" (ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long 函數(shù)功能:設置窗口的區(qū)域 參數(shù):hWnd ,窗口句柄 hRgn,將設置的區(qū)域的句柄 bRedraw ,是否立即重畫窗口
在文字窗體的設計中應當先是選定一種屏蔽色作為窗體的背景色(當然這種顏色應當是窗體 圖形中所沒有的顏色),然后利用了一個字符數(shù)組,將設計好的圖形存儲在里面,之后將圖形輸 出在窗體上。最后用GetPixel函數(shù)掃描窗體上已經(jīng)輸出字符的區(qū)域,將窗體上與屏蔽色不同的區(qū) 域標記出來,并且用CreateRectRgn函數(shù)創(chuàng)建成矩形區(qū)域,再將它們用CombineRgn函數(shù)合并成一 個區(qū)域,之后用SetWindowRgn函數(shù)設置窗體區(qū)域并生成窗體。
屬性設置:新建一個窗體(CAPTION->"制作文字窗體";NAME=FORM1;AUTOREDRAW->TRUE; SCALEMODE->1;BACKCOLOR->屏蔽色)窗體字體和前景色可根據(jù)需要設置,也可以在代碼中設置。
以下是程序源代碼: ------------- API函數(shù)聲明--------------------- Option Explicit 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 DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long Private Declare Function GetPixel Lib "gdi32" (ByVal hdc Long, ByVal X As Long, ByVal Y As Long) As Long Private Declare Function CreateRectRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 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_OR = 2 ------------- 變量------------- Dim oldx As Integer Dim oldy As Integer Dim rgn As Long Dim MaskColor As Long
Private Sub Form_Load() MaskColor=Form1.BackColor CreateForm End Sub
'-------生成文字窗體------- Public Sub CreateForm() Const max = 5 '字符行數(shù)即窗體圖形數(shù)組的最大值 Dim graphics(max) As String '存儲窗體圖形 Dim X As Long Dim Y As Long Dim count As Long '相同像素數(shù) Dim curpixels As Long '當前檢查像素 Dim temp As Long Dim textheight As Long '掃描區(qū)域的高度 Dim textwidth As Long '掃描區(qū)域的寬度 Dim i As Integer Dim maxwidth As Long '窗體的最大寬度
Me.CurrentX = 0 Me.CurrentY = 0 '-------初始化窗體圖形,可根據(jù)自己的需要設計出多種樣式------------- graphics(0) = " ★" graphics(1) = " ★★" graphics(2) = " ◎▲◎" graphics(3) = " ★★★★" graphics(4) = " ★■■■★" graphics(5) = "哈哈文字窗體 " '--------輸出窗體圖形------------ maxwidth = Me.textwidth(graphics(0)) For i = 0 To max If Me.textwidth(graphics(i)) >= maxwidth Then maxwidth = Me.textwidth(graphics(i)) End If textheight = textheight + Me.textheight(graphics(i)) '-----設置圖形顏色----------- If i Mod 2 = 0 Then Me.ForeColor = vbBlue Else Me.ForeColor = vbRed End If Me.Print graphics(i) '-----根據(jù)文字大小自動縮放窗體------ If Me.Height < textheight Then Me.Height = textheight + 500 End If If Me.Width < maxwidth Then Me.Width = maxwidth + 500 End If Next i
textheight = Int(textheight / 15) textwidth = Int(maxwidth / 15)
RGN = CreateRectRgn(0, 0, 0, 0) '創(chuàng)建空區(qū)域 For Y = 0 To textheight - 1 count = 0 For X = 0 To textwidth - 1 curpixels = GetPixel(Form1.hdc, X, Y) If X >= textwidth - 1 And count > 0 Then temp = CreateRectRgn(X + Int((Me.Width - Me.ScaleWidth) \ 30) - count, Y + Int((Me.Height - Me.ScaleHeight) \ 15) - Int((Me.Width - Me.ScaleWidth) \ 30), X + Int((Me.Width - Me.ScaleWidth) \ 30), Y + Int((Me.Height - Me.ScaleHeight) \ 15) - Int((Me.Width - Me.ScaleWidth) \ 30) + 1) '創(chuàng)建區(qū)域 CombineRgn RGN, RGN, temp, RGN_OR '合并兩個區(qū)域 DeleteObject temp End If If curpixels <> MaskColor Then count = count + 1 Else If count > 0 Then temp = CreateRectRgn(X + Int((Me.Width - Me.ScaleWidth) \ 30) - count, Y + Int((Me.Height - Me.ScaleHeight) \ 15) - Int((Me.Width - Me.ScaleWidth) \ 30), X + Int((Me.Width - Me.ScaleWidth) \ 30), Y + Int((Me.Height - Me.ScaleHeight) \ 15) - Int((Me.Width - Me.ScaleWidth) \ 30) + 1) CombineRgn RGN, RGN, temp, RGN_OR DeleteObject temp End If count = 0 End If Next X Next Y SetWindowRgn Me.hwnd, RGN, True '設置窗體區(qū)域 End Sub
Private Sub Form_DblClick() End End Sub
'-----移動窗體代碼------
Private Sub form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) If Button = 1 Then oldx = X oldy = Y End If End Sub
Private Sub form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) If Button = 1 Then If X <> oldx Or Y <> oldy Then Form1.Left = Form1.Left + (X - oldx) Form1.Top = Form1.Top + (Y - oldy) End If End If End Sub
|