- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
因此,我们决定为我们的聊天应用程序添加某种 Toast 功能是个好主意,它实际上工作正常,但是,当表单显示时,它会短暂地窃取焦点,这可能使聊天输入框(当您在其中输入内容时)闪烁或清除所有文本(因为焦点从它那里被偷走了)。
我已经查看了该网站上的几个线程,关于如何通过覆盖 createparms
甚至执行 showwithoutactivating
或类似操作来阻止它发生,但它不起作用非常正确。
这就是我所拥有的(对于所有的评论我深表歉意,我们的老板希望一切都记录在案):
public partial class ToastForm : DevComponents.DotNetBar.Metro.MetroForm
{
#region Variables
private readonly int _location;
/// <summary>
/// The list of currently open ToastForms.
/// </summary>
private static readonly List<ToastForm> OpenForms = new List<ToastForm>();
/// <summary>
/// Set the window to top most
/// </summary>
private const int WsExTopmost = 0x00000008;
#endregion // Variables
#region Constructors
/// <summary>
/// Creates a new ToastForm object that is displayed for the specified length of time.
/// </summary>
/// <param name="lifeTime">
/// The length of time, in milliseconds, that the form will be displayed.
/// </param>
/// <param name="title">title of tooltip</param>
/// <param name="message">message for tooltip</param>
/// <param name="location">Location: 0 = top left, 1 = top right, 2 = bottom left, 3 = bottom right</param>
public ToastForm(int lifeTime,
string title,
string message,
int location)
{
InitializeComponent();
// Set the time for which the form should be displayed and the message to display.
lifeTimer.Interval = lifeTime;
tooltipTitle.Text =
string.Format("<b><font size=\"+6\"><i>New</i><font color=\"#B02B2C\">{0}</font></font></b>",
title);
tooltipText.Text = message;
_location = location;
}
/// <summary>
/// Do not activate the window just show it
/// </summary>
protected override bool ShowWithoutActivation
{
get { return true; }
}
/// <summary>
/// Force the ExStyle to be TopMost
/// </summary>
protected override CreateParams CreateParams
{
get
{
var param = base.CreateParams;
param.ExStyle |= WsExTopmost; // make the form topmost
return param;
}
}
#endregion // Constructors
#region Event Handlers
/// <summary>
/// Do this when the form loads
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ToastFormLoad(object sender,
EventArgs e)
{
switch (_location)
{
case 0: // top left corner of screen
Location = new Point(0,
0);
break;
case 1: // top right corner of screen
Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - Width - 5,
0);
break;
case 2: // bottom left corner of screen
Location = new Point(0,
Screen.PrimaryScreen.WorkingArea.Height - Height - 5);
break;
case 3: // bottom right corner of screen
Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - Width - 5,
Screen.PrimaryScreen.WorkingArea.Height - Height - 5);
break;
}
// Move each open form upwards to make room for this one.
foreach (var openForm in OpenForms)
{
switch (_location)
{
case 0:
case 1:
openForm.Top += Height + 5;
break;
case 2:
case 3:
openForm.Top -= Height + 5;
break;
}
}
OpenForms.Add(this);
lifeTimer.Start();
}
/// <summary>
/// Happens when the form closes
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ToastFormFormClosed(object sender,
FormClosedEventArgs e)
{
// Move down any open forms above this one.
foreach (var openForm in OpenForms.TakeWhile(openForm => openForm != this))
{
switch (_location)
{
case 0:
openForm.Top -= Height + 5;
break;
case 1:
openForm.Top -= Height + 5;
break;
case 2:
openForm.Top += Height + 5;
break;
case 3:
openForm.Top += Height + 5;
break;
}
}
// Remove this form from the open form list.
OpenForms.Remove(this);
}
/// <summary>
/// If the tooltip has expired
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void LifeTimerTick(object sender,
EventArgs e)
{
// The form's lifetime has expired.
Close();
}
#endregion // Event Handlers
#region Methods
/// <summary>
/// Quickly close the tooltip
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void TooltipCancelClick(object sender,
EventArgs e)
{
lifeTimer.Interval = 1;
}
#endregion
}
我们根据主窗体中的事件触发这些弹出窗口(比如当一条新的公共(public)消息进来时,通过引用另一个类中的命令(在我们主窗体的类中节省空间):
class ToastControl
{
public static int SliceCount { get; private set; }
internal static void ShowAlert(string msg, string title, Font fnt)
{
switch (Settings.Default.PopUpSide)
{
case 0:
AlertTopLeft(msg, title, fnt);
break;
case 1:
AlertTopRight(msg, title, fnt);
break;
case 2:
AlertBottomLeft(msg, title, fnt);
break;
case 3:
AlertBottomRight(msg, title, fnt);
break;
default:
AlertBottomRight(msg, title, fnt);
break;
}
}
internal static void AlertBottomRight(string msg, string title, Font fnt)
{
SliceCount += 1;
var slice = new ToastForm(5000,
title,
msg, 3)
{
Height = (25 + 82) + ((int)(msg.Length / fnt.Size)) * 2
};
slice.Show();
}
internal static void AlertBottomLeft(string msg, string title, Font fnt)
{
SliceCount += 1;
var slice = new ToastForm(5000,
title,
msg,
2)
{
Height = (25 + 82) + ((int)(msg.Length / fnt.Size)) * 2
};
slice.Show();
}
internal static void AlertTopLeft(string msg, string title, Font fnt)
{
SliceCount += 1;
var slice = new ToastForm(5000,
title,
msg, 0)
{
Height = (25 + 82) + ((int)(msg.Length / fnt.Size)) * 2
};
slice.Show();
}
internal static void AlertTopRight(string msg, string title, Font fnt)
{
SliceCount += 1;
var slice = new ToastForm(5000,
title,
msg, 1)
{
Height = (25 + 82) + ((int)(msg.Length / fnt.Size)) * 2
};
slice.Show();
}
}
以及调用此类的示例:
if (Settings.Default.PopUpEnabledChat)
{
if (!(Settings.Default.NoAlerts))
ToastControl.ShowAlert(string.Format("{0}: {1}", user.Nick, description.Replace("\r",
"").Replace("\n",
"").Replace("\0",
"")), channel, Font);
}
如何在不从主应用程序窃取焦点的情况下显示此通知表单?
最佳答案
看起来解决方案与我所做的差不多,只是我需要将以下内容添加到通知表单中:
/// <summary>
/// Do not activate the window just show it
/// </summary>
protected override bool ShowWithoutActivation
{
get { return true; }
}
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x00000008; //WS_EX_TOPMOST
return cp;
}
}
并更改 Toast Control 类以使用“Visible”而不是“.Show”:
internal static void AlertBottomRight(string msg, string title, Font fnt)
{
SliceCount += 1;
new ToastForm(5000,
title,
msg,
3) {
Height = (25 + 82) + ((int) (msg.Length / fnt.Size)) * 2,
Visible = true
};
}
internal static void AlertBottomLeft(string msg, string title, Font fnt)
{
SliceCount += 1;
new ToastForm(5000,
title,
msg,
2) {
Height = (25 + 82) + ((int) (msg.Length / fnt.Size)) * 2,
Visible = true
};
}
internal static void AlertTopLeft(string msg, string title, Font fnt)
{
SliceCount += 1;
new ToastForm(5000,
title,
msg,
0) {
Height = (25 + 82) + ((int) (msg.Length / fnt.Size)) * 2,
Visible = true
};
}
internal static void AlertTopRight(string msg, string title, Font fnt)
{
SliceCount += 1;
new ToastForm(5000,
title,
msg,
1) {
Height = (25 + 82) + ((int) (msg.Length / fnt.Size)) * 2,
Visible = true
};
}
现在当显示通知时,它们不会窃取焦点,而且它们在其他窗口的顶部显示得很好 :)
关于C# 显示通知表单而不关注,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8424217/
众所周知,jQuery 的 $.post 函数非常棒,但我遇到的问题是查看页面源代码的人可以查看数据的去向,从而移向该页面进行窥探,或者,上帝禁止找到保存所有内容的文件夹。所以我的问题是,谁知道如何隐
我在下面有这个程序,它执行简单的关注/取消关注功能。一切都很好,除了当我刷新页面时,只有行中的第一个用户保留正确的关注/取消关注按钮。示例 我可以关注 user1 user2 和 user3,但是当我
我想要创建的是一个关注者/关注系统,您不是简单地关注用户,而是关注他们共享的内容部分。几乎就好像您关注的是 Twitter 的“列表”或群组而不是人员。不过,有了这个,您就可以关注/取消关注用户共享的
这个问题已经有答案了: facebook social plug-in not showing up when added dynamically (2 个回答) 已关闭 7 年前。 使用 HTML
我正在构建一个编辑器,它使用 TImage 来显示图片,并具有鼠标事件来能够在图像上绘制、移动框和调整框大小。这一切都很完美。现在我正在尝试实现使用键盘上的箭头移动选定框的功能,但是A)TImage没
我有两个问题,请记住我是一个java新手1.我有一个使用 JFrame 创建 GUI 的类。JFrame 有 2 个面板,我使用 JSplitPane 添加了 问题是我可以设法将焦点设置在所需的 JP
我目前正在使用iOS应用程序进行开发,该应用程序会从流式API捕获一些推文。因此,我使用用户名和密码进行身份验证。除此之外,我想给用户提供在Twitter上关注某些人的机会。我创建了一个UIButto
有没有办法钩入Play evolutions framework这样当它成功从 n.sql 迁移时至 n+1.sql至 n+2.sql ...,它在 Play 应用程序中调用了一些成功后 Hook (
我的 gorm 中有文本模式为多行的文本框。我必须通过 jQuery 将 css 应用到该文本框。为此,我使用了以下脚本。 $(document).ready(function() {
我在强制关注动态生成的 JQuery 对话框内容中的文本字段时遇到问题。我已经在 google 上搜索过这一点,似乎如果 Jquery 对话框设置为模式,JQuery 将“窃取”文档级别的焦点。老实说
下午,我正在使用 PHP、HTML5 和 Bootstrap。我构建了一个分为 5 个选项卡的表单,该表单中有几个必填字段。所有必需的输入字段都是“文本”,并且还标有 required="requir
我创建了一个带有 GridView 的 WPF 页面。在 GridView 中,每行有 5 个可用的 TextBox。当我在第一行的第一个 TextBox 上输入数据,然后按 Tab 键时,焦点移动到
请给我Java中密码验证的正则表达式代码,它应该由一个大写字符、一个整数、一个后面的符号(@、#、$、%、^、&、+、=)和小字符组成。 我一直在尝试使用不同的独立正则表达式和一个组合的正则表达式。
我想在我的 mean-stack 网页上添加一个 Twitter 的关注按钮。我使用以下代码: https://jsbin.com/herikik/3/edit?html,output 在 Ma
在下添加如下代码后到我在 Tumblr 上的主题 .tail { position:fixed; bottom:0px; right:0px; margin-bottom:434px; margin-
我必须从 Angular 应用程序启动一系列窗口。我希望能够让用户单击主页上的按钮以使该窗口重新成为焦点。通常我会在 javascript 中使用类似以下内容来执行此操作: //Launch the
因此,我想显示一些用 AND 或 OR 连接的规则,并且我想为 AND 或 OR 添加颜色,如红色、绿色等。 Fruit = Apple AND Market = SuperMarket1 那么我应该
我正在开发 Windows 商店应用程序,我正在使用 ListView 控件动态添加数据。这些项目被添加到列表的末尾。 Scrollbar 在添加更多数据时出现。我想用底部的滚动条以编程方式突出显示最
(问题仅在 Ubuntu 中出现。在 Windows 中工作正常。我不知道在其他 Linux 环境中) 我已经使用 ComponentListener 的方法在对话框中调用 JTextField 中的
如何将焦点放在时间选择器元素上?我正在开发电视应用程序,因此需要远程操作。所以我需要关注每个元素。TimePicker 有 3 个元素 - 小时列、分钟列和 AM/PM 列。 那么我如何才能专注于这
我是一名优秀的程序员,十分优秀!