• winform 消息通讯组件实习


    最近在项目上要做一个消息推送的组件,实习医生站和护士站通讯的组件,医生站开立医嘱后通知护士需要审核医嘱,会诊医嘱审核之后通知会诊医生会诊,我相信在其他项目上也会用到,在这里先总结一下,留着备用。废话少说,直奔主题。

      第一步,创建l两个个表,如果不用表也可以用xml存。

           脚步如下:

       

     1 -- Create table
     2 create table COM_SYS_MSG
     3 (
     4   msg_id       NUMBER(10) not null,
     5   msg_title    VARCHAR2(50),
     6   msg_content  VARCHAR2(100),
     7   publisher    VARCHAR2(6),
     8   publish_time DATE,
     9   limited_area VARCHAR2(50) default 'ALL',
    10   is_valid     CHAR(1) default '0',
    11   extend1      VARCHAR2(100),
    12   extend2      VARCHAR2(100),
    13   extend3      VARCHAR2(100),
    14   role_area    VARCHAR2(50) default 'ALL'
    15 )
    16 tablespace USERS
    17   pctfree 10
    18   initrans 1
    19   maxtrans 255
    20   storage
    21   (
    22     initial 192K
    23     next 1M
    24     minextents 1
    25     maxextents unlimited
    26   );
    27 -- Add comments to the table 
    28 comment on table COM_SYS_MSG
    29   is '系统消息表,用于群发消息';
    30 -- Add comments to the columns 
    31 comment on column COM_SYS_MSG.msg_id
    32   is '消息编码';
    33 comment on column COM_SYS_MSG.msg_title
    34   is '消息标题';
    35 comment on column COM_SYS_MSG.msg_content
    36   is '消息内容';
    37 comment on column COM_SYS_MSG.publisher
    38   is '消息发布者';
    39 comment on column COM_SYS_MSG.publish_time
    40   is '消息发布时间';
    41 comment on column COM_SYS_MSG.limited_area
    42   is '限制范围:缺省为ALL——全部范围';
    43 comment on column COM_SYS_MSG.is_valid
    44   is '是否有效,0-无效,1-有效,缺省发布消息为0,需要确认发布才更新为1';
    45 comment on column COM_SYS_MSG.extend1
    46   is '审核人';
    47 comment on column COM_SYS_MSG.extend2
    48   is '接收科室';
    49 comment on column COM_SYS_MSG.role_area
    50   is '角色范围:缺省为ALL——全部范围';
    51 -- Create/Recreate indexes 
    52 create index IDX_CSM_TIME on COM_SYS_MSG (PUBLISH_TIME)
    53   tablespace USERS
    54   pctfree 10
    55   initrans 2
    56   maxtrans 255
    57   storage
    58   (
    59     initial 64K
    60     next 1M
    61     minextents 1
    62     maxextents unlimited
    63   );
    64 -- Create/Recreate primary, unique and foreign key constraints 
    65 alter table COM_SYS_MSG
    66   add constraint PK_COM_SYS_MSG primary key (MSG_ID)
    67   using index 
    68   tablespace USERS
    69   pctfree 10
    70   initrans 2
    71   maxtrans 255
    72   storage
    73   (
    74     initial 64K
    75     next 1M
    76     minextents 1
    77     maxextents unlimited
    78   );
     1 -- Create table
     2 create table COM_IP_MSG
     3 (
     4   ip_address     VARCHAR2(50) not null,
     5   last_view_time DATE,
     6   extend1        VARCHAR2(100),
     7   extend2        VARCHAR2(100),
     8   extend3        VARCHAR2(100)
     9 )
    10 tablespace USERS
    11   pctfree 10
    12   initrans 1
    13   maxtrans 255
    14   storage
    15   (
    16     initial 64K
    17     next 1M
    18     minextents 1
    19     maxextents unlimited
    20   );
    21 -- Add comments to the table 
    22 comment on table COM_IP_MSG
    23   is '每个IP地址的最后获取消息记录';
    24 -- Add comments to the columns 
    25 comment on column COM_IP_MSG.ip_address
    26   is 'IP地址';
    27 comment on column COM_IP_MSG.last_view_time
    28   is '最后获取消息的时间';
    29 -- Create/Recreate primary, unique and foreign key constraints 
    30 alter table COM_IP_MSG
    31   add constraint PK_CIM_IP primary key (IP_ADDRESS)
    32   using index 
    33   tablespace USERS
    34   pctfree 10
    35   initrans 2
    36   maxtrans 255
    37   storage
    38   (
    39     initial 64K
    40     next 1M
    41     minextents 1
    42     maxextents unlimited
    43   );

     第二步:

        创建弹窗代码

       1、准备两张资源图片文件

        效果如下:

     2,代码实现:

       

    // C# TaskbarNotifier Class v1.0
    // by John O'Byrne - 02 december 2002
    // 01 april 2003 : Small fix in the OnMouseUp handler
    // 11 january 2003 : Patrick Vanden Driessche <pvdd@devbrains.be> added a few enhancements
    //           Small Enhancements/Bugfix
    //           Small bugfix: When Content text measures larger than the corresponding ContentRectangle
    //                         the focus rectangle was not correctly drawn. This has been solved.
    //           Added KeepVisibleOnMouseOver
    //           Added ReShowOnMouseOver
    //           Added If the Title or Content are too long to fit in the corresponding Rectangles,
    //                 the text is truncateed and the ellipses are appended (StringTrimming).
    
    using System;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.Drawing.Drawing2D;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    
    namespace MyTaskbarNotifier
    {
    
    /// <summary>
    	/// TaskbarNotifier allows to display MSN style/Skinned instant messaging popups
    	/// </summary>
    	public class TaskbarNotifier : System.Windows.Forms.Form
    	{
    		#region TaskbarNotifier Protected Members
    		protected Bitmap BackgroundBitmap = null;
    		protected Bitmap CloseBitmap = null;
    		protected Point CloseBitmapLocation;
    		protected Size CloseBitmapSize;
    		protected Rectangle RealTitleRectangle;
    		protected Rectangle RealContentRectangle;
    		protected Rectangle WorkAreaRectangle;
    		protected Timer timer = new Timer();
    		protected TaskbarStates taskbarState = TaskbarStates.hidden;
    		protected string titleText;
    		protected string contentText;
    		protected Color normalTitleColor = Color.FromArgb(255,0,0);
    		protected Color hoverTitleColor = Color.FromArgb(255,0,0);
    		protected Color normalContentColor = Color.FromArgb(0,0,0);
    		protected Color hoverContentColor = Color.FromArgb(0,0,0x66);
    		protected Font normalTitleFont = new Font("Arial",12,FontStyle.Regular,GraphicsUnit.Pixel);
    		protected Font hoverTitleFont = new Font("Arial",12,FontStyle.Bold,GraphicsUnit.Pixel);
    		protected Font normalContentFont = new Font("Arial",11,FontStyle.Regular,GraphicsUnit.Pixel);
    		protected Font hoverContentFont = new Font("Arial",11,FontStyle.Regular,GraphicsUnit.Pixel);
    		protected int nShowEvents;
    		protected int nHideEvents;
    		protected int nVisibleEvents;
    		protected int nIncrementShow;
    		protected int nIncrementHide;
    		protected bool bIsMouseOverPopup = false;
    		protected bool bIsMouseOverClose = false;
    		protected bool bIsMouseOverContent = false;
    		protected bool bIsMouseOverTitle = false;
    		protected bool bIsMouseDown = false;
    		protected bool bKeepVisibleOnMouseOver = true;			// Added Rev 002
    		protected bool bReShowOnMouseOver = false;				// Added Rev 002
    		#endregion
    						
    		#region TaskbarNotifier Public Members
    		public Rectangle TitleRectangle;
    		public Rectangle ContentRectangle;
    		public bool TitleClickable = false;
    		public bool ContentClickable = true;
    		public bool CloseClickable = true;
    		public bool EnableSelectionRectangle = true;
    		public event EventHandler CloseClick = null;
    		public event EventHandler TitleClick = null;
    		public event EventHandler ContentClick = null;
    		#endregion
    	
    		#region TaskbarNotifier Enums
    		/// <summary>
    		/// List of the different popup animation status
    		/// </summary>
    		public enum TaskbarStates
    		{
    			hidden = 0,
    			appearing = 1,
    			visible = 2,
    			disappearing = 3
    		}
    		#endregion
    
    		#region TaskbarNotifier Constructor
    		/// <summary>
    		/// The Constructor for TaskbarNotifier
    		/// </summary>
    		public TaskbarNotifier()
    		{
    			// Window Style
    			FormBorderStyle = FormBorderStyle.None;
    			WindowState = FormWindowState.Minimized;
    			base.Show();
    			base.Hide();
    			WindowState = FormWindowState.Normal;
    			ShowInTaskbar = false;
    			TopMost = true;
    			MaximizeBox = false;
    			MinimizeBox = false;
    			ControlBox = false;
    			
    			timer.Enabled = true;
    			timer.Tick += new EventHandler(OnTimer);
    		}
    		#endregion
    
    		#region TaskbarNotifier Properties
    		/// <summary>
    		/// Get the current TaskbarState (hidden, showing, visible, hiding)
    		/// </summary>
    		public TaskbarStates TaskbarState
    		{
    			get
    			{
    				return taskbarState;
    			}
    		}
    
    		/// <summary>
    		/// Get/Set the popup Title Text
    		/// </summary>
    		public string TitleText
    		{
    			get
    			{
    				return titleText;
    			}
    			set
    			{
    				titleText=value;
    				Refresh();
    			}
    		}
    
    		/// <summary>
    		/// Get/Set the popup Content Text
    		/// </summary>
    		public string ContentText
    		{
    			get
    			{
    				return contentText;
    			}
    			set
    			{
    				contentText=value;
    				Refresh();
    			}
    		}
    
    		/// <summary>
    		/// Get/Set the Normal Title Color
    		/// </summary>
    		public Color NormalTitleColor
    		{
    			get
    			{
    				return normalTitleColor;
    			}
    			set
    			{
    				normalTitleColor = value;
    				Refresh();
    			}
    		}
    
    		/// <summary>
    		/// Get/Set the Hover Title Color
    		/// </summary>
    		public Color HoverTitleColor
    		{
    			get
    			{
    				return hoverTitleColor;
    			}
    			set
    			{
    				hoverTitleColor = value;
    				Refresh();
    			}
    		}
    
    		/// <summary>
    		/// Get/Set the Normal Content Color
    		/// </summary>
    		public Color NormalContentColor
    		{
    			get
    			{
    				return normalContentColor;
    			}
    			set
    			{
    				normalContentColor = value;
    				Refresh();
    			}
    		}
    
    		/// <summary>
    		/// Get/Set the Hover Content Color
    		/// </summary>
    		public Color HoverContentColor
    		{
    			get
    			{
    				return hoverContentColor;
    			}
    			set
    			{
    				hoverContentColor = value;
    				Refresh();
    			}
    		}
    
    		/// <summary>
    		/// Get/Set the Normal Title Font
    		/// </summary>
    		public Font NormalTitleFont
    		{
    			get
    			{
    				return normalTitleFont;
    			}
    			set
    			{
    				normalTitleFont = value;
    				Refresh();
    			}
    		}
    
    		/// <summary>
    		/// Get/Set the Hover Title Font
    		/// </summary>
    		public Font HoverTitleFont
    		{
    			get
    			{
    				return hoverTitleFont;
    			}
    			set
    			{
    				hoverTitleFont = value;
    				Refresh();
    			}
    		}
    
    		/// <summary>
    		/// Get/Set the Normal Content Font
    		/// </summary>
    		public Font NormalContentFont
    		{
    			get
    			{
    				return normalContentFont;
    			}
    			set
    			{
    				normalContentFont = value;
    				Refresh();
    			}
    		}
    
    		/// <summary>
    		/// Get/Set the Hover Content Font
    		/// </summary>
    		public Font HoverContentFont
    		{
    			get
    			{
    				return hoverContentFont;
    			}
    			set
    			{
    				hoverContentFont = value;
    				Refresh();
    			}
    		}
    
    		/// <summary>
    		/// Indicates if the popup should remain visible when the mouse pointer is over it.
    		/// Added Rev 002
    		/// </summary>
    		public bool KeepVisibleOnMousOver
    		{
    			get
    			{
    				return bKeepVisibleOnMouseOver;
    			}
    			set
    			{
    				bKeepVisibleOnMouseOver=value;
    			}
    		}
    
    		/// <summary>
    		/// Indicates if the popup should appear again when mouse moves over it while it's disappearing.
    		/// Added Rev 002
    		/// </summary>
    		public bool ReShowOnMouseOver
    		{
    			get
    			{
    				return bReShowOnMouseOver;
    			}
    			set
    			{
    				bReShowOnMouseOver=value;
    			}
    		}
    
    		#endregion
    
    		#region TaskbarNotifier Public Methods
    		[DllImport("user32.dll")]
    		private static extern Boolean ShowWindow(IntPtr hWnd,Int32 nCmdShow);
    		/// <summary>
    		/// Displays the popup for a certain amount of time
    		/// </summary>
    		/// <param name="strTitle">The string which will be shown as the title of the popup</param>
    		/// <param name="strContent">The string which will be shown as the content of the popup</param>
    		/// <param name="nTimeToShow">Duration of the showing animation (in milliseconds)</param>
    		/// <param name="nTimeToStay">Duration of the visible state before collapsing (in milliseconds)</param>
    		/// <param name="nTimeToHide">Duration of the hiding animation (in milliseconds)</param>
    		/// <returns>Nothing</returns>
    		public void Show(string strTitle, string strContent, int nTimeToShow, int nTimeToStay, int nTimeToHide)
    		{
    			WorkAreaRectangle = Screen.GetWorkingArea(WorkAreaRectangle);
    			titleText = strTitle;
    			contentText = strContent;
    			nVisibleEvents = nTimeToStay;
    			CalculateMouseRectangles();
    
    			// We calculate the pixel increment and the timer value for the showing animation
    			int nEvents;
    			if (nTimeToShow > 10)
    			{
    				nEvents = Math.Min((nTimeToShow / 10), BackgroundBitmap.Height);
    				nShowEvents = nTimeToShow / nEvents;
    				nIncrementShow = BackgroundBitmap.Height / nEvents;
    			}
    			else
    			{
    				nShowEvents = 10;
    				nIncrementShow = BackgroundBitmap.Height;
    			}
    
    			// We calculate the pixel increment and the timer value for the hiding animation
    			if( nTimeToHide > 10)
    			{
    				nEvents = Math.Min((nTimeToHide / 10), BackgroundBitmap.Height);
    				nHideEvents = nTimeToHide / nEvents;
    				nIncrementHide = BackgroundBitmap.Height / nEvents;
    			}
    			else
    			{
    				nHideEvents = 10;
    				nIncrementHide = BackgroundBitmap.Height;
    			}
    
    			switch (taskbarState)
    			{
    				case TaskbarStates.hidden:
    					taskbarState = TaskbarStates.appearing;
    					SetBounds(WorkAreaRectangle.Right-BackgroundBitmap.Width-17, WorkAreaRectangle.Bottom-1, BackgroundBitmap.Width, 0);
    					timer.Interval = nShowEvents;
    					timer.Start();
    					// We Show the popup without stealing focus
    					ShowWindow(this.Handle, 4);
    					break;
    
    				case TaskbarStates.appearing:
    					Refresh();
    					break;
    
    				case TaskbarStates.visible:
    					timer.Stop();
    					timer.Interval = nVisibleEvents;
    					timer.Start();
    					Refresh();
    					break;
    
    				case TaskbarStates.disappearing:
    					timer.Stop();
    					taskbarState = TaskbarStates.visible;
    					SetBounds(WorkAreaRectangle.Right-BackgroundBitmap.Width-17, WorkAreaRectangle.Bottom-BackgroundBitmap.Height-1, BackgroundBitmap.Width, BackgroundBitmap.Height);
    					timer.Interval = nVisibleEvents;
    					timer.Start();
    					Refresh();
    					break;
    			}
    		}
    
    		/// <summary>
    		/// Hides the popup
    		/// </summary>
    		/// <returns>Nothing</returns>
    		public new void Hide()
    		{
    			if (taskbarState != TaskbarStates.hidden)
    			{
    				timer.Stop();
    				taskbarState = TaskbarStates.hidden;
    				base.Hide();
    			}
    		}
    
    		/// <summary>
    		/// Sets the background bitmap and its transparency color
    		/// </summary>
    		/// <param name="strFilename">Path of the Background Bitmap on the disk</param>
    		/// <param name="transparencyColor">Color of the Bitmap which won't be visible</param>
    		/// <returns>Nothing</returns>
    		public void SetBackgroundBitmap(string strFilename, Color transparencyColor)
    		{
    			BackgroundBitmap = new Bitmap(strFilename);
    			Width = BackgroundBitmap.Width;
    			Height = BackgroundBitmap.Height;
    			Region = BitmapToRegion(BackgroundBitmap, transparencyColor);
    		}
    
    		/// <summary>
    		/// Sets the background bitmap and its transparency color
    		/// </summary>
    		/// <param name="image">Image/Bitmap object which represents the Background Bitmap</param>
    		/// <param name="transparencyColor">Color of the Bitmap which won't be visible</param>
    		/// <returns>Nothing</returns>
    		public void SetBackgroundBitmap(Image image, Color transparencyColor)
    		{
    			BackgroundBitmap = new Bitmap(image);
    			Width = BackgroundBitmap.Width;
    			Height = BackgroundBitmap.Height;
    			Region = BitmapToRegion(BackgroundBitmap, transparencyColor);
    		}
    
    		/// <summary>
    		/// Sets the 3-State Close Button bitmap, its transparency color and its coordinates
    		/// </summary>
    		/// <param name="strFilename">Path of the 3-state Close button Bitmap on the disk (width must a multiple of 3)</param>
    		/// <param name="transparencyColor">Color of the Bitmap which won't be visible</param>
    		/// <param name="position">Location of the close button on the popup</param>
    		/// <returns>Nothing</returns>
    		public void SetCloseBitmap(string strFilename, Color transparencyColor, Point position)
    		{
    			CloseBitmap = new Bitmap(strFilename);
    			CloseBitmap.MakeTransparent(transparencyColor);
    			CloseBitmapSize = new Size(CloseBitmap.Width/3, CloseBitmap.Height);
    			CloseBitmapLocation = position;
    		}
    
    		/// <summary>
    		/// Sets the 3-State Close Button bitmap, its transparency color and its coordinates
    		/// </summary>
    		/// <param name="image">Image/Bitmap object which represents the 3-state Close button Bitmap (width must be a multiple of 3)</param>
    		/// <param name="transparencyColor">Color of the Bitmap which won't be visible</param>
    		/// /// <param name="position">Location of the close button on the popup</param>
    		/// <returns>Nothing</returns>
    		public void SetCloseBitmap(Image image, Color transparencyColor, Point position)
    		{
    			CloseBitmap = new Bitmap(image);
    			CloseBitmap.MakeTransparent(transparencyColor);
    			CloseBitmapSize = new Size(CloseBitmap.Width/3, CloseBitmap.Height);
    			CloseBitmapLocation = position;
    		}
    		#endregion
    
    		#region TaskbarNotifier Protected Methods
    		protected void DrawCloseButton(Graphics grfx)
    		{
    			if (CloseBitmap != null)
    			{	
    				Rectangle rectDest = new Rectangle(CloseBitmapLocation, CloseBitmapSize);
    				Rectangle rectSrc;
    
    				if (bIsMouseOverClose)
    				{
    					if (bIsMouseDown)
    						rectSrc = new Rectangle(new Point(CloseBitmapSize.Width*2, 0), CloseBitmapSize);
    					else
    						rectSrc = new Rectangle(new Point(CloseBitmapSize.Width, 0), CloseBitmapSize);
    				}		
    				else
    					rectSrc = new Rectangle(new Point(0, 0), CloseBitmapSize);
    					
    
    				grfx.DrawImage(CloseBitmap, rectDest, rectSrc, GraphicsUnit.Pixel);
    			}
    		}
    
    		protected void DrawText(Graphics grfx)
    		{
    			if (titleText != null && titleText.Length != 0)
    			{
    				StringFormat sf = new StringFormat();
    				sf.Alignment = StringAlignment.Near;
    				sf.LineAlignment = StringAlignment.Center;
    				sf.FormatFlags = StringFormatFlags.NoWrap;
    				sf.Trimming = StringTrimming.EllipsisCharacter;				// Added Rev 002
    				if (bIsMouseOverTitle)
    					grfx.DrawString(titleText, hoverTitleFont, new SolidBrush(hoverTitleColor), TitleRectangle, sf);
    				else
    					grfx.DrawString(titleText, normalTitleFont, new SolidBrush(normalTitleColor), TitleRectangle, sf);
    			}
    
    			if (contentText != null && contentText.Length != 0)
    			{
    				StringFormat sf = new StringFormat();
    				sf.Alignment = StringAlignment.Center;
    				sf.LineAlignment = StringAlignment.Center;
    				sf.FormatFlags = StringFormatFlags.MeasureTrailingSpaces;
    				sf.Trimming = StringTrimming.Word;							// Added Rev 002
    				
    				if (bIsMouseOverContent)
    				{
    					grfx.DrawString(contentText, hoverContentFont, new SolidBrush(hoverContentColor), ContentRectangle, sf);
    					if (EnableSelectionRectangle)
    						ControlPaint.DrawBorder3D(grfx, RealContentRectangle, Border3DStyle.Etched, Border3DSide.Top | Border3DSide.Bottom | Border3DSide.Left | Border3DSide.Right);
    					
    				}
    				else
    					grfx.DrawString(contentText, normalContentFont, new SolidBrush(normalContentColor), ContentRectangle, sf);
    			}
    		}
    
    		protected void CalculateMouseRectangles()
    		{
    			Graphics grfx = CreateGraphics();
    			StringFormat sf = new StringFormat();
    			sf.Alignment = StringAlignment.Center;
    			sf.LineAlignment = StringAlignment.Center;
    			sf.FormatFlags = StringFormatFlags.MeasureTrailingSpaces;
    			SizeF sizefTitle = grfx.MeasureString(titleText, hoverTitleFont, TitleRectangle.Width, sf);
    			SizeF sizefContent = grfx.MeasureString(contentText, hoverContentFont, ContentRectangle.Width, sf);
    			grfx.Dispose();
    
    			// Added Rev 002
    	        //We should check if the title size really fits inside the pre-defined title rectangle
    			if (sizefTitle.Height > TitleRectangle.Height)
    			{
    				RealTitleRectangle = new Rectangle(TitleRectangle.Left, TitleRectangle.Top, TitleRectangle.Width , TitleRectangle.Height );
    			} 
    			else
    			{
    				RealTitleRectangle = new Rectangle(TitleRectangle.Left, TitleRectangle.Top, (int)sizefTitle.Width, (int)sizefTitle.Height);
    			}
    			RealTitleRectangle.Inflate(0,2);
    
    			// Added Rev 002
    			//We should check if the Content size really fits inside the pre-defined Content rectangle
    			if (sizefContent.Height > ContentRectangle.Height)
    			{
    				RealContentRectangle = new Rectangle((ContentRectangle.Width-(int)sizefContent.Width)/2+ContentRectangle.Left, ContentRectangle.Top, (int)sizefContent.Width, ContentRectangle.Height );
    			}
    			else
    			{
    				RealContentRectangle = new Rectangle((ContentRectangle.Width-(int)sizefContent.Width)/2+ContentRectangle.Left, (ContentRectangle.Height-(int)sizefContent.Height)/2+ContentRectangle.Top, (int)sizefContent.Width, (int)sizefContent.Height);
    			}
    			RealContentRectangle.Inflate(0,2);
    		}
    
    		protected Region BitmapToRegion(Bitmap bitmap, Color transparencyColor)
    		{
    			if (bitmap == null)
    				throw new ArgumentNullException("Bitmap", "Bitmap cannot be null!");
    
    			int height = bitmap.Height;
    			int width = bitmap.Width;
    
    			GraphicsPath path = new GraphicsPath();
    
    			for (int j=0; j<height; j++ )
    				for (int i=0; i<width; i++)
    				{
    					if (bitmap.GetPixel(i, j) == transparencyColor)
    						continue;
    
    					int x0 = i;
    
    					while ((i < width) && (bitmap.GetPixel(i, j) != transparencyColor))
    						i++;
    
    					path.AddRectangle(new Rectangle(x0, j, i-x0, 1));
    				}
    
    			Region region = new Region(path);
    			path.Dispose();
    			return region;
    		}
    		#endregion
    
    		#region TaskbarNotifier Events Overrides
    		protected void OnTimer(Object obj, EventArgs ea)
    		{
    			switch (taskbarState)
    			{
    				case TaskbarStates.appearing:
    					if (Height < BackgroundBitmap.Height)
    						SetBounds(Left, Top-nIncrementShow ,Width, Height + nIncrementShow);
    					else
    					{
    						timer.Stop();
    						Height = BackgroundBitmap.Height;
    						timer.Interval = nVisibleEvents;
    						taskbarState = TaskbarStates.visible;
    						timer.Start();
    					}
    					break;
    
    				case TaskbarStates.visible:
    					timer.Stop();
    					timer.Interval = nHideEvents;
    					// Added Rev 002
    					if ((bKeepVisibleOnMouseOver && !bIsMouseOverPopup ) || (!bKeepVisibleOnMouseOver))
    					{
    						taskbarState = TaskbarStates.disappearing;
    					} 
    					//taskbarState = TaskbarStates.disappearing;		// Rev 002
    					timer.Start();
    					break;
    
    				case TaskbarStates.disappearing:
    					// Added Rev 002
    					if (bReShowOnMouseOver && bIsMouseOverPopup) 
    					{
    						taskbarState = TaskbarStates.appearing;
    					} 
    					else 
    					{
    						if (Top < WorkAreaRectangle.Bottom)
    							SetBounds(Left, Top + nIncrementHide, Width, Height - nIncrementHide);
    						else
    							Hide();
    					}
    					break;
    			}
    			
    		}
    
    		protected override void OnMouseEnter(EventArgs ea)
    		{
    			base.OnMouseEnter(ea);
    			bIsMouseOverPopup = true;
    			Refresh();
    		}
    
    		protected override void OnMouseLeave(EventArgs ea)
    		{
    			base.OnMouseLeave(ea);
    			bIsMouseOverPopup = false;
    			bIsMouseOverClose = false;
    			bIsMouseOverTitle = false;
    			bIsMouseOverContent = false;
    			Refresh();
    		}
    
    		protected override void OnMouseMove(MouseEventArgs mea)
    		{
    			base.OnMouseMove(mea);
    
    			bool bContentModified = false;
    			
    			if ( (mea.X > CloseBitmapLocation.X) && (mea.X < CloseBitmapLocation.X + CloseBitmapSize.Width) && (mea.Y > CloseBitmapLocation.Y) && (mea.Y < CloseBitmapLocation.Y + CloseBitmapSize.Height) && CloseClickable )
    			{
    				if (!bIsMouseOverClose)
    				{
    					bIsMouseOverClose = true;
    					bIsMouseOverTitle = false;
    					bIsMouseOverContent = false;
    					Cursor = Cursors.Hand;
    					bContentModified = true;
    				}
    			}
    			else if (RealContentRectangle.Contains(new Point(mea.X, mea.Y)) && ContentClickable)
    			{
    				if (!bIsMouseOverContent)
    				{
    					bIsMouseOverClose = false;
    					bIsMouseOverTitle = false;
    					bIsMouseOverContent = true;
    					Cursor = Cursors.Hand;
    					bContentModified = true;
    				}
    			}
    			else if (RealTitleRectangle.Contains(new Point(mea.X, mea.Y)) && TitleClickable)
    			{
    				if (!bIsMouseOverTitle)
    				{
    					bIsMouseOverClose = false;
    					bIsMouseOverTitle = true;
    					bIsMouseOverContent = false;
    					Cursor = Cursors.Hand;
    					bContentModified = true;
    				}
    			}
    			else
    			{
    				if (bIsMouseOverClose || bIsMouseOverTitle || bIsMouseOverContent)
    					bContentModified = true;
    
    				bIsMouseOverClose = false;
    				bIsMouseOverTitle = false;
    				bIsMouseOverContent = false;
    				Cursor = Cursors.Default;
    			}
    
    			if (bContentModified)
    				Refresh();
    		}
    
    		protected override void OnMouseDown(MouseEventArgs mea)
    		{
    			base.OnMouseDown(mea);
    			bIsMouseDown = true;
    			
    			if (bIsMouseOverClose)
    				Refresh();
    		}
    
    		protected override void OnMouseUp(MouseEventArgs mea)
    		{
    			base.OnMouseUp(mea);
    			bIsMouseDown = false;
    
    			if (bIsMouseOverClose)
    			{
    				Hide();
    							
    				if (CloseClick != null)
    					CloseClick(this, new EventArgs());
    			}
    			else if (bIsMouseOverTitle)
    			{
    				if (TitleClick != null)
    					TitleClick(this, new EventArgs());
    			}
    			else if (bIsMouseOverContent)
    			{
    				if (ContentClick != null)
    					ContentClick(this, new EventArgs());
    			}
    		}
    
    		protected override void OnPaintBackground(PaintEventArgs pea)
    		{
    			Graphics grfx = pea.Graphics;
    			grfx.PageUnit = GraphicsUnit.Pixel;
    			
    			Graphics offScreenGraphics;
    			Bitmap offscreenBitmap;
    			
    			offscreenBitmap = new Bitmap(BackgroundBitmap.Width, BackgroundBitmap.Height);
    			offScreenGraphics = Graphics.FromImage(offscreenBitmap);
    			
    			if (BackgroundBitmap != null)
    			{
    				offScreenGraphics.DrawImage(BackgroundBitmap, 0, 0, BackgroundBitmap.Width, BackgroundBitmap.Height);
    			}
    			
    			DrawCloseButton(offScreenGraphics);
    			DrawText(offScreenGraphics);
    
    			grfx.DrawImage(offscreenBitmap, 0, 0);
    		}
    		#endregion
    
            private void InitializeComponent()
            {
                this.SuspendLayout();
                // 
                // TaskbarNotifier
                // 
                this.BackColor = System.Drawing.Color.White;
                this.ClientSize = new System.Drawing.Size(284, 262);
                this.Name = "TaskbarNotifier";
                this.ResumeLayout(false);
    
            }
    	}
    }

      第三步,调用:

           1,在您需要显示的窗体里面添加一个定时器,

          代码如下:

         

      private System.Windows.Forms.Timer timer1;
       this.timer1 = new System.Windows.Forms.Timer(this.components);
        this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
    

      在构造函数里面添加代码:
         

      #region  
                if (!this.DesignMode)
                {
                    timer1.Enabled = true;
                    timer1.Interval = 10000;
                }
                #endregion

        2,初始化TaskbarNotifier 

    代码如下:

      #region 初始化弹窗 
                taskbarNotifier.SetBackgroundBitmap(MytaskbarNotifier.Properties.Resources.skin3, Color.FromArgb(255, 0, 255));
                taskbarNotifier.SetCloseBitmap(MytaskbarNotifier.Properties.Resources.close, Color.FromArgb(255, 0, 255), new Point(280, 57));
    
                taskbarNotifier.TitleRectangle = new Rectangle(150, 57, 125, 28);
                taskbarNotifier.ContentRectangle = new Rectangle(75, 92, 215, 55);
    
                taskbarNotifier.CloseClickable = true;
                taskbarNotifier.TitleClickable = false;
                taskbarNotifier.ContentClickable = true;
                taskbarNotifier.EnableSelectionRectangle = true;
                taskbarNotifier.KeepVisibleOnMousOver = true;
                taskbarNotifier.ReShowOnMouseOver = false;
    
                taskbarNotifier.ContentClick += new EventHandler(taskbarNotifier_ContentClick);
                #endregion

                       

    void taskbarNotifier_ContentClick(object sender, EventArgs e)
    {
    return;//暂时不用此功能
    

      3

    显示代码实习:
     /// <summary>
            /// 逐条显示消息
            /// </summary>
            /// <param name="dsMessage"></param>
            private void DisplayMessage(DataRow dsMessage)
            {
                try
                {
                    Int32 msgID = Convert.ToInt32(dsMessage[0].ToString());
                    string msgTitle = dsMessage[1].ToString();
                    string msgContent = dsMessage[2].ToString();
                    string msgPublisherCode = dsMessage[3].ToString();
                    string msgPublisherName = dsMessage[4].ToString();
                    DateTime msgPublishTime = Convert.ToDateTime(dsMessage[5].ToString());
                    string msgLimitedArea = dsMessage[6].ToString();
                    string msgIsValid = dsMessage[7].ToString();
                    string msgExtend1 = dsMessage[8].ToString();
                    string msgExtend2 = dsMessage[9].ToString();
                    string msgExtend3 = dsMessage[10].ToString();
                    string msgRoleArea = dsMessage[11].ToString();
    
                    string displayTitle = msgTitle;
                    string displayContent = msgContent + "
    
    " + msgPublisherName + "[" + msgPublisherCode + "]" + msgPublishTime;
    
                    taskbarNotifier.Show(displayTitle, displayContent, 500, delayTime, 500);
                    System.Threading.Thread.Sleep(500);
    
                    this.timer1.Start();
                }
                catch
                {
    
                    this.timer1.Stop();
                }
            }
        
    

      实现定时器事件代码:

      decimal validTime = 1m;   //系统消息有效时间
            int refreshTime = 5000;  //刷新数据库时间
            int delayTime = 5000;    //提示窗口停留时间
            int intervalTime = 10000; //间隔显示时间
            DataSet dsMessage = new DataSet();  
            /// <summary>
            /// Timer
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void timer1_Tick(object sender, EventArgs e)
            {
                try
                {
                    this.timer1.Stop();
    
                    if (dsMessage == null || dsMessage.Tables.Count == 0 || dsMessage.Tables[0].Rows.Count == 0)
                    {                  
                        // 获取系统消息按照权限获取要显示的消息table
                     
                         //todo sql实现
    SELECT COM_SYS_MSG.MSG_ID,
           COM_SYS_MSG.MSG_TITLE,
           COM_SYS_MSG.MSG_CONTENT,
           COM_SYS_MSG.PUBLISHER,
           (SELECT COM_EMPLOYEE.EMPL_NAME
            FROM COM_EMPLOYEE
            WHERE COM_EMPLOYEE.EMPL_CODE = COM_SYS_MSG.PUBLISHER) empl_name,
           COM_SYS_MSG.PUBLISH_TIME,
           COM_SYS_MSG.LIMITED_AREA,
           COM_SYS_MSG.IS_VALID,
           COM_SYS_MSG.EXTEND1,
           COM_SYS_MSG.EXTEND2,
           COM_SYS_MSG.EXTEND3,
           COM_SYS_MSG.ROLE_AREA
    FROM COM_SYS_MSG
    WHERE COM_SYS_MSG.LIMITED_AREA = 'ALL'
    AND (COM_SYS_MSG.ROLE_AREA = '{2}' or '{1}'='ALL')
    AND COM_SYS_MSG.IS_VALID = '1'
    and (COM_SYS_MSG.Extend2='{1}' or '{1}'='ALL')
    AND COM_SYS_MSG.PUBLISH_TIME>=
    (
    SELECT COM_IP_MSG.LAST_VIEW_TIME
    FROM COM_IP_MSG
    WHERE COM_IP_MSG.IP_ADDRESS = '{0}')
    ORDER BY COM_SYS_MSG.PUBLISH_TIME

                                     

      



    } if (dsMessage != null && dsMessage.Tables[0].Rows.Count > 0) { if (dsMessage.Tables[0].Rows.Count == 1) { //只剩最后一条时 DateTime msgPublishTime = msgPublishTime = Convert.ToDateTime(dsMessage.Tables[0].Rows[0][5].ToString()); this.UpdateViewTime(msgPublishTime);//更新时间
    //
    //UPDATE com_ip_msg
    //SET com_ip_msg.LAST_VIEW_TIME = to_date('{1}','yyyy-mm-dd hh24:mi:ss')
    //WHERE com_ip_msg.IP_ADDRESS = '{0}'
    

      

              this.timer1.Interval = refreshTime;
           }
          else
           {
        this.timer1.Interval = intervalTime;
            }
    this.DisplayMessage(dsMessage.Tables[0].Rows[0]);
    dsMessage.Tables[0].Rows.RemoveAt(0); this.timer1.Start();
    }
    else
    {
    this.timer1.Start();
    }
    } catch {
    this.timer1.Stop();
    }
    }

      整理完毕。

  • 相关阅读:
    写在毕业季前
    使用Github Page鼓励自己每日编程
    win8/Metro开发系列一 Xaml布局
    AlertDialog详解
    安卓项目文件目录
    Andriod布局之LinearLayout
    Andriod定时任务
    android 设置布局横屏竖屏
    Android默认启动程序问题
    Android全屏显示
  • 原文地址:https://www.cnblogs.com/cegh/p/4123373.html
Copyright © 2020-2023  润新知