Cod*_*les 10 .net c# pinvoke custom-controls winforms
我正在Rebar为.NET 做一个包装器.这是我如何控制自己.
public class Rebar : Control {
public Rebar() : base() {
//Control won't even work if I let UserPaint enabled
SetStyle(ControlStyles.UserPaint, false);
}
protected override CreateParams CreateParams {
get {
CreateParams cp = base.CreateParams;
cp.ClassName = "ReBarWindow32"; //REBARCLASSNAME
cp.ExStyle |= 0x00000080; //WS_EX_TOOLWINDOW
//Windows Forms will control the position and size, not the native control
cp.Style |= 0x00000004 | 0x00000008; //CCS_NORESIZE and CCS_NOPARENTALIGN
return cp;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我通过REBARBANDINFO在控件和IT WORKED中添加一个来测试我的控件.
REBARBANDINFO info = new REBARBANDINFO();
info.cbSize = Marshal.SizeOf(typeof(REBARBANDINFO));
info.fMask = RBBIM_TEXT; // 0x00000004
info.lpText = "example";
SendMessage(this.Handle, RB_INSERTBANDW, -1, ref myband);
Run Code Online (Sandbox Code Playgroud)
我不会包含我的p/invoke签名的实现,因为那里的一切都很好.
控件不能按我预期的方式工作,不遵守Rebar游标,Cursor属性控制游标,它甚至会覆盖调整大小光标.
看看这个例子ListView.可以创建一个尊重其原始游标消息的Control.
如何Rebar判断鼠标光标而不是Cursor属性?
有条件:我尽力提出一个好问题.我仔细检查了这个问题,以确保它能被理解.
Control类处理WM_SETCURSOR并有自己的逻辑。
作为一个选项,您可以覆盖WndProc并让DefWndProc句柄WM_SETCURSOR:
const int WM_SETCURSOR = 0x0020;
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_SETCURSOR)
base.DefWndProc(ref m);
else
base.WndProc(ref m);
}
Run Code Online (Sandbox Code Playgroud)