走近VB.Net(三) 源码详解—运用颜色的初步探讨

类别:.NET开发 点击:0 评论:0 推荐:

 

走近VB.Net() 源码详解运用颜色的初步探讨

 新建一个工程,加入以下控件

1.      label控件:label1,label2,labred ,labblue,labgreen
2.      picturebox控件:picturebox1
3.  button控件:Button1
设置form1的opacity属性为80%,设置透明的窗体
office中抓取一幅取色图片存为bmp格式,并设为picturebox1的backgroundimage,即背景图片
Option Strict Off '关闭 strict off
Imports System.ComponentModel '引用族名
Imports System.Drawing '引用族名
Imports System.WinForms '引用族名
'下面的例程我将写出全部的族名与子类,是避免大家看得不明白,如果明白了意思,就可以使用基于省略的方式
 
Public Class Form1
    Inherits System.WinForms.Form '从System.WinForms.Form类引用到form1
    Public pixX, pixY As Integer '声明两个32位的变量,用public定义他在类中所有成员间共享
    Public Apr As Boolean = False '声明两个32位的变量,用public定义他在类中所有成员间共享并初始化他的值为false
    Public Sub New() '新建一个窗体
        MyBase.New() '调用
        Form1 = Me '定义me关键字的当前值为form1
        'This call is required by the Win Form Designer.
        InitializeComponent() '在这个下面写的代码等同于VB6的form_ load中所写的代码
        Call me_load() '你可以写一个子过程,在这里调用他,当然你完全可以写成别的过程名如me_beginform
        'TODO: Add any initialization after the InitializeComponent() call
    End Sub
    
    'Form overrides dispose to clean up the component list.
    Public Overrides Sub Dispose() '在这个下面写的代码等同于VB6的form_ unload中所写的代码
        MyBase.Dispose() '调用
        components.Dispose() 'unload组件
    End Sub
    
    
下面一段#Region " Windows Form Designer generated code "是不必看的,
我全部写在这里是为了您可以直接粘贴运行
#Region " Windows Form Designer generated code "
    
    'Required by the Windows Form Designer
    Private components As System.ComponentModel.Container
    
    Private WithEvents Label2 As System.WinForms.Label
    
    
    Private WithEvents Lab4 As System.WinForms.Label
    Private WithEvents Lab3 As System.WinForms.Label
    Private WithEvents Lab2 As System.WinForms.Label
    Private WithEvents LBblue As System.WinForms.Label
    Private WithEvents LBgreen As System.WinForms.Label
    Private WithEvents LBred As System.WinForms.Label
    
    Private WithEvents Button1 As System.WinForms.Button
    Private WithEvents Label1 As System.WinForms.Label
    Private WithEvents PictureBox1 As System.WinForms.PictureBox
    
    Dim WithEvents Form1 As System.WinForms.Form
    
    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Private Sub InitializeComponent()
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
        
        Me.components = New System.ComponentModel.Container()
        Me.LBblue = New System.WinForms.Label()
        Me.Lab2 = New System.WinForms.Label()
        Me.Label2 = New System.WinForms.Label()
        Me.Label1 = New System.WinForms.Label()
        Me.Lab3 = New System.WinForms.Label()
        Me.LBgreen = New System.WinForms.Label()
        Me.LBred = New System.WinForms.Label()
        Me.Lab4 = New System.WinForms.Label()
        Me.PictureBox1 = New System.WinForms.PictureBox()
        Me.Button1 = New System.WinForms.Button()
        
        '@design Me.TrayHeight = 0
        '@design Me.TrayLargeIcon = False
        '@design Me.TrayAutoArrange = True
        LBblue.Location = New System.Drawing.Point(216, 200)
        LBblue.Size = New System.Drawing.Size(112, 32)
        LBblue.BorderStyle = System.WinForms.BorderStyle.Fixed3D
        LBblue.TabIndex = 6
        LBblue.BackColor = System.Drawing.Color.Blue
        
        Lab2.Location = New System.Drawing.Point(8, 176)
        Lab2.Text = "R="
        Lab2.Size = New System.Drawing.Size(100, 23)
        Lab2.ForeColor = System.Drawing.SystemColors.ActiveCaptionText
        Lab2.Font = New System.Drawing.Font("宋体", 12!, System.Drawing.FontStyle.Bold)
        Lab2.TabIndex = 7
        
        Label2.Location = New System.Drawing.Point(248, 8)
        Label2.Size = New System.Drawing.Size(48, 96)
        Label2.BorderStyle = System.WinForms.BorderStyle.Fixed3D
        Label2.TabIndex = 10
        
        Label1.Location = New System.Drawing.Point(192, 80)
        Label1.Size = New System.Drawing.Size(48, 88)
        Label1.BorderStyle = System.WinForms.BorderStyle.Fixed3D
        Label1.TabIndex = 1
        
        Lab3.Location = New System.Drawing.Point(112, 176)
        Lab3.Text = "G="
        Lab3.Size = New System.Drawing.Size(104, 24)
        Lab3.ForeColor = System.Drawing.SystemColors.ActiveCaptionText
        Lab3.Font = New System.Drawing.Font("宋体", 12!, System.Drawing.FontStyle.Bold)
        Lab3.TabIndex = 8
        
        LBgreen.Location = New System.Drawing.Point(112, 200)
        LBgreen.Size = New System.Drawing.Size(104, 32)
        LBgreen.BorderStyle = System.WinForms.BorderStyle.Fixed3D
        LBgreen.TabIndex = 5
        LBgreen.BackColor = System.Drawing.Color.Lime
        
        LBred.Location = New System.Drawing.Point(8, 200)
        LBred.Size = New System.Drawing.Size(104, 32)
        LBred.BorderStyle = System.WinForms.BorderStyle.Fixed3D
        LBred.TabIndex = 4
        LBred.BackColor = System.Drawing.Color.Red
        
        Lab4.Location = New System.Drawing.Point(216, 176)
        Lab4.Text = "B="
        Lab4.Size = New System.Drawing.Size(112, 24)
        Lab4.ForeColor = System.Drawing.SystemColors.ActiveCaptionText
        Lab4.Font = New System.Drawing.Font("宋体", 12!, System.Drawing.FontStyle.Bold)
        Lab4.TabIndex = 9
        
        PictureBox1.Cursor = System.Drawing.Cursors.Hand
        PictureBox1.BackgroundImage = CType(resources.GetObject("PictureBox1.BackgroundImage"), System.Drawing.Image)
        PictureBox1.Location = New System.Drawing.Point(8, 8)
        PictureBox1.Size = New System.Drawing.Size(176, 168)
        PictureBox1.TabIndex = 0
        PictureBox1.TabStop = False
        
        Button1.Location = New System.Drawing.Point(304, 48)
        Button1.ForeColor = System.Drawing.SystemColors.ControlLightLight
        Button1.Size = New System.Drawing.Size(24, 120)
        Button1.TabIndex = 2
        Button1.Text = "退出"
        Me.Text = "Form1"
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.BorderStyle = System.WinForms.FormBorderStyle.None
        Me.BackColor = System.Drawing.Color.Black
        Me.ClientSize = New System.Drawing.Size(344, 256)
        Me.Opacity = 0.8#
        
        Me.Controls.Add(Label2)
        Me.Controls.Add(Lab4)
        Me.Controls.Add(Lab3)
        Me.Controls.Add(Lab2)
        Me.Controls.Add(LBblue)
        Me.Controls.Add(LBgreen)
        Me.Controls.Add(LBred)
        Me.Controls.Add(Button1)
        Me.Controls.Add(Label1)
        Me.Controls.Add(PictureBox1)
    End Sub
    
