【问题标题】:Form without focus/activation没有焦点/激活的表格
【发布时间】:2011-09-13 19:28:34
【问题描述】:

我想为我的多行文本框实现类似智能感知的功能。智能感知控件以没有控制框的标准形式放置(因此,没有标题或最大化/最小化按钮可见)。

一切正常,但如果显示 intellisense-form 并且用户单击 intellisense 表单,则主表单失去焦点(因此,用户必须单击返回到文本框才能写入)。

我知道ShowWithoutActivation 属性,但它只适用于激活,而不适用于“标准焦点”。

编辑:

我在 http://www.daniweb.com/software-development/csharp/threads/273724 上找到了帮助,但提供的代码不起作用。在“Show()”方法中抛出“Invalid parameter”异常。

【问题讨论】:

  • 为什么你不使用控件而不是智能感知表单?
  • 因为客户希望智能感知表单可以从主表单溢出。
  • 显然您的“智能控制”不是一个控制。很难猜测它到底是一种什么样的动物。如果是 Form 则将其 TopLevel 属性设置为 false 以将其变为控件。
  • @Hans Passant:UserControl 处于状态。表单通过“Show()”方法显示。属性“TopMost”设置为“true”,但与表单焦点无关:(.
  • 嗯,当然是。控件没有 TopMost 属性。显然它是一个表单,使用 TopLevel 属性。

标签: winforms focus activation


【解决方案1】:

要在不激活的情况下显示表单,请覆盖 ShowWithoutActivation 属性

protected override bool ShowWithoutActivation
{
  get { return true; }
}

如果您不想在单击鼠标时激活表单,请覆盖 CreateParams 并设置这些样式

protected override CreateParams CreateParams
{
  get
  {
    CreateParams p = base.CreateParams;

    p.Style |= 0x40000000; // WS_CHILD
    p.ExStyle |= 0x8000000; // WS_EX_NOACTIVATE - requires Win 2000 or higher :)

    return p;
  }
}

【讨论】:

  • ExStyle 是一个 int,所以它不能在最近的 .NET 版本上编译。此外,关于调整 CreateParams 的最后一个建议,即使修改为编译,也不起作用。你只是得到“错误创建窗口句柄”Win32Exception
【解决方案2】:

有一天我从代码项目(我认为)下载了一个代码,但我不知道原始下载链接是什么,尝试使用这个

using System;
using System.Drawing;
using System.Windows.Forms;

namespace Balloon.NET
{
    public class BalloonWindow : Form
    {
        public static readonly int TIPMARGIN;
        public static readonly int TIPTAIL;

        public BalloonWindow();

        public Point AnchorPoint { get; set; }
        public BalloonWindow.BallonQuadrant Quadrant { get; }

        public static Point AnchorPointFromControl(Control anchorControl);
        protected override void Dispose(bool disposing);
        protected override void OnLoad(EventArgs e);
        protected virtual Rectangle OnNCCalcSize(Rectangle windowRect);
        protected virtual void OnNCPaint(Graphics g);
        protected override void OnResize(EventArgs e);
        protected void RecalcLayout();
        protected void RepositionWindow(Point oldAnchorPoint, Point newAnchorPoint);
        public void ShowBalloon(Control anchorControl);
        protected override void WndProc(ref Message m);

        public enum BallonQuadrant
        {
            TopLeft = 0,
            TopRight = 1,
            BottomLeft = 2,
            BottomRight = 3,
        }
    }
}

并按如下方式使用此表格

Balloon.NET.BalloonWindow ms = new Balloon.NET.BalloonWindow();
private void numberEdit1_TextChanged(object sender, EventArgs e)
{
    if (!ms.Visible)
    {
        ms.ShowBalloon(numberEdit1);
        numberEdit1.Focus();
    }
}

【讨论】:

  • 看起来很有用,我试试。谢谢!
猜你喜欢
  • 2019-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多