[VBNET] Union 應用簡例 ~

話說在論壇上看到 Union 的應用,想了一下,拿 32 bits 全彩顏色測一下,結構大概如下宣告,直接把顏色值只給此結構,就可以直接得到各色的值。簡例如下,輸出案例是拿另一顏色值測試,確認 A/R/G/B 都正常,讚啦~

<Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential, Pack:=1)> _

Structure ColorArgb

   <Runtime.InteropServices.FieldOffset(0)> Dim Argb As Integer

   <Runtime.InteropServices.FieldOffset(0)> Dim Blue As Byte

   <Runtime.InteropServices.FieldOffset(1)> Dim Green As Byte

   <Runtime.InteropServices.FieldOffset(2)> Dim Red As Byte

   <Runtime.InteropServices.FieldOffset(3)> Dim Alpha As Byte

EndStructure

Dim pointColor As myColorArgb = Color.FromArgb(255, 0, 255, 0).ToArgb

? pointColor
{vbtest.Form1.ColorArgb}
Alpha: 255
Argb: -4210689
Blue: 255
Green: 127
Red: 127

Categories: 技術分享 | 發表留言

文章導覽

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 變更 )

Twitter picture

You are commenting using your Twitter account. Log Out / 變更 )

Facebook照片

You are commenting using your Facebook account. Log Out / 變更 )

Google+ photo

You are commenting using your Google+ account. Log Out / 變更 )

連結到 %s

在WordPress.com寫網誌.

%d 位部落客按了讚: