How to handle Form caption right click

后端 未结 6 900
感情败类
感情败类 2021-01-07 10:09

I\'d like a context menu on the caption bar right click

any tips/samples pref in c# ?

UPDATE - for various reasons, right click on the form won\'t work becau

6条回答
  •  一个人的身影
    2021-01-07 11:11

    You can do this by trapping the WM_NCRBUTTONDOWN notification that Windows sends when the user right-clicks the title bar. The control class does not have an event for it, you'll need to override WndProc(). Here's an example form, you'll need to add a ContextMenuStrip:

    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }
    
        protected void OnTitlebarClick(Point pos) {
            contextMenuStrip1.Show(pos);
        }
    
        protected override void WndProc(ref Message m) {
            const int WM_NCRBUTTONDOWN = 0xa4;
            if (m.Msg == WM_NCRBUTTONDOWN) { 
                var pos = new Point(m.LParam.ToInt32());
                OnTitlebarClick(pos);
                return;
            }                                           
            base.WndProc(ref m);
        }
    }
    

提交回复
热议问题