#End Region
    上面一段#Region " Windows Form Designer generated code "是不必看的,
我全部写在这里是为了您可以直接粘贴运行
 
    Sub me_load()
        system.WinForms.MessageBox.show("欢迎使用我的第一个VB.Net程序", "微型取色器", messagebox.OK BitOr messagebox.IconAsterisk)
    End Sub
    
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) 'sender是发送数据的对象,e是按收参数的对象
        End '退出程序
    End Sub
    
    Protected Sub PictureBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) 'sender是发送数据的对象,e是按收参数的对象
        If apr = True Then system.WinForms.MessageBox.show("请将鼠标重新移入再次取色", "微型取色器", messagebox.OK BitOr messagebox.IconAsterisk)
        apr = True '选定色彩以后,RGB的分析就停止
        label2.BackColor = label1.BackColor '把源色彩传到目的色彩
    End Sub
    
    Public Function Getpixel() As System.Drawing.Color '这个也是函数的一个Alias
        Dim PM As System.Drawing.Bitmap '声明一个对象
        PM = PictureBox1.BackgroundImage '设置一个实例(注意前面的Option Strict Off不能少)
        Return pm.GetPixel(pixX, pixY) '使用Return返回一个值给函数,这是调用system.drawing.bitmap类的getpixel函数
        '如果是一个成员过程,不是一个函数,请用调用过程的方法调用,如system.drawing.bitmap类的setpixel过程:
        'Public Sub Setpixel(ByVal X As Integer, ByVal Y As Integer, ByVal pColor As System.Drawing.Color)
        'Dim PM As System.Drawing.Bitmap
        'PM = PictureBox1.BackgroundImage
        'pm.SetPixel(X, y, pcolor)
        'End Sub '当然这个代码是不能放在函数里面的了!!!
    End Function
    
    
    Public Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.WinForms.MouseEventArgs) Handles PictureBox1.MouseMove
        Dim PColor As System.Drawing.Color '声明一个color类型的变量
        Dim strPC As String  '声明一个String类型的变量
        Dim cltmpA, cltmpR, cltmpG, cltmpB As String '声明三个String类型的变量
         
        If apr Then Exit Sub '给一点时间好取定一个像素
        
        PColor = Getpixel '调用了getpixel,你也可以通过使用括号中的参数传第X,Y的值,在这个示例中使用公用变量pixX,pixY 
        LABEL1.BackColor = PColor '设定LABEL1控件的背景色
        strPC = PColor.ToString 'tostring是把pcolor从system.drawing.color转为string的数据
        pixX = e.X 'e是参数,也被当作一个对象,e.X是对象的一个成员,指明鼠标的当前坐标X值
        pixY = e.Y : LABEL1.BackColor = Getpixel '键盘不灵,回车不了,只好打“:”代替分行
        
        cltmpA = strPC.Substring(STRPC.IndexOf("A"), (STRPC.INDEXOF("R") - 2) - STRPC.IndexOf("A")) 'substring 就替我们sub一部份string,就好象VB6的mid函数,取代left,right函数
        CLTMPA = CLTMPA.Substring(CLTMPA.IndexOf("=") + 1, CLTMPA.Length - (CLTMPA.IndexOf("=") + 1)) 'substring 就替我们sub一部份string,就好象VB6的mid函数,取代left,right函数
        '取得红色
        cltmpR = strPC.Substring(STRPC.IndexOf("R"), (STRPC.INDEXOF("G") - 2) - STRPC.IndexOf("R")) 'substring 就替我们sub一部份string,就好象VB6的mid函数,取代left,right函数
        CLTMPR = CLTMPR.Substring(CLTMPR.IndexOf("=") + 1, CLTMPR.Length - (CLTMPR.IndexOf("=") + 1)) 'substring 就替我们sub一部份string,就好象VB6的mid函数,取代left,right函数
        '取得绿色
        cltmpG = strPC.Substring(STRPC.IndexOf("G"), (STRPC.INDEXOF("B") - 2) - STRPC.IndexOf("G")) 'substring 就替我们sub一部份string,就好象VB6的mid函数,取代left,right函数
        CLTMPG = CLTMPG.Substring(CLTMPG.IndexOf("=") + 1, CLTMPG.Length - (CLTMPG.IndexOf("=") + 1)) 'substring 就替我们sub一部份string,就好象VB6的mid函数,取代left,right函数
        '取得蓝色
        cltmpB = strPC.Substring(STRPC.IndexOf("B"), STRPC.Length - STRPC.IndexOf("B") - 1) 'substring 就替我们sub一部份string,就好象VB6的mid函数,取代left,right函数
        CLTMPB = CLTMPB.Substring(CLTMPB.IndexOf("=") + 1, CLTMPB.Length - (CLTMPB.IndexOf("=") + 1)) 'substring 就替我们sub一部份string,就好象VB6的mid函数,取代left,right函数
        
        lbred.BackColor = System.Drawing.Color.FromARGB(CLTMPA.ToInt32, cltmpR.ToInt32, 0, 0) 'fromargb就是VB6的RGB一样,只是在第一位多一个阿尔法(alpha)参数
        lbgreen.BackColor = System.Drawing.Color.FromARGB(CLTMPA.ToInt32, 0, cltmpG.ToInt32, 0) '提取绿色
        lbblue.BackColor = System.Drawing.Color.FromARGB(CLTMPA.ToInt32, 0, 0, cltmpB.ToInt32) '提取蓝色
        
        LAB2.Text = "R=" & CLTMPA '显示R值
        LAB3.Text = "G=" & CLTMPG '显示G值
        LAB4.Text = "B=" & CLTMPB '显示B值
    End Sub
    'cool啊,拖动是没有边框的
    Private Sub Form1_MouseDown(ByVal eventSender As Object, ByVal e As System.WinForms.MouseEventArgs)
        'mousedown的事件中窗体中的下拉列表是找不到的,自己写一个吧,加上(ByVal eventSender As Object, ByVal e As System.WinForms.MouseEventArgs)即可
        'Me.Capture() = False '释放鼠标捕获,等同于API的ReleaseCapture()
        'Me.SendMessage(&HA1S, 2, 0) '唔,这个就是哪个sendmessage的API了,只是第一个句柄参数不再用了。
    End Sub
    
    Public Sub PictureBox1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.MouseLeave
        apr = False '这个很有用的了,就是鼠标离开一个控件时以生的事件,这样的话,做一个动态3D按钮就很方便了
    End Sub
    
End Class
本文是面向对象的(对于VBNet的入门者),我不保证在基于传统的面向对象的理论有曲解的地方(我从来没有面向对象过,我也并不是很喜欢面向对象,起码再怎么面向也面向不出第二个windows来),
但这篇文章确实能使初学者(我相信困惑于此的人不少吧)快速运用VB.Net编出简单的程序,
我的贴文是想激励更多的人来“未必多了他(VBNet)”。
If
如果你是A(SP),B(ASIC),C(XX),D(ELPHI),E(XP),F(OX),G(O)的行家 
And 是不屑与我等庸手为伍的
Then 这篇文章实在没有看的必要(作为中国后备程序员,请原谅我有此通病而牢骚很多) 
Else  
仍然,衷心地期待您的不吝赐教           VB.Net中文站网址:http://vbnetcn.126.com
End If
 VB.Net中文站今天基本搞好了  示例源程序请到网站 相关下载 200k (4月16日)
 

本文地址:http://com.8s8s.com/it/it46333.htm