DoboWiki
Top
> .NETプログラミング研究/5 をテンプレートにして作成
.NETプログラミング研究/5 をテンプレートにして作成
開始行:
#title(.NETプログラミング研究 第5号)
#navi(.NETプログラミング研究)
#contents
*.NETプログラミング研究 第5号 [#k277db53]
**注意事項 [#y3b522fa]
今回はコードが長いため、VB.NETのコードのみを紹介します。...
**ピンポイントリンク [#q3e43a7a]
***メニューにアイコンを表示する2 [#yb112a6f]
前回メニュにアイコンを表示する方法についてその基礎を説明...
前回のImageMenuItemクラスの欠点として主に次の2点が挙げら...
+メニュー項目が無効になっているとき(EnabledがFalseになっ...
+チェックが付いているとき(CheckedがTrueのとき)の処理
まず1番目の問題ですが、これには文字列を立体的に灰色表示す...
多分これが一般的なのではないかと思いますが、まずはAPIを使...
(下記コード内のDrawStateString関数で描画した文字列の位置...
#code(vbnet){{
'API宣言
Private Declare Function DrawState Lib "user32" Alias "Dr...
(ByVal hDC As IntPtr, _
ByVal hBrush As Integer, _
ByVal lpDrawStateProc As Integer, _
ByVal lParam As IntPtr, _
ByVal wParam As Integer, _
ByVal n1 As Integer, _
ByVal n2 As Integer, _
ByVal n3 As Integer, _
ByVal n4 As Integer, _
ByVal un As Integer) As Integer
Private Declare Function DrawStateString Lib "user32" _
Alias "DrawStateA" (_
ByVal hDC As IntPtr, _
ByVal hBrush As Integer, _
ByVal lpDrawStateProc As Integer, _
ByVal lpString As String, _
ByVal cbStringLen As Integer, _
ByVal n1 As Integer, _
ByVal n2 As Integer, _
ByVal n3 As Integer, _
ByVal n4 As Integer, _
ByVal un As Integer) As Integer
Private Const DST_COMPLEX = &H0
Private Const DST_TEXT = &H1
Private Const DST_PREFIXTEXT = &H2
Private Const DST_ICON = &H3
Private Const DST_BITMAP = &H4
Private Const DSS_NORMAL = &H0
Private Const DSS_UNION = &H10
Private Const DSS_DISABLED = &H20
Private Const DSS_MONO = &H80
Private Const DSS_RIGHT = &H8000
'メニュー項目に表示させるアイコン
Private _icon As New Icon("open.ico")
'メニュー項目の描画
Private Sub MenuItem2_DrawItem(ByVal sender As System.Obj...
ByVal e As System.Windows.Forms.DrawItemEventArgs...
Handles MenuItem2.DrawItem
'無効状態のアイコンを描画
Dim hdc As IntPtr = e.Graphics.GetHdc()
If Not (_icon Is Nothing) Then
DrawState(hdc, 0, 0, _icon.Handle, 0, e.Bounds.Le...
e.Bounds.Top + (e.Bounds.Height - _icon.Heigh...
16, 16, DST_ICON Or DSS_DISABLED)
End If
'無効状態の文字列を描画
DrawStateString(hdc, 0, 0, sender.Text, LenB(sender.T...
e.Bounds.Left + 22, _
e.Bounds.Top + _
(e.Bounds.Height - SystemInformation.MenuFont.Hei...
e.Bounds.Width, e.Bounds.Height, _
DST_PREFIXTEXT Or DSS_DISABLED)
'開放
e.Graphics.ReleaseHdc(hdc)
End Sub
'VB6のLenBの代わりになるかもしれない関数
Private Function LenB(ByVal strString As String) As Integer
'Shift JISに変換したときに必要なバイト数を返す
Return System.Text.Encoding.GetEncoding(932). _
GetByteCount(strString)
End Function
}}
APIを使わずに同じことができないものか調べたところ、それら...
#code(vbnet){{
'無効状態のアイコンを描画
ControlPaint.DrawImageDisabled(e.Graphics, _
_image, x, y, SystemColors.Menu)
'無効状態の文字列を描画
ControlPaint.DrawStringDisabled(e.Graphics, _
text, SystemInformation.MenuFont, _
SystemColors.ControlLightLight, _
New RectangleF(point.X, point.Y, _
e.Bounds.Width, e.Bounds.Height), _
sf)
}}
これで期待通りの結果が得られるかといいますと、そううまく...
こうなったら自分でなんとかすることにしましょう。無効状態...
問題は画像の方です。ここでは確実に、元の画像を1ドットずつ...
(ここでは白のみを透明にしていますが、これには何の根拠も...
bmp.GetPixel(x, y).GetBrightness < 1.0F
の1.0Fを別の値にしたほうがいいのかもしれません。どの値が...
#code(vbnet){{
'無効状態の画像を作成する
Private Function CreateDisanabledImage(ByVal img As Image...
As Image
Dim x, y As Integer
If img Is Nothing Then
Return Nothing
End If
'無効状態の画像 元の画像より縦横1ドットずつ大きくなる
Dim dbmp As New Bitmap(img.Width + 1, img.Height + 1)
'もとの画像
Dim bmp As New Bitmap(img)
'1ドットずつ調べる
For y = 0 To bmp.Height - 1
For x = 0 To bmp.Width - 1
'元の色が透明か、白を透明にする
If bmp.GetPixel(x, y).A = &HFF AndAlso _
bmp.GetPixel(x, y).GetBrightness < 1....
dbmp.SetPixel(x, y, SystemColors.ControlD...
dbmp.SetPixel(x + 1, y + 1, _
SystemColors.ControlLightLight)
End If
Next
Next
bmp.Dispose()
Return dbmp
End Function
}}
次に2番目の問題に移りたいところですが、予想以上に長くなっ...
**コメント [#m023e7b4]
#comment
//これより下は編集しないでください
#pageinfo([[:Category/.NET]],2003-04-15 (火) 06:00:00,DOB...
終了行:
#title(.NETプログラミング研究 第5号)
#navi(.NETプログラミング研究)
#contents
*.NETプログラミング研究 第5号 [#k277db53]
**注意事項 [#y3b522fa]
今回はコードが長いため、VB.NETのコードのみを紹介します。...
**ピンポイントリンク [#q3e43a7a]
***メニューにアイコンを表示する2 [#yb112a6f]
前回メニュにアイコンを表示する方法についてその基礎を説明...
前回のImageMenuItemクラスの欠点として主に次の2点が挙げら...
+メニュー項目が無効になっているとき(EnabledがFalseになっ...
+チェックが付いているとき(CheckedがTrueのとき)の処理
まず1番目の問題ですが、これには文字列を立体的に灰色表示す...
多分これが一般的なのではないかと思いますが、まずはAPIを使...
(下記コード内のDrawStateString関数で描画した文字列の位置...
#code(vbnet){{
'API宣言
Private Declare Function DrawState Lib "user32" Alias "Dr...
(ByVal hDC As IntPtr, _
ByVal hBrush As Integer, _
ByVal lpDrawStateProc As Integer, _
ByVal lParam As IntPtr, _
ByVal wParam As Integer, _
ByVal n1 As Integer, _
ByVal n2 As Integer, _
ByVal n3 As Integer, _
ByVal n4 As Integer, _
ByVal un As Integer) As Integer
Private Declare Function DrawStateString Lib "user32" _
Alias "DrawStateA" (_
ByVal hDC As IntPtr, _
ByVal hBrush As Integer, _
ByVal lpDrawStateProc As Integer, _
ByVal lpString As String, _
ByVal cbStringLen As Integer, _
ByVal n1 As Integer, _
ByVal n2 As Integer, _
ByVal n3 As Integer, _
ByVal n4 As Integer, _
ByVal un As Integer) As Integer
Private Const DST_COMPLEX = &H0
Private Const DST_TEXT = &H1
Private Const DST_PREFIXTEXT = &H2
Private Const DST_ICON = &H3
Private Const DST_BITMAP = &H4
Private Const DSS_NORMAL = &H0
Private Const DSS_UNION = &H10
Private Const DSS_DISABLED = &H20
Private Const DSS_MONO = &H80
Private Const DSS_RIGHT = &H8000
'メニュー項目に表示させるアイコン
Private _icon As New Icon("open.ico")
'メニュー項目の描画
Private Sub MenuItem2_DrawItem(ByVal sender As System.Obj...
ByVal e As System.Windows.Forms.DrawItemEventArgs...
Handles MenuItem2.DrawItem
'無効状態のアイコンを描画
Dim hdc As IntPtr = e.Graphics.GetHdc()
If Not (_icon Is Nothing) Then
DrawState(hdc, 0, 0, _icon.Handle, 0, e.Bounds.Le...
e.Bounds.Top + (e.Bounds.Height - _icon.Heigh...
16, 16, DST_ICON Or DSS_DISABLED)
End If
'無効状態の文字列を描画
DrawStateString(hdc, 0, 0, sender.Text, LenB(sender.T...
e.Bounds.Left + 22, _
e.Bounds.Top + _
(e.Bounds.Height - SystemInformation.MenuFont.Hei...
e.Bounds.Width, e.Bounds.Height, _
DST_PREFIXTEXT Or DSS_DISABLED)
'開放
e.Graphics.ReleaseHdc(hdc)
End Sub
'VB6のLenBの代わりになるかもしれない関数
Private Function LenB(ByVal strString As String) As Integer
'Shift JISに変換したときに必要なバイト数を返す
Return System.Text.Encoding.GetEncoding(932). _
GetByteCount(strString)
End Function
}}
APIを使わずに同じことができないものか調べたところ、それら...
#code(vbnet){{
'無効状態のアイコンを描画
ControlPaint.DrawImageDisabled(e.Graphics, _
_image, x, y, SystemColors.Menu)
'無効状態の文字列を描画
ControlPaint.DrawStringDisabled(e.Graphics, _
text, SystemInformation.MenuFont, _
SystemColors.ControlLightLight, _
New RectangleF(point.X, point.Y, _
e.Bounds.Width, e.Bounds.Height), _
sf)
}}
これで期待通りの結果が得られるかといいますと、そううまく...
こうなったら自分でなんとかすることにしましょう。無効状態...
問題は画像の方です。ここでは確実に、元の画像を1ドットずつ...
(ここでは白のみを透明にしていますが、これには何の根拠も...
bmp.GetPixel(x, y).GetBrightness < 1.0F
の1.0Fを別の値にしたほうがいいのかもしれません。どの値が...
#code(vbnet){{
'無効状態の画像を作成する
Private Function CreateDisanabledImage(ByVal img As Image...
As Image
Dim x, y As Integer
If img Is Nothing Then
Return Nothing
End If
'無効状態の画像 元の画像より縦横1ドットずつ大きくなる
Dim dbmp As New Bitmap(img.Width + 1, img.Height + 1)
'もとの画像
Dim bmp As New Bitmap(img)
'1ドットずつ調べる
For y = 0 To bmp.Height - 1
For x = 0 To bmp.Width - 1
'元の色が透明か、白を透明にする
If bmp.GetPixel(x, y).A = &HFF AndAlso _
bmp.GetPixel(x, y).GetBrightness < 1....
dbmp.SetPixel(x, y, SystemColors.ControlD...
dbmp.SetPixel(x + 1, y + 1, _
SystemColors.ControlLightLight)
End If
Next
Next
bmp.Dispose()
Return dbmp
End Function
}}
次に2番目の問題に移りたいところですが、予想以上に長くなっ...
**コメント [#m023e7b4]
#comment
//これより下は編集しないでください
#pageinfo([[:Category/.NET]],2003-04-15 (火) 06:00:00,DOB...
ページ名:
▲
▼
[
トップ
] [
新規
|
子ページ作成
|
一覧
|
単語検索
|
最終更新
|
ヘルプ
]