和其他API一樣吧,用Declare聲明后直接用
成都創(chuàng)新互聯(lián)專(zhuān)注于環(huán)江企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站開(kāi)發(fā),商城網(wǎng)站制作。環(huán)江網(wǎng)站建設(shè)公司,為環(huán)江等地區(qū)提供建站服務(wù)。全流程按需制作網(wǎng)站,專(zhuān)業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,成都創(chuàng)新互聯(lián)專(zhuān)業(yè)和態(tài)度為您提供的服務(wù)
如
Public Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
Public Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
首先,還是談?wù)剤D像像素時(shí)數(shù)據(jù)獲取方面吧,.net中的圖像相關(guān)類(lèi)基本上都是基于GDI+的,因此,圖像數(shù)據(jù)的獲取其實(shí)也是調(diào)用GDI+的一些函數(shù)。這個(gè)函數(shù)就是LockBits,在vb.net中彩色圖像數(shù)據(jù)的快速獲取 一文中,我們是調(diào)用了Marshal.Copy把LockBits鎖定的內(nèi)存數(shù)據(jù)拷貝到數(shù)據(jù)中,然后對(duì)數(shù)組中的值進(jìn)行處理。這樣做主要的原因是VB.NET不好直接訪問(wèn)內(nèi)存(Marshal.ReadByte之類(lèi)的函數(shù)不適合用于大型的循環(huán)中)。那么,這就造成了2個(gè)不好的事情,第一:在同一時(shí)間需要2倍于圖像數(shù)據(jù)量的內(nèi)存,第二:內(nèi)存數(shù)據(jù)拷貝到數(shù)據(jù),以及處理后再把數(shù)組的數(shù)據(jù)拷貝會(huì)內(nèi)存中都是會(huì)減低速度的。作為一種改進(jìn),我們應(yīng)該充分利用LockBits的功能。LockBits中的LockMode中有一種模式為ImageLockMode.UserInputBuffer,該模式下需要用戶(hù)先申請(qǐng)內(nèi)存,然后在把圖像數(shù)據(jù)按照相關(guān)格式填充如這個(gè)內(nèi)存中。這樣,就可以先定義個(gè)數(shù)組,然后把圖像數(shù)據(jù)填充到這個(gè)數(shù)組中,就避免了來(lái)回拷貝的耗時(shí)了,簡(jiǎn)單示例代碼如下:
Dim BmpData As New BitmapData
Stride = ((Bmp.Width * 3 + 3) And HFFFFFFFC)
Dim PixleValue(Stride * Bmp.Height) As Byte
Dim Hanlde As GCHandle = GCHandle.Alloc(PixleValue, GCHandleType.Pinned)
BmpData.Scan0 = Hanlde.AddrOfPinnedObject()
繪圖代碼寫(xiě)在Paint事件中,如
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Dim g As Graphics = Me.CreateGraphics
g.DrawLine(Pens.Red, 100, 100, 200, 100)
End Sub
'方法二:在 PictureBox1上顯示圖像----圖畫(huà)在Bitmap
PictureBox1.Image = Nothing
Dim wid As Integer = PictureBox1.ClientSize.Width
Dim hgt As Integer = PictureBox1.ClientSize.Height
Dim bm As New Bitmap(wid, hgt)
Dim g As Graphics = Graphics.FromImage(bm)
'畫(huà)圖代碼
'畫(huà)圖代碼
PictureBox1.Image = bm
PictureBox1.Refresh()
g.Dispose()
(1).使用DllImport特征類(lèi)來(lái)申明Windows API函數(shù):
下面是在Visual Basic .Net中使用DllImport特征類(lèi)申明二個(gè)Windows API函數(shù)的具體示例:
'函數(shù)ExtractIcon,其功能是是從指定文件的指定位置導(dǎo)出圖標(biāo)的Windows句柄。
< System.Runtime.InteropServices.DllImport ( "Shell32.dll" , EntryPoint := "ExtractIcon" ) > _
Public Function _
ExtractIcon ( ByVal src As System.IntPtr , ByVal strFileName As string , ByVal uiIconIndex As UInt32 ) As System.IntPtr
End Function
'函數(shù)Icon_Num,其功能是獲得指定文件中的圖標(biāo)數(shù)目
< System.Runtime.InteropServices.DllImport ( "Shell32.dll" , EntryPoint := "ExtractIcon" ) > _
Public Function _
Icon_Num ( ByVal src As System.IntPtr , ByVal strFileName As string , ByVal uiIconIndex As Integer ) As Integer
End Function
在使用DllImport特征類(lèi)申明Windows API函數(shù)時(shí),如果申明的函數(shù)名稱(chēng)和函數(shù)的入口點(diǎn)相同,則可以在申明Windows API函數(shù)時(shí),省略定義函數(shù)入口點(diǎn)對(duì)應(yīng)的代碼,即EntryPoint對(duì)象字段對(duì)應(yīng)的代碼,這樣聲明ExtractIcon函數(shù)的代碼也可以簡(jiǎn)化為如下所示:
< System.Runtime.InteropServices.DllImport ( "Shell32.dll" ) > _
Public Function _
ExtractIcon ( ByVal src As System.IntPtr , ByVal strFileName As string , ByVal uiIconIndex As UInt32 ) As System.IntPtr
End Function
(2).使用“Declare”語(yǔ)句來(lái)申明Windows API函數(shù):
使用“Declare”語(yǔ)句的確比使用DllImport特征類(lèi)要簡(jiǎn)單了許多,下面是在Visual Basic .Net中使用“Declare”語(yǔ)句來(lái)聲明上述二個(gè)Windows API函數(shù)的具體方法:
Declare Auto Function ExtractIcon Lib "Shell32.dll" Alias "ExtractIcon" ( ByVal src As System.IntPtr , ByVal strFileName As string , ByVal uiIconIndex As UInt32 ) As System.IntPtr
'聲明ExtractIcon函數(shù)
Declare Auto Function Icon_Num Lib "Shell32.dll" Alias "ExtractIcon" ( ByVal src As System.IntPtr , ByVal strFileName As string , ByVal uiIconIndex As Integer ) As Integer
'聲明Icon_Num函數(shù)
在Visual Basic .Net中聲明Windows API函數(shù)時(shí),“Declare”語(yǔ)句中Alias關(guān)鍵字的作用相當(dāng)于使用DllImport特征類(lèi)中的EntryPoint對(duì)象字段。同樣在使用“Declare”語(yǔ)句聲明Windows API函數(shù)時(shí),如果聲明的函數(shù)和函數(shù)的入口點(diǎn)相同,也可以省略Alias關(guān)鍵字對(duì)應(yīng)的代碼,所以ExtractIcon函數(shù)也可以簡(jiǎn)化為如下:
Declare Auto Function ExtractIcon Lib "Shell32.dll" ( ByVal src As System.IntPtr , ByVal strFileName As string , ByVal uiIconIndex As UInt32 ) As System.IntPtr
該窗體是個(gè)矩形,但是四個(gè)角都是切圓的,實(shí)現(xiàn)代碼如下:
'首先我們先創(chuàng)建一個(gè)窗體,并將窗體的BorderStyle屬性設(shè)為0
'然后聲明API函數(shù),如下:
Private Declare Function SetWindowRgn Lib "USER32" (ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long
Private Declare Function CreateRoundRectRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long, ByVal X3 As Long, ByVal Y3 As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
'我們先來(lái)了解一下這幾個(gè)函數(shù)
'函數(shù)CreateRoundRectRgn用于創(chuàng)建一個(gè)圓角矩形,該矩形由X1,Y1-X2,Y2確定,
'并由X3,Y3確定的橢圓描述圓角弧度
'CreateRoundRectRgn參數(shù) 類(lèi)型及說(shuō)明
'X1,Y1 Long,矩形左上角的X,Y坐標(biāo)
'X2,Y2 Long,矩形右下角的X,Y坐標(biāo)
'X3 Long,圓角橢圓的寬。其范圍從0(沒(méi)有圓角)到矩形寬(全圓)
'Y3 Long,圓角橢圓的高。其范圍從0(沒(méi)有圓角)到矩形高(全圓)
'SetWindowRgn用于將CreateRoundRectRgn創(chuàng)建的圓角區(qū)域賦給窗體
'DeleteObject用于將CreateRoundRectRgn創(chuàng)建的區(qū)域刪除,這是必要的,否則不必要的占用電腦內(nèi)存
'接下來(lái)聲明一個(gè)全局變量,用來(lái)獲得區(qū)域句柄,如下:
Dim outrgn As Long
'然后分別在窗體Activate()事件和Unload事件中輸入以下代碼
Private Sub Form_Activate()
Call rgnform(Me, 20, 20) '調(diào)用子過(guò)程
End Sub
Private Sub Form_Unload(Cancel As Integer)
DeleteObject outrgn '將圓角區(qū)域使用的所有系統(tǒng)資源釋放
End Sub
'接下來(lái)我們開(kāi)始編寫(xiě)子過(guò)程
Private Sub rgnform(ByVal frmbox As Form, ByVal fw As Long, ByVal fh As Long)
Dim w As Long, h As Long
w = frmbox.ScaleX(frmbox.Width, vbTwips, vbPixels)
h = frmbox.ScaleY(frmbox.Height, vbTwips, vbPixels)
outrgn = CreateRoundRectRgn(0, 0, w, h, fw, fh)
Call SetWindowRgn(frmbox.hWnd, outrgn, True)
End Sub
'好了,程序可以直接運(yùn)行了,看看效果如何,如果把Call rgnform(Me, 20, 20)中
'的20改為50又會(huì)怎樣呢?
'60呢?100呢?
'所以通過(guò)改變參數(shù)fw和fh的值就可以實(shí)現(xiàn)圓角窗體了。
網(wǎng)站題目:vb.netgdi函數(shù),net vb
分享路徑:http://vcdvsql.cn/article34/hsesse.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、面包屑導(dǎo)航、App設(shè)計(jì)、外貿(mào)建站、電子商務(wù)、服務(wù)器托管
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)