using this:[DllImport("user32.dll", EntryPoint="HideCaret")]
public static extern bool HideCaret (
IntPtr hwnd
);

解决方案 »

  1.   

    [DllImport("user32.dll", EntryPoint="HideCaret")]
    public static extern bool HideCaret (
    IntPtr hwnd
    );这段代码怎么放?能不能给个完整的例子?谢谢!
      

  2.   

    >>>>这段代码怎么放?能不能给个完整的例子?放在任何一个class中都可以,因为它是一个public static函数,调用的时候ClassName.HideCaret(..)就可以了
      

  3.   

    已经实现了,代码如下:
    --------------------------------------
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;namespace test
    {
    /// <summary>
    /// HideControlCaret 的摘要说明。
    /// </summary>
    public class HideControlCaret : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.TextBox txtForeColor;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; public HideControlCaret()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if(components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.txtForeColor = new System.Windows.Forms.TextBox();
    this.label1 = new System.Windows.Forms.Label();
    this.SuspendLayout();
    // 
    // txtForeColor
    // 
    this.txtForeColor.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(224)), ((System.Byte)(192)));
    this.txtForeColor.Cursor = System.Windows.Forms.Cursors.Default;
    this.txtForeColor.Location = new System.Drawing.Point(100, 36);
    this.txtForeColor.Name = "txtForeColor";
    this.txtForeColor.ReadOnly = true;
    this.txtForeColor.Size = new System.Drawing.Size(128, 21);
    this.txtForeColor.TabIndex = 3;
    this.txtForeColor.Text = "";
    this.txtForeColor.MouseDown += new System.Windows.Forms.MouseEventHandler(this.txtForeColor_MouseDown);
    // 
    // label1
    // 
    this.label1.Location = new System.Drawing.Point(20, 40);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(68, 16);
    this.label1.TabIndex = 4;
    this.label1.Text = "选择颜色:";
    // 
    // HideControlCaret
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(292, 129);
    this.Controls.Add(this.label1);
    this.Controls.Add(this.txtForeColor);
    this.Name = "HideControlCaret";
    this.Text = "HideControlCaret";
    this.Load += new System.EventHandler(this.HideControlCaret_Load);
    this.ResumeLayout(false); }
    #endregion
    [DllImport("user32.dll", EntryPoint="HideCaret")]
    public static extern bool HideCaret (IntPtr hwnd); private void txtForeColor_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    // show color picker dialog
    ColorDialog dlg = new ColorDialog();
    dlg.Color = txtForeColor.BackColor; // if the user clicked OK, set new border color
    if (dlg.ShowDialog() == DialogResult.OK)
    txtForeColor.BackColor = dlg.Color; HideCaret(txtForeColor.Handle);

    } private void HideControlCaret_Load(object sender, System.EventArgs e)
    {
    HideCaret(txtForeColor.Handle);
    } }
    }
    现在只是不能控制窗体加载时就让Textbox没有光标,
    本来想在HideControlCaret_Load中实现,但是好像不行。
      

  4.   

    Replace 
    private void HideControlCaret_Load(object sender, System.EventArgs e)
    {
    HideCaret(txtForeColor.Handle);
    }with 
    private void HideControlCaret_Activated(object sender, System.EventArgs e)
     {
        HideCaret(txtForeColor.Handle);
     }