⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 notify.cs

📁 蓝牙通讯
💻 CS
📖 第 1 页 / 共 2 页
字号:
				if(textptr!=IntPtr.Zero)
				{
					return Marshal.PtrToStringUni(textptr);
				}
				else
				{
					return null;
				}
			}
			set
			{
				//free up previous value
				IntPtr textptr = (IntPtr)BitConverter.ToInt32(data, 8);

				if(textptr!=IntPtr.Zero)
				{
					MarshalEx.FreeHGlobal(textptr);
				}
				//marshal string to unmanaged memory
				textptr = MarshalEx.StringToHGlobalUni(value);
				//store ptr in bytearray
				BitConverter.GetBytes((int)textptr).CopyTo(data, 8);			
			}
		}
		#endregion

		#region Sound
		/// <summary>
		/// Sound string as supplied to PlaySound.
		/// </summary>
		/// <remarks>SetUserNotification() ignores it if the <see cref="P:OpenNETCF.Win32.Notify.UserNotification.Action">Action property</see> does not contain <see cref="T:OpenNETCF.Win32.Notify.NotifyAction">NotifyAction.Sound</see>.</remarks>
		public string Sound
		{
			get
			{
				IntPtr soundptr = (IntPtr)BitConverter.ToInt32(data, 12);

				if(soundptr!=IntPtr.Zero)
				{
					return Marshal.PtrToStringUni(soundptr);
				}
				else
				{
					return null;
				}
			}
			set
			{
				//free up previous value
				IntPtr soundptr = (IntPtr)BitConverter.ToInt32(data, 12);

				if(soundptr!=IntPtr.Zero)
				{
					MarshalEx.FreeHGlobal(soundptr);
				}
				//marshal string to unmanaged memory
				soundptr = MarshalEx.StringToHGlobalUni(value);
				//store ptr in bytearray
				BitConverter.GetBytes((int)soundptr).CopyTo(data, 12);
			}
		}
		#endregion
	}
	#endregion

	#region User Notification Info Header
	/// <summary>
	/// Contains information about notification events.
	/// </summary>
	public class UserNotificationInfoHeader
	{
		private byte[] data;

		private UserNotificationTrigger trigger;
		private UserNotification notify;

		#region Constructor
		/// <summary>
		/// Create a new instance of UserNotificationInfoHeader
		/// </summary>
		public UserNotificationInfoHeader()
		{
			//16 byte structure
			data = new byte[16];
		}
		#endregion

		#region From Pointer
		/// <summary>
		/// 
		/// </summary>
		/// <param name="pointer"></param>
		/// <returns></returns>
		internal static UserNotificationInfoHeader FromPtr(IntPtr pointer)
		{
			UserNotificationInfoHeader nih = new UserNotificationInfoHeader();

			Marshal.Copy(pointer, nih.data, 0, 16);

			//marshall trigger
			nih.trigger = UserNotificationTrigger.FromPtr((IntPtr)BitConverter.ToInt32(nih.data, 8));

			//marshall notification
			IntPtr notifyptr = (IntPtr)BitConverter.ToInt32(nih.data, 12);
			//if null pointer no notification
			if(notifyptr!=IntPtr.Zero)
			{
				//else convert to managed notificatio object
				nih.notify = UserNotification.FromPtr(notifyptr);
			}

			return nih;
		}
		#endregion

		#region Handle
		/// <summary>
		/// Handle to the notification.
		/// </summary>
		public int Handle
		{
			get
			{
				return BitConverter.ToInt32(data, 0);
			}
		}
		#endregion

		#region Status
		/// <summary>
		/// Indicates current state of the notification.
		/// </summary>
		public NotificationStatus Status
		{
			get
			{
				return (NotificationStatus)BitConverter.ToInt32(data, 4);
			}
		}
		#endregion

		#region User Notification Trigger
		/// <summary>
		/// The UserNotificationTrigger object
		/// </summary>
		public UserNotificationTrigger UserNotificationTrigger
		{
			get
			{
				return trigger;
			}
		}
		#endregion

		#region User Notification
		/// <summary>
		/// The UserNotification object.
		/// </summary>
		public UserNotification UserNotification
		{
			get
			{
				return notify;
			}
		}
		#endregion
	}
	#endregion

	#region User Notification Trigger
	/// <summary>
	/// Defines what event activates a notification.
	/// </summary>
	public class UserNotificationTrigger
	{
		internal byte[] data;

		private char[] sApplication;
		GCHandle sApplicationHandle;

		private char[] sArgs;
		GCHandle sArgsHandle;

		#region Constructor
		/// <summary>
		/// Create a new instance of UserNotificationTrigger
		/// </summary>
		public UserNotificationTrigger()
		{
			data = new byte[52];

			sApplication = new char[OpenNETCF.IO.FileEx.MaxPath];
			sApplicationHandle = GCHandle.Alloc(sApplication, GCHandleType.Pinned);
			BitConverter.GetBytes((int)sApplicationHandle.AddrOfPinnedObject() + 4).CopyTo(data, 12);

			sArgs = new char[OpenNETCF.IO.FileEx.MaxPath];
			sArgsHandle = GCHandle.Alloc(sArgs, GCHandleType.Pinned);
			BitConverter.GetBytes((int)sArgsHandle.AddrOfPinnedObject() + 4).CopyTo(data, 16);

		}
		#endregion

		#region From Pointer
		/// <summary>
		/// Returns a NotificationTrigger object from a native pointer.
		/// </summary>
		/// <param name="pointer">Native memory pointer.</param>
		/// <returns>New managed NotificationTrigger object.</returns>
		public static UserNotificationTrigger FromPtr(IntPtr pointer)
		{
			UserNotificationTrigger trigger = new UserNotificationTrigger();

			//copy the bytes
			Marshal.Copy(pointer, trigger.data, 0, 52);

			//clear the existing string pointers
			BitConverter.GetBytes((int)0).CopyTo(trigger.data, 12);
			BitConverter.GetBytes((int)0).CopyTo(trigger.data, 16);

			//marshal the strings
			trigger.Application = Marshal.PtrToStringUni((IntPtr)Marshal.ReadInt32(pointer, 12));
			trigger.Arguments = Marshal.PtrToStringUni((IntPtr)Marshal.ReadInt32(pointer, 16));

			return trigger;
		}
		#endregion

		#region Type
		/// <summary>
		/// Specifies the type of notification.
		/// </summary>
		public NotificationType Type
		{
			get
			{
				return (NotificationType)BitConverter.ToInt32(data, 4);
			}
			set
			{
				BitConverter.GetBytes((int)value).CopyTo(data, 4);
			}
		}
		#endregion

		#region Event
		/// <summary>
		/// Specifies the type of event should Type = Event.
		/// </summary>
		public NotificationEvent Event
		{
			get
			{
				return (NotificationEvent)BitConverter.ToInt32(data, 8);
			}
			set
			{
				BitConverter.GetBytes((int)value).CopyTo(data, 8);
			}
		}
		#endregion

		#region Application
		/// <summary>
		/// Name of the application to execute.
		/// </summary>
		public string Application
		{
			get
			{
				IntPtr appptr = (IntPtr)BitConverter.ToInt32(data, 12);
				if(appptr!=IntPtr.Zero)
				{
					return Marshal.PtrToStringUni(appptr);
				}
				else
				{
					return null;
				}
			}
			set
			{
				IntPtr appptr = (IntPtr)BitConverter.ToInt32(data, 12);
				//if native string already allocated
				if(appptr!=IntPtr.Zero)
				{
					//free previous value
					MarshalEx.FreeHGlobal(appptr);
				}
				appptr = MarshalEx.StringToHGlobalUni(value);
				//put value into array
				BitConverter.GetBytes((int)appptr).CopyTo(data, 12);
			}
		}
		#endregion

		#region Arguments
		/// <summary>
		/// Command line (without the application name). 
		/// </summary>
		public string Arguments
		{
			get
			{
				IntPtr argptr = (IntPtr)BitConverter.ToInt32(data, 16);
				if(argptr!=IntPtr.Zero)
				{
					return Marshal.PtrToStringUni(argptr);
				}
				else
				{
					return null;
				}
			}
			set
			{
				IntPtr argptr = (IntPtr)BitConverter.ToInt32(data, 16);
				//if native string already allocated
				if(argptr!=IntPtr.Zero)
				{
					//free previous value
					MarshalEx.FreeHGlobal(argptr);
				}
				argptr = MarshalEx.StringToHGlobalUni(value);
				//put value into array
				BitConverter.GetBytes((int)argptr).CopyTo(data, 16);
			}
		}
		#endregion

		#region Start Time
		/// <summary>
		/// Specifies the beginning of the notification period.
		/// </summary>
		public DateTime StartTime
		{
			get
			{
				byte[] datebytes = new byte[16];
				Buffer.BlockCopy(data, 20, datebytes, 0, 16);
				SystemTime st = new SystemTime(datebytes);
				
				return st.ToDateTime();
			}
			set
			{
				byte[] datebytes = SystemTime.FromDateTime(value).ToByteArray();
				Buffer.BlockCopy(datebytes, 0, data, 20, 16);
			}
		}
		#endregion

		#region End Time
		/// <summary>
		/// Specifies the end of the notification period. 
		/// </summary>
		public DateTime EndTime
		{
			get
			{
				byte[] datebytes = new byte[16];
				Buffer.BlockCopy(data, 36, datebytes, 0, 16);
				
				SystemTime st = new SystemTime(datebytes);
				
				return st.ToDateTime();
			}
			set
			{
				byte[] datebytes = SystemTime.FromDateTime(value).ToByteArray();
				Buffer.BlockCopy(datebytes, 0, data, 36, 16);
			}
		}
		#endregion
	}
	#endregion

	#region Notification Event
	/// <summary>   
	/// System Event Flags   
	/// </summary>   
	public enum NotificationEvent : int   
	{   
		/// <summary>   
		/// No events梤emove all event registrations for this application.   
		/// </summary>   
		None                    = 0x00,   
		/// <summary>   
		/// When the system time is changed.   
		/// </summary>   
		TimeChange              = 0x01,   
		/// <summary>   
		/// When data synchronization finishes.   
		/// </summary>   
		SyncEnd                 = 0x02,   
		/// <summary>   
		/// When a PC Card device is changed.   
		/// </summary>   
		DeviceChange    = 0x07,   
		/// <summary>   
		/// When an RS232 connection is made.   
		/// </summary>   
		RS232Detected   = 0x09,   
		/// <summary>   
		/// When a full device data restore completes.   
		/// </summary>   
		RestoreEnd              = 0x0A,   
		/// <summary>   
		/// When the device wakes up.   
		/// </summary>   
		Wakeup                  = 0x0B,   
		/// <summary>   
		/// When the time zone is changed.   
		/// </summary>   
		TimeZoneChange  = 0x0C,
		/// <summary>
		/// When the machines name changes.
		/// Requires Windows CE.NET 4.2.
		/// </summary>
		MachineNameChange = 0x0D,
	}   
	#endregion

	#region Notification Action
	/// <summary>
	/// Specifies the action to take when a notification event occurs.
	/// </summary>
	[Flags()]
	public enum NotificationAction : int
	{
		/// <summary>
		/// Flashes the LED.
		/// </summary>
		Led = 1,
		/// <summary>
		/// Vibrates the device.
		/// </summary>
		Vibrate = 2,
		/// <summary>
		/// Displays the user notification dialog box.
		/// </summary>
		Dialog = 4,
		/// <summary>
		/// Plays the sound specified.
		/// </summary>
		Sound = 8,
		/// <summary>
		/// Repeats the sound for 10

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -