【问题标题】:How to set the margin with P/Invoke SendMessage?如何使用 P/Invoke SendMessage 设置边距?
【发布时间】:2009-12-08 10:20:52
【问题描述】:

这个线程依赖于How to add button to textbox?

谢谢。

【问题讨论】:

  • 如何使用 P/Invoke SendMessage 和 M_SETMARGINS 设置右边距?

标签: c# .net sendmessage


【解决方案1】:

这是一个支持 RightMargin 属性的文本框控件。在 Win7 上测试:

using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.InteropServices;

class MyTextBox : TextBox {
  private int mRightMargin;

  [DefaultValue(0)]
  public int RightMargin {
    get { return mRightMargin; }
    set {
      if (value < 0) throw new ArgumentOutOfRangeException();
      mRightMargin = value;
      if (this.IsHandleCreated) updateMargin();
    }
  }

  protected override void OnHandleCreated(EventArgs e) {
    base.OnHandleCreated(e);
    if (mRightMargin > 0) updateMargin();
  }

  private void updateMargin() {
    // Send EM_SETMARGINS
    SendMessage(this.Handle, 0xd3, (IntPtr)2, (IntPtr)(mRightMargin << 16));
  }

  [DllImport("user32.dll", CharSet = CharSet.Auto)]
  private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
}

【讨论】:

    【解决方案2】:

    根据the documentation

    [DllImport("user32.dll", CharSet=CharSet.Auto)]
    static extern IntPtr SendMessage(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam);
    
    ...
    
    SendMessage(hwnd, EM_SETMARGINS, (IntPtr)EC_RIGHTMARGIN, (IntPtr)(rightMargin << 16));
    

    【讨论】:

      猜你喜欢
      • 2022-06-10
      • 2023-04-10
      • 1970-01-01
      • 2010-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-27
      • 1970-01-01
      相关资源
      最近更新 更多