关于XP样式按钮的问题

yy721521 2005-06-04 11:51:01
小弟,最近优化软件,找到一个可以优化按钮为xp效果的代码,可是不知道为什么其中有一个变量总是没有声明。很是郁闷,请高手告诉我怎么样才能声明么?谢了
InitializeComponent()就是这个变量没有声明

找了别的窗口代码,找了一句声明这个函数的语句 <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

可是也插不进去,什么地方都不让放,请问这是为什么啊~谢谢大虾了,小弟不才。。


Imports System.Drawing
Imports System.ComponentModel
Public Class winxpbutton
Inherits System.Windows.Forms.Button

Private my_mouseDown As Boolean = False '鼠标按下
Private my_mouseHover As Boolean = False '鼠标移到上面
Private m_textcolor As Color = System.Drawing.Color.Black '字体颜色
<Description("Red")> Public Property textcolor() As Color
Get
Return m_textcolor

End Get
Set(ByVal Value As Color)
m_textcolor = Value
Me.Invalidate()
End Set
End Property
Public Sub New()
MyBase.New() '该调用是 Windows 窗体设计器所必需的。
InitializeComponent()*************************************就是这里!!!!!!
'在 InitializeComponent() 调用之后添加任何初始化,true表示将指定的样式应用到控件
'设置控件样式位能够充分地更改控件行为
Me.SetStyle(ControlStyles.UserPaint, True) '关联事件委托
AddHandler Me.MouseDown, AddressOf my_OnMouseDown
AddHandler Me.MouseUp, AddressOf my_OnMouseUp
AddHandler Me.MouseEnter, AddressOf my_OnMouseEnter
AddHandler Me.MouseLeave, AddressOf my_OnMouseLeave
Height = 23
Width = 75
End Sub
Protected Overrides Sub OnPaint(ByVal pevent As System.Windows.Forms.PaintEventArgs)
'pevent.ClipRectangle指在其中绘制的矩形,即使用父控件的背景色来画这个矩形按钮
pevent.Graphics.FillRectangle(New SolidBrush(Me.Parent.BackColor), pevent.ClipRectangle)
If (Enabled = False) Then
'画不可用状态
DrawDisableButton(pevent.Graphics)
ElseIf (my_mouseDown) Then '画鼠标按下状态
DrawMouseDownButton(pevent.Graphics)
ElseIf (my_mouseHover) Then '画鼠标移动到其上状态
DrawMouseHoverButton(pevent.Graphics)
ElseIf (Focused) Then '有焦点,但鼠标未移动到其上
DrawContainFocusButton(pevent.Graphics)
Else '一般情况下
DrawNormalButton(pevent.Graphics)
End If
'写文本
WriteText(pevent.Graphics)
End Sub
'鼠标按下的状态处理
Private Sub my_OnMouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
my_mouseDown = True '鼠标按下
End Sub
'鼠标松开状态的处理
Private Sub my_OnMouseUp(ByVal sender As Object, ByVal e As MouseEventArgs)
my_mouseDown = False '鼠标松开
'重新绘制控件时发生 Paint 事件。PaintEventArgs 指定绘制控件所用的 Graphics
'以及绘制控件所在的 ClipRectangle。
Dim pe As PaintEventArgs = New PaintEventArgs(CreateGraphics(), ClientRectangle)
OnPaint(pe)
End Sub
'鼠标进入
Private Sub my_OnMouseEnter(ByVal sender As Object, ByVal e As EventArgs)
my_mouseHover = True '鼠标移动到其上
Dim pe As PaintEventArgs = New PaintEventArgs(CreateGraphics(), ClientRectangle)
OnPaint(pe)
End Sub
'鼠标移动开
Private Sub my_OnMouseLeave(ByVal sender As Object, ByVal e As EventArgs)
my_mouseHover = False '鼠标移动开
Dim pe As PaintEventArgs = New PaintEventArgs(CreateGraphics(), ClientRectangle)
OnPaint(pe)
End Sub
...全文
196 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
goldli 2005-06-08
  • 打赏
  • 举报
回复
8错
yy721521 2005-06-08
  • 打赏
  • 举报
回复
谢谢兄弟,我试试。
laker0575 2005-06-07
  • 打赏
  • 举报
回复
8错,UP
  • 打赏
  • 举报
回复
太长了
mouse0987 2005-06-07
  • 打赏
  • 举报
回复
请点这里,查看详细情况

http://www.lz.net.cn/bbs/X_AdvCom_Get.asp?UserID=7342
tihua 2005-06-07
  • 打赏
  • 举报
回复
to :Snrmnm_sx(Ψ 踏雪无痕 Ω)
这个方法在Windows2000下能实现XP界面吗?
Snrmnm_sx 2005-06-07
  • 打赏
  • 举报
