- 題名: ピクチャーボックスにラベルを重ねた時のこと
- 日時: 2012/03/30 21:35:24
- ID: 30187
- この記事の返信元: - (なし)
 
- この記事への返信: - [30188] Re[1]: ピクチャーボックスにラベルを重ねた時のこと2012/03/30 23:20:05
- [30190] Re[1]: ピクチャーボックスにラベルを重ねた時のこと2012/03/30 23:41:55
 
- ツリーを表示
■No30187に返信(dropitem92さんの記事)
> ただ上にラベルをおき、バックカラーをTransparencyにしても
> ピクチャーボックスに描画された絵が表示されません。
描画した絵を、BackgroundImage や Image に配置しておくと表示されるかと。
Imports Microsoft.VisualBasic
Imports System
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Threading
Imports System.Windows.Forms
Partial Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        PictureBox1.Controls.Add(Label1)
        Label1.BackColor = Color.Transparent
        Label1.Location = New Point(5, 5)
        Timer1.Start()
    End Sub
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Static r As Integer = Now.Minute * 256 \ 60
        Static g As Integer = Now.Second * 256 \ 60
        Static b As Integer = Now.Millisecond * 256 \ 1000
        Label1.Text = Now.ToString("HH:mm:ss.ffff")
        Dim bmp As New Bitmap(PictureBox1.Width, PictureBox1.Height)
        Using gr As Graphics = Graphics.FromImage(bmp), pa As New GraphicsPath()
            gr.Clear(Color.White)
            pa.AddEllipse(10, 10, 200, 100)
            Using br As New PathGradientBrush(pa)
                br.CenterColor = Color.FromArgb(r, g, b)
                r = Now.Millisecond * 256 \ 1000
                g = Now.Second * 256 \ 60
                b = Now.Minute * 256 \ 60
                br.SurroundColors = New Color() {Color.FromArgb(r, g, b)}
                gr.FillPath(br, pa)
            End Using
        End Using
        Dim oldImg As Image = Interlocked.Exchange(PictureBox1.Image, bmp)
        If oldImg IsNot Nothing Then
            oldImg.Dispose()
        End If
    End Sub
End Class
こちらは Paint イベントで実装してみた場合。
ラベルの下の楕円はグラデーションがかからず、単一色で塗られてしまう…?
Imports Microsoft.VisualBasic
Imports System
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Threading
Imports System.Windows.Forms
Partial Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        PictureBox1.Controls.Add(Label1)
        Label1.BackColor = Color.Transparent
        Label1.Location = New Point(5, 5)
        Timer1.Start()
    End Sub
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        PictureBox1.Refresh()   'Invalidateでも可
        Label1.Text = Now.ToString("HH:mm:ss.ffff")
    End Sub
    Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
        Static r As Integer = Now.Minute * 256 \ 60
        Static g As Integer = Now.Second * 256 \ 60
        Static b As Integer = Now.Millisecond * 256 \ 1000
        Dim bmp As New Bitmap(PictureBox1.Width, PictureBox1.Height)
        Using pa As New GraphicsPath()
            e.Graphics.Clear(Color.White)
            pa.AddEllipse(10, 10, 200, 100)
            Using br As New PathGradientBrush(pa)
                br.CenterColor = Color.FromArgb(r, g, b)
                r = Now.Millisecond * 256 \ 1000
                g = Now.Second * 256 \ 60
                b = Now.Minute * 256 \ 60
                br.SurroundColors = New Color() {Color.FromArgb(r, g, b)}
                e.Graphics.FillPath(br, pa)
            End Using
        End Using
    End Sub
End Class
分類:[.NET]
前回、円グラデーションの質問をしました。
今回はそのピクチャーボックスの上にラベルをかさねました。
picturebox1.controls.add(label1)
label1.location = new point(0,0)
としました。
タイマーを使用し時間で
ピクチャーボックスに円を描画したり再描画したりしています。
ただ上にラベルをおき、バックカラーをTransparencyにしても
ピクチャーボックスに描画された絵が表示されません。
ラベルの設定の仕方がいけないのでしょうか?
教えてください。
ラベルのtextプロパティには文字列が設定されています。