2014年1月13日月曜日

.NETでのPG間共通Config

別PGから同じDLLを使用したシステム開発を依頼された。
で、外部に設定値としてConfigファイルを持たせることに。

ただ、通常ではEXE単位(userSettingsだとさらにユーザ単位)にConfigを持つわけで。
全PGにDLLの情報を持たせるのはめんどくさいし
出来れば変更内容を別EXEにも反映させたい。

で、お決まりのググる作業w

まず、設定の統一化
  各PGの App.Config (コンパイル後は[PG名].exe.config)から同一のconfigファイルを参照させる
  各PGのApp.configのApplicationSettingを改造
例 改造前 各PGのApp.config
        <applicationSettings>
           <TestSys.TestLibs.My.MySettings>
               <setting name="TESTDATA" serializeAs="String">
                   <value>100</value>
               </setting>
           </TestSys.TestLibs.My.MySettings>
        </applicationSettings>
 ↓
  改造後 各PGのApp.config
        <applicationSettings>
           <TestSys.TestLibs.My.MySettings configSource="TestCommon.config"/>
        </applicationSettings>

     共通のConfigファイル(ここでは仮にTestCommon.config)
           <TestSys.TestLibs.My.MySettings>
               <setting name="TESTDATA" serializeAs="String">
                   <value>100</value>
               </setting>
           </TestSys.TestLibs.My.MySettings>
これで同一ファイルへの参照が可能

でPGから設定内容を変更したい場合はTestCommon.configを直接変更かける仕組みを
追加する
 内容はSystem.Xml.XmlDocument でxmlファイルへ出力



2014年1月11日土曜日

色選択用コンボボックス

PGから色を指定できる仕様にして欲しいとの依頼。
Configファイルでの指定では駄目らしい。

うむ。めんどくさい。
ColorDialogを出しても良いけど、色指定が微妙。
簡単に指定が良いなぁ・・・

で、ネットを検索して、コンボボックスを自分で描写する事に。

以下がそのソース
コンボボックスの継承クラスです。

################### ソースここから #################
Imports System.ComponentModel

''' <summary>
''' 色選択コンボボックス
''' </summary>
Public Class ColorComboBox
    Inherits System.Windows.Forms.ComboBox

#Region "内部変数"
    ''' <summary>カラー色表示</summary>
    Private _DispColorName As Boolean = True
    ''' <summary>サンプル文字</summary>
    Private _SampleString As String = ""
#End Region
#Region "プロパティ"
    ''' <summary>
    ''' カラー色表示
    ''' </summary>
    <Category("表示")> _
    <Description("色名の表示設定")> _
    Public Property DispColorName() As Boolean
        Get
            Return _DispColorName
        End Get
        Set(ByVal value As Boolean)
            _DispColorName = value
        End Set
    End Property
    ''' <summary>
    ''' サンプル文字
    ''' </summary>
    <Category("表示")> _
    <Description("色指定でのサンプル文字")> _
    Public Property SampleString() As String
        Get
            Return _SampleString
        End Get
        Set(ByVal value As String)
            _SampleString = value
        End Set
    End Property
#End Region

#Region "コンストラクタ"
    ''' <summary>
    ''' コンストラクタ
    ''' </summary>
    Public Sub New()
        MyBase.New()
        '固定プロパティ
        Me.DrawMode = Windows.Forms.DrawMode.OwnerDrawFixed
        Me.DropDownStyle = ComboBoxStyle.DropDownList
        '色設定
        Me.Items.Clear()
        For Each col As KnownColor In [Enum].GetValues(GetType(KnownColor))
            Me.Items.Add(Color.FromName(col.ToString))
        Next
    End Sub
#End Region

#Region "イベント"
    ''' <summary>
    ''' 行描写
    ''' </summary>
    Protected Overrides Sub OnDrawItem(ByVal e As System.Windows.Forms.DrawItemEventArgs)
        '領域
        Dim rCol As RectangleF
        Dim rText As RectangleF
        Dim rTextBack As RectangleF
        If DispColorName Then
            '色名あり
            Dim rWid As Single = 30
            If rWid > CSng(e.Bounds.Width * 0.9) Then
                rWid = CSng(e.Bounds.Width * 0.9)
            End If
            rCol = New RectangleF(e.Bounds.Left, e.Bounds.Top, rWid, e.Bounds.Height)
            If (rWid + 10) > e.Bounds.Width Then
                rWid = 0
            Else
                rWid = 10
            End If
            rText = New RectangleF(rCol.Right + rWid, e.Bounds.Top, e.Bounds.Width, e.Bounds.Height)
            rTextBack = New RectangleF(rCol.Right, e.Bounds.Top, e.Bounds.Width, e.Bounds.Height)

        Else
            '色名なし
            rCol = New RectangleF(e.Bounds.Left, e.Bounds.Top, e.Bounds.Width, e.Bounds.Height)
            rText = New RectangleF(e.Bounds.Left, e.Bounds.Top, e.Bounds.Width, e.Bounds.Height)
            rTextBack = New RectangleF(rCol.Right, e.Bounds.Top, e.Bounds.Width, e.Bounds.Height)
        End If
        If e.Index >= 0 Then
            '外枠   
            Using brush As New SolidBrush(Me.BackColor)
                e.Graphics.FillRectangle(brush, rTextBack)
            End Using
            '選択色   
            Dim objColor As Color = DirectCast(Me.Items(e.Index), Color)
            If DispColorName Then
                '色名称
                Dim txt As String = objColor.ToKnownColor.ToString
                Dim fmt As StringFormat = CType(StringFormat.GenericDefault.Clone, StringFormat)
                fmt.Alignment = StringAlignment.Near
                fmt.LineAlignment = StringAlignment.Center
                Using brush As New SolidBrush(Me.ForeColor)
                    e.Graphics.DrawString(txt, e.Font, brush, rText, fmt)
                End Using
            End If
            '色枠  
            Using backBrush As New SolidBrush(objColor)
                e.Graphics.FillRectangle(backBrush, rCol)
            End Using
            'サンプル文字
            Dim Samplefmt As StringFormat = CType(StringFormat.GenericDefault.Clone, StringFormat)
            Samplefmt.Alignment = StringAlignment.Center
            Samplefmt.LineAlignment = StringAlignment.Center
            Using brush As New SolidBrush(Me.ForeColor)
                e.Graphics.DrawString(Me.SampleString, e.Font, brush, rCol, Samplefmt)
            End Using
        End If
    End Sub
#End Region

End Class

################### ソースここまで #################

プロパティを2つほど追加しています。
DispColorName  色名称の表示ON/OFF
SampleString       色にかぶせるサンプル文字

で、設定・取得は SelectItem を使用(colorオブジェクトでね)

いつもの通り、即席での作成です。
バグ等があるやも知りませんがwww