回复
虽然WindowsXP的界面非常华丽。但是令人遗憾的是,我们用VB写的程序除了菜单和窗口的外观是XP风格外,其他控件还是一如既往的是普通风格。我想,有的程序在XP下有XP的风格,VB也一定有办法实现XP的风格。后来笔者在网上看到一篇介绍XML的文章,又在网上找到了一篇实现XP风格的文章,经过自己的实践,终于找到了VB程序实现XP风格的办法。为了使其他的网友不至于走像我这样的多的弯路,将此办法写出来,与大家共同提高!!
具体的实现方法如下:

①:在你的窗体加入代码。如果是多个窗口,可以将此代码放到模块中。

代码如下:

'声明API函数

Private Declare Sub InitCommonControls Lib "comctl32.dll" ()

Private Sub Form_Initialize()

InitCommonControls

End Sub

注意:千万不要在Form_load()事件下写InitCommonControls这句话,否则你的窗体会启动不了。(不要怪我没告诉你啊~~~~)

②:建立一个和你的.exe同名的.exe.manifest文件(假如:如果你最后编译好的文件名为WindowXPStyle.exe,则建立一个文件名为WindowXPStyle.exe.manifest的文件),并且此文件必须存在于和你Exe文件的同一目录下。

以WindowXPStyle.exe为例,则这个WindowXPStyle.exe.manifest的文件的内容如下:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">

<assemblyIdentity

version="1.0.0.0"

processorArchitecture="X86"

name="CompanyName.ProductName.YourApp"

type="win32"

/>


<description>Your application description here.</description>

<dependency>

<dependentAssembly>

<assemblyIdentity

type="win32"

name="Microsoft.Windows.Common-Controls"

version="6.0.0.0"


processorArchitecture="X86"

publicKeyToken="6595b64144ccf1df"

language="*"

/>

</dependentAssembly>

</dependency>

</assembly>

将以上内容保存为WindowXPStyle.exe.manifest。并且和执行文件放在一个目录下(一定要记住)。

到此为止,我们的程序已经完成,不过在VB的设计环境下运行是看不到XP效果的。必须编译成可执行文件运行才可以。(因为这个我试了不知多少遍,以为VB程序就是实现不了XP效果呢~~,没想到必须编译成执行文件才可以)。


作者:cuizm
yy721521 2005-06-07
  • 打赏
  • 举报
回复
还是不太明白老大的解释。是要把我那段代码放到userControll位置里面么?放到
Public Sub New()
end sub后面它后面的语句就说没有命名空间了。。。
还是找不到正确的位置。。
老戴12345654 2005-06-05
  • 打赏
  • 举报
回复
在 Public Sub New()
………………
End Sub
后面添加下面代码就可以了

'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
'
'UserControl1
'
Me.Name = "XPButton"

End Sub
yy721521 2005-06-04
  • 打赏
  • 举报
