1设置WinForm窗体属性showinTask=false
2加notifyicon控件notifyIcon1,为控件notifyIcon1的属性Icon添加一个icon图标。
3添加窗体最小化事件(首先需要添加事件引用):
代码如下:
thisSizeChanged += new SystemEventHandler(thisForm1_SizeChanged);
//上面一行是主窗体InitializeComponent()方法中需要添加的引用
private void Form1_SizeChanged(object sender, EventArgs e)
{
if(thisWindowState == FormWindowStateMinimized)
{
thisHide();
thisnotifyIcon1Visible=true;
}
}
4添加点击图标事件(首先需要添加事件引用):
private void notifyIcon1_Click(object sender, EventArgs e)
{
thisVisible = true;
thisWindowState = FormWindowStateNormal;
thisnotifyIcon1Visible = false;
}
5可以给notifyIcon添加右键菜单:
主窗体中拖入一个ContextMenu控件NicontextMenu,点中控件,在上下文菜单中添加菜单,notifyIcon1的ContextMenu行为中选中NicontextMenu 作为上下文菜单。
代码如下:
thisnotifyIcon1 = new SystemWindowsFormsNotifyIcon(thiscomponents);
thisNicontextMenu = new SystemWindowsFormsContextMenu();
thismenuItem_Hide = new SystemWindowsFormsMenuItem();
thismenuItem_Show = new SystemWindowsFormsMenuItem();
thismenuItem_Aubot = new SystemWindowsFormsMenuItem();
thismenuItem_Exit = new SystemWindowsFormsMenuItem();
thisnotifyIcon1ContextMenu = thisNicontextMenu;
thisnotifyIcon1Icon = ((SystemDrawingIcon)(resourcesGetObject( "NotifyIconIcon ")));
thisnotifyIcon1Text = " ";
thisnotifyIcon1Visible = true;
thisnotifyIcon1DoubleClick += new SystemEventHandler(thisnotifyIcon1_DoubleClick);
thisnotifyIcon1Click += new SystemEventHandler(thisnotifyIcon1_Click);
thisNicontextMenuMenuItemsAddRange(
new SystemWindowsFormsMenuItem[]
{
thismenuItem_Hide,
thismenuItem_Show,
thismenuItem_Aubot,
thismenuItem_Exit
}
);
//
// menuItem_Hide
//
thismenuItem_HideIndex = 0;
thismenuItem_HideText = "隐藏 ";
thismenuItem_HideClick += new SystemEventHandler(thismenuItem_Hide_Click);
//
// menuItem_Show
//
thismenuItem_ShowIndex = 1;
thismenuItem_ShowText = "显示 ";
thismenuItem_ShowClick += new SystemEventHandler(thismenuItem_Show_Click);
//
// menuItem_Aubot
//
thismenuItem_AubotIndex = 2;
thismenuItem_AubotText = "关于 ";
thismenuItem_AubotClick += new SystemEventHandler(thismenuItem_Aubot_Click);
//
// menuItem_Exit
//
thismenuItem_ExitIndex = 3;
thismenuItem_ExitText = "退出 ";
thismenuItem_ExitClick += new SystemEventHandler(thismenuItem_Exit_Click);
protected override void OnClosing(CancelEventArgs e)
{
thisShowInTaskbar = false;
thisWindowState = FormWindowStateMinimized;
eCancel = true;
}
protected override void OnClosing(CancelEventArgs e)
{
//thisShowInTaskbar = false;
thisWindowState = FormWindowStateMinimized;
eCancel = true;
}
private void CloseCtiServer()
{
timerEnabled = false;
DJ160APIDisableCard();
thisNotifyIconVisible = false;
thisClose();
thisDispose();
ApplicationExit();
}
private void HideCtiServer()
{
thisHide();
}
private void ShowCtiServer()
{
thisShow();
thisWindowState = FormWindowStateNormal;
thisActivate();
}
private void CtiManiForm_Closing(object sender, SystemComponentModelCancelEventArgs e)
{
thisCloseCtiServer();
}
private void menuItem_Show_Click(object sender, SystemEventArgs e)
{
thisShowCtiServer();
}
private void menuItem_Aubot_Click(object sender, SystemEventArgs e)
{
}
private void menuItem_Exit_Click(object sen
启动Visual Studio Net 2005,创建C# Windows 窗体应用程序,将解决方案命名为TaskbarForm,包含的项目名也为TaskbarForm,首先创建程序的主窗体Form1,在上面添加两个Button控件,一个用于显示通知窗体,另一个则终止程序。然后在解决方案管理器中右击项目,单击"添加 - Windows 窗体",我们把新创建的窗体命名为TaskbarForm。
在类TaskbarForm定义的下方,我们创建用于显示的字符串和其颜色的变量,再定义几个Rectangle对象的变量用于放置标题、提示内容以及可以拖动窗体的区域和关闭按钮的区域。然后,我们需要保存窗体在浮动时的高度以便计算移动后的新高度,intervalValue变量用来确定窗体显示和隐藏的速度。进行平台调用时我们需要提前定义好常量的值用来传递给函数,WM_NCLBUTTONDOWN和HT_CAPTION常量用于拖动窗体,他们的值都保存在WinUserh头文件中,所对应的动态链接库名为:user32dll。我们用到的Win32API为:SendMessage、ReleaseCapture和ShowWindow,通过使用DllImportAttribute可以导入相应的函数并在程序中重新进行定义,如下:
[DllImportAttribute("user32dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
//发送消息//winuserh 中有函数原型定义
[DllImportAttribute("user32dll")]
public static extern bool ReleaseCapture(); //释放鼠标捕捉winuserh
[DllImportAttribute("user32dll")] //winuserh
private static extern Boolean ShowWindow(IntPtr hWnd, Int32 nCmdShow);
SendMessage向消息循环发送标题栏被按下的消息来模拟窗体的拖动,ShowWindow用来将特定句柄的窗体显示出来,注意第二个参数nCmdShow,它表示窗体应该怎样显示出来,而我们需要窗体不获得焦点显示出来,SW_SHOWNOACTIVATE可以满足我们要求,继续在WinUserh文件中搜索找到该常量对应的值为4,于是我们就可以这样调用来显示窗体了:
ShowWindow(thisHandle, 4);
我们创建了一个自定义函数ShowForm用来封装上面的ShowWindow用来是显示窗体,同时传递了所用到的几个Rectangle矩形区域对象,最后调用ShowWindows函数将窗体显示出来,代码片段如下:
public void ShowForm(string ftitletext, string fcontenttext, Rectangle fRegionofFormTitle, Rectangle fRegionofFormTitlebar, Rectangle fRegionofFormContent, Rectangle fRegionofCloseBtn)
{
titleText = ftitletext;
contentText = fcontenttext;
WorkAreaRectangle = ScreenGetWorkingArea(WorkAreaRectangle);
thisTop = WorkAreaRectangleHeight + thisHeight;
FormBorderStyle = FormBorderStyleNone;
WindowState = FormWindowStateNormal;
thisSetBounds(WorkAreaRectangleWidth - thisWidth, WorkAreaRectangleHeight - currentTop, thisWidth,thisHeight);
CurrentState = 1;
timer1Enabled = true;
TitleRectangle = fRegionofFormTitle;
TitlebarRectangle = fRegionofFormTitlebar;
ContentRectangle = fRegionofFormContent;
CloseBtnRectangle = fRegionofCloseBtn;
ShowWindow(thisHandle, 4); //#define SW_SHOWNOACTIVATE 4
}
CurrentState变量表示窗体的状态是显示中、停留中还是隐藏中,两个计时器根据窗体不同状态对窗体的位置进行更改,我们会使用SetBounds来执行该 *** 作:
thisSetBounds(WorkAreaRectangleWidth - thisWidth, WorkAreaRectangleHeight - currentTop, thisWidth, thisHeight);
当窗体需要升起时将窗体的Top属性值不断减少,而窗体回落时将Top属性值增加并超过屏幕的高度窗体就消失了,虽然原理很简单但仍需精确控制。
SetBackgroundBitmap函数首先将窗体背景图像保存到BackgroundBitmap变量中,然后根据该位图图像轮廓和透明色创建Region,BitmapToRegion就用于完成Bitmap到Region的转换,程序再将这个Region付值给窗体的Region属性以完成不规则窗体的创建。
public void SetBackgroundBitmap(Image image, Color transparencyColor)
{
BackgroundBitmap = new Bitmap(image);
Width = BackgroundBitmapWidth;
Height = BackgroundBitmapHeight;
Region = BitmapToRegion(BackgroundBitmap, transparencyColor);
}
public Region BitmapToRegion(Bitmap bitmap, Color transparencyColor)
窗体有Visible属性,设置为true窗体将显示出来,现将具体代码粘贴如下:
打开新的窗体Form2时隐藏原来的窗体Form1
Form2 fm = new Form2(this);
thisHide();//隐藏现在这个窗口
fmShow() ;//新窗口显现
关闭新的窗体Form2时显示原来的窗体Form1,
public partial class Form2 : Form
{
private Form1 returnForm1 = null;
public Form2(Form1 F1)
{
InitializeComponent();
// 接受Form1对象
thisreturnForm1 = F1;
}
}
为Form2添加Form2_FormClosing事件
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
//恢复Form1
thisreturnForm1Visible = true;
}
以上就是关于【C#】WinForm中如何让窗口最小化时直接最小化到托盘,后台运行全部的内容,包括:【C#】WinForm中如何让窗口最小化时直接最小化到托盘,后台运行、winform程序如何实现任务栏通知窗口、winform窗口程序的隐藏等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)