开发者

How to set the margin with P/Invoke SendMessage?

开发者 https://www.devze.com 2022-12-13 08:10 出处:网络
This thread depends on How to add button to textbox?.开发者_StackOverflow社区 Thanks.Here\'s a textbox control that supports a RightMargin property.Tested on Win7:

This thread depends on How to add button to textbox?.开发者_StackOverflow社区

Thanks.


Here's a textbox control that supports a RightMargin property. Tested on 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);
}


According to 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));
0

精彩评论

暂无评论...
验证码 换一张
取 消