回复
Private Sub DrawBorder(ByVal g As Graphics, ByVal state As Integer)
If (state = 1) Then '绘制一般边框
'绘制一个画笔,高光点,宽度2
Dim p As Pen = New Pen(SystemColors.ControlLightLight, 2)
'g.DrawLine画线,p是画笔,后面是第一个点的坐标,第二个点的坐标
g.DrawLine(p, 1, 1, 1, Height - 2) '绘制左侧竖线
g.DrawLine(p, 1, 1, Width - 2, 1) '绘制上面横线
g.DrawLine(p, Width - 1, 2, Width - 1, Height - 2) '绘制右侧竖线,由于已经在上面绘制了横线(纵坐标为1),所以从2开始
g.DrawLine(p, 2, Height - 1, Width - 2, Height - 1) '绘制下面横线
ElseIf (state = 2) Then '绘制移动到其上的边框
'与一般边框用高光区别的是显示黄色
Dim p As Pen = New Pen(Color.Yellow, 2)
g.DrawLine(p, 1, 1, 1, Height - 2)
g.DrawLine(p, 1, 1, Width - 2, 1)
g.DrawLine(p, Width - 1, 2, Width - 1, Height - 2)
g.DrawLine(p, 2, Height - 1, Width - 2, Height - 1)
ElseIf (state = 3) Then '绘制按下的显示边框
'与一般边框用高光区别的是显示暗褐色
Dim p As Pen = New Pen(SystemColors.ControlDark, 2)
g.DrawLine(p, 1, 1, 1, Height - 2)
g.DrawLine(p, 1, 1, Width - 2, 1)
g.DrawLine(p, Width - 1, 2, Width - 1, Height - 2)
g.DrawLine(p, 2, Height - 1, Width - 2, Height - 1)
ElseIf (state = 4) Then '绘制不可用状态边框
'与一般边框用高光区别的是显示亮色
Dim p As Pen = New Pen(SystemColors.ControlLight, 2)
g.DrawLine(p, 1, 1, 1, Height - 2)
g.DrawLine(p, 1, 1, Width - 2, 1)
g.DrawLine(p, Width - 1, 2, Width - 1, Height - 2)
g.DrawLine(p, 2, Height - 1, Width - 2, Height - 1)
ElseIf (state = 5) Then '绘制有焦点但鼠标不在其上的状态
'与一般边框用高光区别的是显示兰色
Dim p As Pen = New Pen(Color.SkyBlue, 2)
g.DrawLine(p, 1, 1, 1, Height - 2)
g.DrawLine(p, 1, 1, Width - 2, 1)
g.DrawLine(p, Width - 1, 2, Width - 1, Height - 2)
g.DrawLine(p, 2, Height - 1, Width - 2, Height - 1)
End If
'//做完如上的处理后再对可用和不可用做圆化边缘处理(也就是把按钮的4个角进行圆化处理)
If (state = 4) Then '不可用时
'使用画笔,Color.FromArgb(161, 161, 146)是从一个32位的ARGB值创建系统颜色,宽度为1
Dim p As Pen = New Pen(Color.FromArgb(161, 161, 146), 1)
g.DrawLine(p, 0, 2, 0, Height - 3) '左侧竖线(除了两个边角剩下的线)
g.DrawLine(p, 2, 0, Width - 3, 0) '上面横线(除了两个边角剩下的线)
g.DrawLine(p, Width - 1, 2, Width - 1, Height - 3) '右侧竖线(除了两个边角剩下的线)
g.DrawLine(p, 2, Height - 1, Width - 3, Height - 1) '下面的横线
g.DrawLine(p, 0, 2, 2, 0) '左上角
g.DrawLine(p, 0, Height - 3, 2, Height - 1) '左下角
g.DrawLine(p, Width - 3, 0, Width - 1, 2) '右上角
g.DrawLine(p, Width - 3, Height - 1, Width - 1, Height - 3) '右下角
Else 'draw normal style border
'采用默认的黑色进行绘制边角
g.DrawLine(Pens.Black, 0, 2, 0, Height - 3)
g.DrawLine(Pens.Black, 2, 0, Width - 3, 0)
g.DrawLine(Pens.Black, Width - 1, 2, Width - 1, Height - 3)
g.DrawLine(Pens.Black, 2, Height - 1, Width - 3, Height - 1)
g.DrawLine(Pens.Black, 0, 2, 2, 0)
g.DrawLine(Pens.Black, 0, Height - 3, 2, Height - 1)
g.DrawLine(Pens.Black, Width - 3, 0, Width - 1, 2)
g.DrawLine(Pens.Black, Width - 3, Height - 1, Width - 1, Height - 3)
End If
End Sub
'一般状态
Private Sub DrawNormalButton(ByVal g As Graphics)
'绘制边框,宽度为1
DrawBorder(g, 1)
'绘制背景,用高光点颜色
PaintBack(g, SystemColors.ControlLightLight)
End Sub
'鼠标移动到其上的状态
Private Sub DrawMouseHoverButton(ByVal g As Graphics)
DrawBorder(g, 2)
PaintBack(g, SystemColors.ControlLightLight)
End Sub
Private Sub DrawMouseDownButton(ByVal g As Graphics)
DrawBorder(g, 3)
'绘制背景,用三维元素的亮色
PaintBack(g, SystemColors.ControlLight)
End Sub
Private Sub DrawDisableButton(ByVal g As Graphics)
DrawBorder(g, 4)
'亮色
PaintBack(g, SystemColors.ControlLight)
End Sub
Private Sub DrawContainFocusButton(ByVal g As Graphics)
DrawBorder(g, 5)
'高光点
PaintBack(g, SystemColors.ControlLightLight)
End Sub
'绘制背景色
Private Sub PaintBack(ByVal g As Graphics, ByVal c As Color)
'填充时采用:单色画刷,相对与(0,0)坐标(3,3)的位置,大小为宽-6,高-6
g.FillRectangle(New SolidBrush(c), 3, 3, Width - 6, Height - 6)
End Sub
'写文本
Private Sub WriteText(ByVal g As Graphics)
'计算文本的位置
Dim x As Integer = 0
Dim y As Integer = 0
'size用宽高有序对表示矩形区域
Dim s As Size = g.MeasureString(Text, Font).ToSize()
x = (Width - s.Width) / 2 '文字相对控件x偏移
y = (Height - s.Height) / 2 '文字相对控件y偏移
'写文本
If (Enabled) Then '如果控件可用,则黑色文字
'g.DrawString(Text, Font, Brushes.Black, x, y)
Dim b As New SolidBrush(m_textcolor)
g.DrawString(Text, Font, b, x, y)
Else '如果控件不可用,则灰色文字
g.DrawString(Text, Font, Brushes.Gray, x, y)
End If
End Sub
End Class

16,721

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