C# と VB.NET の質問掲示板

ASP.NET、C++/CLI、Java 何でもどうぞ

ログ内検索
  • キーワードを複数指定する場合は 半角スペース で区切ってください。
  • 検索条件は、(AND)=[A かつ B] (OR)=[A または B] となっています。
  • [返信]をクリックすると返信ページへ移動します。
キーワード/ 検索条件 /
検索範囲/ 強調表示/ ON (自動リンクOFF)
結果表示件数/ 記事No検索/ ON
大文字と小文字を区別する

No.93012 の関連記事表示

<< 0 >>
■93012  子コントロールのマウスイベントを全て親コントロールに渡す方法
□投稿者/ RAGI -(2019/11/13(Wed) 22:52:24)

    分類:[C#] 

    https://qiita.com/yaju/items/b86be7b86c1e54e08e39

    ↑こちらの記事のように、ボタンの上にラベルを重ねたコントロールを作成しています。
    (ボタンの子コントロールとしてラベルを持たせている)

    このときラベルをクリックしても、ボタンのクリックイベントではなく、ラベルのクリックイベントが呼ばれるため何も反応しません。
    現在は解決策として、ラベルのクリックイベントでボタンのクリックイベントを呼び出すようにしています。

    同様にMouseEnterやMouseLeaveでもボタンのイベントが呼び出されるよう一つ一つ記述しているのですが、泥臭さが否めません。

    子コントロールのマウスイベントを全て親コントロールに受け流すようなスマートな方法はないでしょうか。



親記事 /過去ログ161より / 関連記事表示
削除チェック/

■93013  Re[1]: 子コントロールのマウスイベントを全て親コントロールに渡す方法
□投稿者/ KOZ -(2019/11/14(Thu) 01:00:59)
    No93012 (RAGI さん) に返信
    > https://qiita.com/yaju/items/b86be7b86c1e54e08e39
    > ↑こちらの記事のように、ボタンの上にラベルを重ねたコントロールを作成しています。
    > (ボタンの子コントロールとしてラベルを持たせている)
    
    私だったら文字は直接書いてしまいます。
    #プロパティに関しては、値が変わったら Invalidate を行うような処理が必要ですが、端折っています。
    
    using System.Drawing;
    using System.Windows.Forms;
    
    public class CommentButton : Button
    {
        public string CommentText { get; set; } = string.Empty;
        public Color CommentColor { get; set; } = SystemColors.WindowText;
        public Font CommentFont { get; set; } = Control.DefaultFont;
    
        protected override void OnPaint(PaintEventArgs pevent) {
            base.OnPaint(pevent);
            var g = pevent.Graphics;
            StringFormat stringFormat = new StringFormat();
            stringFormat.Alignment = StringAlignment.Center;
            stringFormat.LineAlignment = StringAlignment.Center;
            SizeF textSize = g.MeasureString(Text, Font, ClientSize, stringFormat);
            int commentHeight = (int)((ClientSize.Height - textSize.Height) / 2);
            int commentTop = ClientRectangle.Bottom - commentHeight;
            Rectangle commentBounds = new Rectangle(ClientRectangle.Left, commentTop, ClientSize.Width, commentHeight);
            using (var brush = new SolidBrush(CommentColor)) {
                g.DrawString(CommentText, CommentFont, brush, commentBounds, stringFormat);
            }
        }
    }
    
    使うときは
    
    public partial class Form1 : Form
    {
        public Form1() {
            InitializeComponent();
    
            var button = new CommentButton();
            button.Font = new Font("MS UI Gothic", 18F, FontStyle.Bold,GraphicsUnit.Point);
            button.Bounds = new Rectangle(50, 50, 300, 100);
            button.Text = @"注釈ありボタン";
            button.CommentText= @"※ 注釈コメントがついています。";
            button.CommentFont = new Font("MS UI Gothic", 15F, FontStyle.Bold, GraphicsUnit.Point);
            button.CommentColor = Color.Red;
            Controls.Add(button);
        }
    }
    
    
記事No.93012 のレス /過去ログ161より / 関連記事表示
削除チェック/

■93019  Re[2]: 子コントロールのマウスイベントを全て親コントロールに渡す方法
□投稿者/ RAGI -(2019/11/14(Thu) 23:17:18)
    No93013 (KOZ さん) に返信

    OnPaintは完全に盲点でした!
    ご提示いただいたコードでやりたかったことが完璧に実現できました。
    ありがとうございます!

記事No.93012 のレス / END /過去ログ161より / 関連記事表示
削除チェック/



<< 0 >>

パスワード/

- Child Tree -