首页 > 编程 > .NET > 正文

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

2024-07-10 13:01:25
字体:
来源:转载
供稿:网友

走近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
本文是面向对象的(对于vb。net的入门者),我不保证在基于传统的面向对象的理论有曲解的地方(我从来没有面向对象过,我也并不是很喜欢面向对象,起码再怎么面向也面向不出第二个windows来),
但这篇文章确实能使初学者(我相信困惑于此的人不少吧)快速运用vb.net编出简单的程序,
我的贴文是想激励更多的人来“未必多了他(vb。net)”。
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日)
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表