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

📄 itemsmscreator.cs

📁 Wince 上操作短信的实例
💻 CS
📖 第 1 页 / 共 2 页
字号:
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES OR INDEMNITIES.
//
using Microsoft.WindowsMobile.PocketOutlook.MessageInterception;
using Microsoft.WindowsMobile.PocketOutlook;
using System;
using System.Text.RegularExpressions;

namespace AppointmentOverSms
{

    /// <summary>
    /// Type of message.
    /// </summary>
    public enum MessageType
    {
        Appointment
    };

    /// <summary>
    /// The action that the user has taken.
    /// </summary>
    public enum ActionType
    {
        ProposalSubject,
        ProposalBody,
        Accept,
        Decline,
        Tentative
    };

    /// <summary>
    /// This class handles all the back end of this app. It creates
    /// SMS', 
    /// </summary>
    public static class SmsCreator
    {
        // Sms encoding strings
        public const string  identifierProtocol = "I:";
        public const string  appointmentProtocol = "A";
        public const string  separator = "#";
        public const string acceptActionString = "A";
        public const string proposalSubjectString = "PS";
        public const string proposalBodyString = "PB";
        public const string declineActionString = "D";
        public const string tentativeActionString = "T";
        
        //Maximum size that an SMS can take
        private const int maxSmsSize = 140;

        #region Constructor
        /// <summary>
        /// Creates two strings that contain all the information necessary to send
        /// an appointment proposal. These strings are to be sent as sms messages.
        /// </summary>
        public static void SmsCreateProposalMessages(Appointment appointment, ref string smsMessage1, ref string SmsMessage2)
        {
            int spaceLeft;
            string subject = appointment.Subject;
            DateTime start = appointment.Start;
            DateTime end = appointment.End;
            string location = appointment.Location;

            // 3 digit random number to maintain uniqueness of SMS
            Random random = new Random();
            int identifier = random.Next(1000);

            string action = GetActionString(ActionType.ProposalSubject);

            // Construct first message.
            spaceLeft = CheckSpaceAvailable(ActionType.ProposalSubject, appointment, identifier);
          
            smsMessage1 = identifierProtocol + appointmentProtocol + separator + action + separator + identifier +
                          separator + location + 
                          separator + subject.Substring(0,spaceLeft)   + 
                          separator + start.ToFileTimeUtc().ToString() +
                          separator + end.ToFileTimeUtc().ToString()   + separator;

            // Construct second message
            action = GetActionString(ActionType.ProposalBody);

            spaceLeft = CheckSpaceAvailable(ActionType.ProposalBody, appointment, identifier);

            if (appointment.Body.Length > spaceLeft)
            {
                SmsMessage2 = identifierProtocol + appointmentProtocol + separator + action + 
                              separator + identifier + 
                              separator + 
                              separator + appointment.Body.Substring(0, spaceLeft) +
                              separator + separator + separator;
            }
            else
            {
                SmsMessage2 = identifierProtocol + appointmentProtocol + 
                              separator + action +
                              separator + identifier + 
                              separator + 
                              separator + appointment.Body +
                              separator + separator + separator;
            }
        }

        /// <summary>
        /// Creates a string that contains all the information necessary to send a response to an
        /// appointment proposal. This strings is to be sent as an sms message.
        /// </summary>
        /// <param name="appointment">The appointment to respond to.</param>
        /// <param name="actionType">The action taken by the user - request/accept/decline/tentative.</param>
        /// <param name="smsMessage">the sms message string.</param>
        public static void SmsCreateResponseMessage(Appointment appointment, ActionType actionType, ref string smsMessage)
        {
            string subject  = appointment.Subject;
            string location = appointment.Location;
            DateTime start  = appointment.Start;
            DateTime end    = appointment.End;

            // 3 digit random number to maintain uniqueness of SMS
            Random random = new Random();
            int identifier = random.Next(1000);

            string action = GetActionString(actionType);

            // Construct message.
            int spaceLeft = CheckSpaceAvailable(actionType, appointment, identifier);

            smsMessage = identifierProtocol + appointmentProtocol + separator + action + separator + identifier +
                    separator + location + separator + subject.Substring(0, spaceLeft) +
                    separator + start.ToFileTimeUtc().ToString() + separator + end.ToFileTimeUtc().ToString() +
                    separator;
        }
        #endregion

        /// <summary>
        /// Helper function to check the availabe space in an SMS
        /// </summary>
        /// <param name="actionType"> Proposal body or proposal subject</param>
        /// <param name="appointment"> The appointment</param>
        /// <param name="identifier"> Identifier for the SMS</param>
        /// <returns>Space available for body/subject</returns>
        private static int CheckSpaceAvailable(ActionType actionType, Appointment appointment, int identifier)
        {
            string msg;
            int spaceLeft;
            string action = GetActionString(actionType);
            
            if (ActionType.ProposalBody == actionType)
            {
                //create "fake" msg to find out how much space will be left for body
                msg = identifierProtocol + appointmentProtocol + separator + action + 
                      separator + identifier +
                      separator + separator + separator +  separator + separator;
                
                spaceLeft = maxSmsSize - msg.Length;

                //Take as much of the body as can fit into the SMS
                if (spaceLeft > appointment.Body.Length)
                {
                    spaceLeft = appointment.Body.Length;
                }
            }
            else
            {
                //create "fake" msg to find out how much space will be left for subject
                msg = identifierProtocol + appointmentProtocol + separator + action + separator + identifier +
                    separator + appointment.Location + 
                    separator + 
                    separator + appointment.Start.ToFileTimeUtc().ToString() +
                    separator + appointment.End.ToFileTimeUtc().ToString() +
                    separator;

                spaceLeft = maxSmsSize - msg.Length;
               
                if (spaceLeft < 5)
                {
                    throw new ArgumentException("Appointment request is too large");
                }

                if (spaceLeft > appointment.Subject.Length)
                {
                    spaceLeft = appointment.Subject.Length;
                }
            }

            return spaceLeft;

⌨️ 快捷键说明

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