在windows系统中,当程序在后台运行时,如果某个窗体的提示信息需要用户浏览,该窗体就会不停地闪烁,这样就会吸引用户的注意。同样,如果在自己的程序中使某个窗体不停地闪烁也会吸引用户的注意。
1.api函数flashwindow
api函数flashwindow用于闪烁窗口。该函数通过在窗口上显示和隐藏光标来实现闪烁效果。
(1)添加命名空间
在c#中,可以调用windows api中的flashwindow函数。首先添加以下命名空间以使用dllimport特性:
using system.runtime.interopservices;
(2)声明dllimport方法
接下来,声明flashwindow函数的dllimport方法:
[dllimport("user32.dll", setlasterror = true)]
public static extern bool flashwindow(intptr hwnd, bool binvert);
声明dllimport时,vs会提示警告信息:sys1054,并建议修改为libraryimport。不要理睬,因为修改后更严重。作者认为不是微软的普遍性建议错了,而是最新的libraryimport不适合c++的原生方法flashwindow(c++跟不上发展了)。对于flashwindow只能使用古老的dllimport p/invoke 源生成器 。
(3)flashwindow函数
现在,您可以使用以下示例代码在c#中使用flashwindow函数:
// 让窗体闪烁起来
using system.runtime.interopservices;
namespace _168_2
{
class program
{
[dllimport("user32.dll", setlasterror = true)]
public static extern bool flashwindow(intptr hwnd, bool binvert);
[dllimport("user32.dll", setlasterror = true, charset = charset.unicode)]
public static extern intptr findwindow(string? lpclassname, string lpwindowname);
static void main(string[] args)
{
argumentnullexception.throwifnull(args);
// 获取当前窗口的句柄
intptr hwnd = findwindow(null, "新建文本文档 - 记事本");
if (hwnd != intptr.zero)
{
while (true)
{
// 闪烁窗口
flashwindow(hwnd, true);
console.writeline("窗口闪烁成功!");
// 等待2秒钟
thread.sleep(2000);
}
}
else
{
console.writeline("未找到指定的窗口。");
}
console.readkey();
}
}
}
本实例运行之前,打开一个记事本文件,空白的,其标题栏显示:"新建文本文档 - 记事本"。然后运行本程序,如果找到了这个记事本文件,控制台显示“窗口闪烁成功”并记事本文件的的标题栏同步闪烁。否则,结果相反。
2.p/invoke
p/invoke(platform invoke)是一种由.net框架提供的技术,用于调用本机代码(即非托管代码)。它允许托管应用程序(即由.net运行时管理的代码)访问本机api函数,例如windows api函数。
当托管应用程序使用p/invoke调用本机函数时,.net运行时会将托管代码与本机代码之间进行转换,确保两种类型的代码可以相互交互。这涉及处理诸如内存分配,数据类型和异常处理等底层细节。
为了使用p/invoke,需要使用dllimport属性将本机函数导入托管代码中。dllimport属性指示该函数的名称,它所在的库以及传递给该函数的参数的类型。一旦导入了本机函数,就可以像调用托管函数一样调用它。但是,需要确保本机函数的签名与在dllimport属性中指定的签名匹配,包括参数的数量和类型以及返回类型。
3.再来一个示例
本实例设计了一个闪烁的窗体标题栏,运行本实例,单击“开始闪烁”按钮,窗体标题栏就会不停地闪烁;单击“停止闪烁”按钮,窗体标题栏就会停止闪烁。
// 闪烁的标题栏
using system.windows.forms;
namespace _168
{
public partial class form1 : form
{
private picturebox? picturebox1;
private button? button1;
private button? button2;
private system.windows.forms.timer? timer1;
public form1()
{
initializecomponent();
startposition = formstartposition.centerscreen;
load += form1_load;
}
//重写api函数,用来实现窗体标题栏闪烁功能
[system.runtime.interopservices.dllimport("user32.dll")]
public static extern bool flashwindow(intptr handle, bool binvert);
private void timer1_tick(object? sender, eventargs e)
{
flashwindow(handle, true);//启用窗体闪烁函数
}
private void button1_click(object? sender, eventargs e)
{
timer1!.enabled = true;//启动计时器
}
private void button2_click(object? sender, eventargs e)
{
timer1!.enabled = false;//关闭计时器
}
private void form1_load(object? sender, eventargs e)
{
//
// button1
//
button1 = new button
{
location = new point(45, 50),
name = "button1",
size = new size(75, 23),
tabindex = 1,
text = "开始闪烁",
usevisualstylebackcolor = true
};
button1.click += new eventhandler(button1_click);
//
// button2
//
button2 = new button
{
location = new point(150, 50),
name = "button2",
size = new size(75, 23),
tabindex = 2,
text = "停止闪烁",
usevisualstylebackcolor = true
};
button2.click += new eventhandler(button2_click);
//
// picturebox1
//
picturebox1 = new picturebox
{
dock = dockstyle.fill,
location = new point(0, 0),
name = "picturebox1",
size = new size(325, 127),
tabindex = 0,
tabstop = false
};
picturebox1.controls.add(button2);
picturebox1.controls.add(button1);
//
// timer1
//
timer1 = new system.windows.forms.timer(components);
timer1.tick += new eventhandler(timer1_tick);
//
// form1
//
autoscaledimensions = new sizef(7f, 17f);
autoscalemode = autoscalemode.font;
clientsize = new size(266, 155);
controls.add(picturebox1);
name = "frm_main";
name = "form1";
text = "闪烁的标题栏";
resumelayout(false);
}
}
}

以上就是c#实现标题闪烁效果的示例代码的详细内容,更多关于c#标题闪烁的资料请关注代码网其它相关文章!
发表评论