以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  [求助] 狐表GDI绘图.DrawString方法如何动态获得画刷的颜色?  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=5977)

--  作者:程兴刚
--  发布时间:2010/2/21 10:29:00
--  [求助] 狐表GDI绘图.DrawString方法如何动态获得画刷的颜色?
在狐表设计系统中,希望能让用户自由设置GDI绘图界面的颜色,但通过反复研究,未能实现动态获得画刷的颜色(Brushes.Red,这是红色画刷,这一句的代码是这样的:g.DrawString(gjdm,fnt,Brushes.Red,45,62)
下面是设置前后的界面对比:
1、这是系统默认界面:
    

图片点击可在新窗口打开查看此主题相关图片如下:1.png
图片点击可在新窗口打开查看


2、这是调整后的界面:


图片点击可在新窗口打开查看此主题相关图片如下:2.png
图片点击可在新窗口打开查看

上面是改变颜色后,点击二折钢筋的按钮生成的结果,其中背景颜色、钢筋图标颜色、标注线颜色动态活动颜色已经实现,因为画笔支持多种格式的颜色(

3、下面是2折钢筋按钮的全部代码,请大家帮助时参考:


Vars("钢筋代码") = left(e.Sender.text,1).Replace("焊","HZ") & " " \'
if datatables("系统设置").datarows(0)("提示音类型") = "无" then
    Audio.Stop()
else
    if datatables("系统设置").datarows(0)("提示音类型") = "随机" then
        dim tsyy As Integer = Rand.Next(11) \'生成0到11之间的随机整数
        If FileSys.FileExists(Vars("系统文件路径") & tsyy & ".wav") = true Then
            Audio.play(Vars("系统文件路径") & tsyy & ".wav",AudioPlayMode.Background)
        End If
    else
        If FileSys.FileExists(Vars("系统文件路径") & datatables("系统设置").datarows(0)("提示音类型") & ".wav") = true Then
            Audio.play(Vars("系统文件路径") & datatables("系统设置").datarows(0)("提示音类型") & ".wav",AudioPlayMode.Background)
        End If
    End If
End If
Dim Multi As String = "A|B|C|D|E|F|G|H|I|JD1|JD2"
Dim Values() as String
Values = Multi.split("|")
For Index As Integer = 0 To Values.Length - 1
    if e.Form.ExistControl(Values(Index))  = true
        e.Form.RemoveControl(Values(Index))
    end if
Next                                                                       
Dim p As WinForm.Painter = e.Form.Controls("Painter100")
Dim g As Graphics = p.Graphics
g.Clear(Color.LightSeaGreen) \'清除所有内容,并将背景颜色设为黑色
p.Repaint()                                                   
灰色部分无需参考
dim sum as Integer
dim i as Integer
dim lb as Integer = val(left(e.Sender.text,1).Replace("焊","10"))
Sum = Tables("单筋定位参数").Compute("Sum(钢筋类别)", "钢筋类别 = " & lb)/lb
For tb As Integer = 1 To 99
    if e.Form.ExistControl("Painter" & tb)= true
        e.Form.RemoveControl("Painter" & tb)
    end if
    if tb <= Math.Max(sum,25)
        dim gjdm as String
        gjdm = lb & format(tb,"000")
        Dim A As WinForm.Painter
        A = e.Form.CreateControl("Painter" & tb, ControlTypeEnum.Painter)
        A.Left =98*((tb-1) mod 5)
        A.Top = 65*((tb-1)\\5)
        A.Width =97
        A.Height =64
        A.BackColor = Color.FromARGB(getConfigValue("窗口图库背景色",e.Form.Controls("ColorLabel5").Value))
        e.Form.Controls("Panel11").AddControl(A)
        if tb <= sum
            p = Forms("单筋翻样操作").controls("Painter" & tb)
            g = p.Graphics
            Dim pn As New Pen(Color.FromARGB(getConfigValue("窗口钢筋图标色",e.Form.Controls("ColorLabel1").Value)),2.5) \'指定画笔颜色和画笔宽度
            Dim pn1 As New Pen(Color.FromARGB(getConfigValue("窗口标注线颜色",e.Form.Controls("ColorLabel2").Value)),1) \'指定画笔颜色和画笔宽度
            Dim fnt As New Font("宋体",11,FontStyle.Regular)
            Dim fnt1 As New Font("宋体",11,FontStyle.Bold)
            p.Cursor = Windows.Forms.Cursors.Hand
            g.scaletransform(0.8f,0.8f)
            Dim dr As DataRow
            For i = 1 to DataTables("单筋定位参数").Compute("max(顺序号)","[钢筋代码] = \'" & gjdm & "\' and [图形类型] = \'直线\'")
                dr = DataTables("单筋定位参数").Find("[钢筋代码] = \'" & gjdm & "\' and [图形类型] = \'直线\'and [顺序号] = \'" & i & "\'")
                g.DrawLine(pn,dr("x1")-9,dr("y1")+25,dr("x2")-9,dr("y2")+25)
            Next
            For i = 1 to DataTables("单筋定位参数").Compute("max(顺序号)","[钢筋代码] = \'" & gjdm & "\' and [图形类型] = \'圆\'")
                dr = DataTables("单筋定位参数").Find("[钢筋代码] = \'" & gjdm & "\' and [图形类型] = \'圆\'and [顺序号] = \'" & i & "\'")
                g.DrawArc(pn,dr("x1")-9,dr("y1")+25,dr("直径"),dr("直径"),dr("起始度"),dr("旋转角度"))
            Next
            Multi = "A|B|C|D|E|F|G|H|I"
            Values = Multi.split("|")
            For i = 1 to DataTables("单筋定位参数").Compute("max(顺序号)","[钢筋代码] = \'" & gjdm & "\' and [图形类型] = \'字注\'")
                dr = DataTables("单筋定位参数").Find("[钢筋代码] = \'" & gjdm & "\' and [图形类型] = \'字注\'and [顺序号] = \'" & i & "\'")
                g.DrawString( Values(i-1),fnt,Brushes.Lime,dr("x1"),dr("y1")+21)
            Next
            Multi = "JD1|JD2"
            Values = Multi.split("|")
            For i = 1 to DataTables("单筋定位参数").Compute("max(顺序号)","[钢筋代码] = \'" & gjdm & "\' and [图形类型] = \'角注\'")
                dr = DataTables("单筋定位参数").Find("[钢筋代码] = \'" & gjdm & "\' and [图形类型] = \'角注\'and [顺序号] = \'" & i & "\'")
                g.DrawString( Values(i-1),fnt,Brushes.Lime,dr("x1")-19,dr("y1")+25)
            Next
            For i = 1 to DataTables("单筋定位参数").Compute("max(顺序号)","[钢筋代码] = \'" & gjdm & "\' and [图形类型] = \'标注线\'")
                dr = DataTables("单筋定位参数").Find("[钢筋代码] = \'" & gjdm & "\' and [图形类型] = \'标注线\'and [顺序号] = \'" & i & "\'")
                g.DrawLine(pn1,dr("x1")-9,dr("y1")+25,dr("x2")-9,dr("y2")+25)
            Next
            For i = 1 to DataTables("单筋定位参数").Compute("max(顺序号)","[钢筋代码] = \'" & gjdm & "\' and [图形类型] = \'标注弧\'")
                dr = DataTables("单筋定位参数").Find("[钢筋代码] = \'" & gjdm & "\' and [图形类型] = \'标注弧\'and [顺序号] = \'" & i & "\'")
                g.DrawArc(pn1,dr("x1")-9,dr("y1")+25,dr("直径"),dr("直径"),dr("起始度"),dr("旋转角度"))
            Next
            g.DrawString(gjdm,fnt1,Brushes.Lime,45,62)
            p.Repaint()
        end if
    end if
Next



        上述代码中,红色部分是需要动态获得画刷颜色的代码行,红色粗体部分是指定画刷颜色的代码。
                         绿色部分是已经实现获得画笔颜色的代码行,绿色粗体部分是动态指定画刷颜色的代码。

         主要原因:画笔支持多种颜色格式,划算却却只能获取画刷颜色(Brushes.Lime,这里不是一个简单的字符型名称,而是一个新的画笔颜色)

4、另外:颜色标签的ValueChanged事件代码:SaveConfigValue("窗口钢筋图标色",e.Sender.Value)  
(目的,调整颜色后的属性保证已后打开项目有效)       里是钢筋图标颜色,其它颜色标签都一样。
             窗口的AfterLoad事件代码:
                     e.Form.Controls("ColorLabel1").Value = getConfigValue("窗口钢筋图标色",Color.red)
                     e.Form.Controls("ColorLabel2").Value = getConfigValue("窗口标注线颜色",Color.DimGray)
                     e.Form.Controls("ColorLabel3").Value = getConfigValue("窗口标注文字色",FontStyle.Regular)
                     e.Form.Controls("ColorLabel4").Value = getConfigValue("窗口钢筋代码色",Color.red)
                     e.Form.Controls("ColorLabel5").Value = getConfigValue("窗口图库背景色",Color.Black)

--  作者:baoxyang
--  发布时间:2010/2/21 11:58:00
--  
值得学习!谢了!!
--  作者:czy
--  发布时间:2010/2/21 15:14:00
--  
目前来看没有简单的办法实现动态改变颜色,因为涉及Brushes部份只能是一个颜色类型,如果用颜色标签只能返回其值(不能返回颜色类型),而Brush类型又不能从字符转换而来,所以最简单的办法是用Select Case语句判断所选值(也可以是颜色标签的值)再进行定义(转换)画涮颜色,最后在DrawString中使用选中的经过转换的画涮颜色。
--  作者:yangming
--  发布时间:2010/2/21 16:08:00
--  
If e.Form.Controls("ComboBox16").value <> "" Then
Select Case e.Form.Controls("ComboBox16").value
        Case "酸橙色"
      g.DrawString(gjdm,fnt1,Brushes.Lime,45,62)
              Case "红"
        g.DrawString(gjdm,fnt1,Brushes.Red,45,62)

     Case "蓝"
        g.DrawString(gjdm,fnt1,Brushes.Blue,45,62)
     Case "黄"
        g.DrawString(gjdm,fnt1,Brushes.Yellow,45,62)
     Case "绿"
        g.DrawString(gjdm,fnt1,Brushes.Green,45,62)
       End Select
 End If

--  作者:程兴刚
--  发布时间:2010/2/21 22:19:00
--  
谢谢各位!
--  作者:zxczhanghu
--  发布时间:2014/4/25 14:11:00
--  
很感兴趣  你的狐表  软件


--  作者:ido
--  发布时间:2014/4/25 14:25:00
--  
程版威武~~~
--  作者:zxczhanghu
--  发布时间:2014/4/25 14:38:00
--  
是不是 版主 不做这个了?