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

📄 pimitemlistview.cs

📁 通过手机短消息来协调会议等
💻 CS
📖 第 1 页 / 共 2 页
字号:
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//

using System;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using Microsoft.WindowsMobile.PocketOutlook;


namespace Microsoft.WindowsMobile.Samples.ControlLibrary
{
    /// <summary>
    /// Base class for TaskListView, ContactListView and AppointmentListView
    /// </summary>
    public partial class PimItemListView: System.Windows.Forms.UserControl, IDisposable
    {
        #region Private and protected member variables
        //
        //   Cell:
        //   +---------------------------------------------+   +
        //   |                           +---------+  | /\ |   | 
        //   |   First Label- property1  |         |  |    |   |
        //   |                           |         |  |    |   | e
        //   |   Second Label -property2 |         |  |    |   |
        //   |                           +---------+  | \/ |   |
        //   +---------------------------------------------+   +
        //   <->                          <--------><-><--->
        //    a                                b     c   d
        //
        //   a: borderWidth
        //   b: pictureWidth
        //   c: pictureBorderWidth
        //   d: Scrollbar 
        //   e: cellHeight
                
        private int pimItemsShowing;            // Number of PimItems currently showing
        private int maxCellCount;               // Maximum number of cells that fit in the control
        private int cellHeight;                 // Height of each pimItem on the view
        private int cellHeightDiv2;             // Height divided by 2
        private int pictureWidth;               // Picture width
        private int pictureHeight;              // Picture height

        private int borderWidth;                // Label border width
        private int pictureBorderWidth;         // Picture border width

        private int minControlHeight;           // Minimum control height
        private int minControlWidth;            // Minimum control width
        private int scrollValue;                // Current value of the scrollbar
        private int selectedCell;               // Selected cell

        private Color highlightBackgroundColor; // Highlighted cell background 
        private Color backgroundColor;          // Control default background 
        private Color highlightTextColor;       // Highlighted cell text color 
        private Color textColor;                // Control default text color

        protected bool showPicture;             // Show the picture?
        protected ContactProperty property1;    // Property shown by first label for a pimItem
        protected ContactProperty property2;    // Property shown by second label for a pimItem

        private PimItemCollection pimItemsCollection; //PimItem collection passed in by the user

        private Font line1Font;                 // Font used in cell's first line.
        private Font line2Font;                 // Font used in cell's second line.

        // Objects required for rendering 
        private Graphics   formGraphics;
        private Graphics backGraphics;
        private Bitmap backBuffer;
        private SolidBrush backgroundBrush;
        private SolidBrush highlightBackgroundBrush;
        private SolidBrush textBrush;
        private SolidBrush hightlightTextBrush;
        private Pen pen;

        private bool disposed = false;

        #endregion

        #region Public Event
        public class PimItemListViewEventArgs : EventArgs
        {
            #region Public
            public PimItem PimItem
            {
                get
                {
                    return pimItem;
                }
            }
            #endregion

            #region Private
            private PimItem pimItem;
            #endregion

            #region Internal
            internal PimItemListViewEventArgs(PimItem pimItem)
            {
                this.pimItem = pimItem;
            }
            #endregion
        }
        public delegate void ClickEventHandler(object sender, PimItemListViewEventArgs item);
        #endregion

        #region Constructor and Initializers
        /// <summary>
        /// Constructor: Initialize all the values
        /// </summary>
        public PimItemListView()
        {
            InitializeComponent();

            InitializeWithDefaultValues();
            ResetGeometry();

            vScrollBar1.ValueChanged += new EventHandler(vScrollBar1_ValueChanged);
            this.Resize += new System.EventHandler(this.FormResized);
            this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.OnMouseDown);
            this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);            
        }

        /// <summary>
        /// Initializes private members with default values.
        /// </summary>
        private void InitializeWithDefaultValues()
        {
            //Setting background colors and color for highlighting            
            backgroundColor = System.Drawing.SystemColors.Window;
            textColor = System.Drawing.SystemColors.WindowText;
            highlightBackgroundColor = System.Drawing.SystemColors.Highlight;
            highlightTextColor = System.Drawing.SystemColors.HighlightText;
            
            //Setting graphic objects
            formGraphics = this.CreateGraphics();

            line1Font = new Font("Microsoft Sans Serif", 8, FontStyle.Regular);
            line2Font = new Font("Microsoft Sans Serif", 8, FontStyle.Regular);
            backgroundBrush = new SolidBrush(backgroundColor);
            highlightBackgroundBrush = new SolidBrush(highlightBackgroundColor);
            textBrush = new SolidBrush(textColor);
            hightlightTextBrush = new SolidBrush(highlightTextColor);
            pen = new Pen(textColor);

            //Height of each cell for one PimItem (two labels for every pimItem)
            cellHeight = 32;

            borderWidth = 2;

            //height offset for the picture so that it appears on the center
            //of the pimItem "panel"            
            pictureBorderWidth = 0;

            //Minimum height/width that the control can take
            minControlHeight = 36;
            minControlWidth = 120;

            //Change when the arrows on the scrollbar is clicked
            vScrollBar1.SmallChange = 1;

            //Default property1
            property1 = ContactProperty.FirstName;

            //Default property2
            property2 = ContactProperty.MobileTelephoneNumber;

            //Default - don't show pictures.
            showPicture = false;
        }

        /// <summary>
        /// Resets all the values that depend on the size of the control
        /// or on the cellHeight
        /// </summary>
        private void ResetGeometry()
        {
            //Height of each pimItem on screen (height of one label)
            cellHeightDiv2 = cellHeight / 2;

            //Number of cells that can be drawn.
            maxCellCount = this.ClientSize.Height / cellHeight;

            // Actual number of items that we can display is
            pimItemsShowing = 0;
            vScrollBar1.Hide();

            if (pimItemsCollection != null && pimItemsCollection.Count > 0)
            {
                //Dont show more than maximum pimItems
                if (pimItemsCollection.Count > maxCellCount)
                {
                    pimItemsShowing = maxCellCount;
                    vScrollBar1.Location = new System.Drawing.Point(this.ClientSize.Width - vScrollBar1.Width, 0);
                    vScrollBar1.Minimum = 0;
                    vScrollBar1.Maximum = pimItemsCollection.Count - 1;
                    vScrollBar1.LargeChange = pimItemsShowing;
                    vScrollBar1.Size = new System.Drawing.Size(vScrollBar1.Width, maxCellCount * cellHeight + 1);                    
                    vScrollBar1.Show();
                }
                else
                {                    
                    pimItemsShowing = pimItemsCollection.Count;
                    this.ClientSize = new System.Drawing.Size(this.ClientSize.Width, cellHeight * pimItemsShowing + 1);
                }
            }

            //Picture width and height. Maintain 4:3 ration between height
            //and picture to be consistent with the way pictures are shown
            //on the built-in pimItem viewer           
            pictureHeight = cellHeight - 2 * pictureBorderWidth - 1; //1 is the divider line width.
            pictureWidth = pictureHeight * 3 / 4;

            //Creating a back buffer to make scrolling smooth.
            if (backGraphics != null)
            {
                backGraphics.Dispose();
            }

            if (backBuffer != null)
            {
                backBuffer.Dispose();
            }
            backBuffer = new Bitmap(this.Size.Width, this.Size.Height);
            backGraphics = Graphics.FromImage(backBuffer);
        }
        #endregion

        #region Public Event
        /// <summary>
        /// Raised when a pimItem is selected
        /// </summary>
        public event ClickEventHandler PimItemSelected;
        #endregion

        #region Scrollbar Event Handler
       /// <summary>
       /// Eventhandler for scroll bar changing value
       /// </summary>
       /// <param name="sender"> Sender</param>
       /// <param name="e"> EventArgs</param>
       /// <remarks> This is called twice on every value change. Thats why the
       /// if clase is required.</remarks>
       private void vScrollBar1_ValueChanged(Object sender,
           EventArgs e)
       {
           if (scrollValue != vScrollBar1.Value)
           {
               UpdateDisplay();
           }
       }
       #endregion

        #region Resize Handler
       /// <summary>
       /// Handles resizing of the control
       /// </summary>
       /// <param name="sender"> Sender with resizing information</param>
       /// <param name="e"> EventArgs</param>
       private void FormResized(object sender, System.EventArgs e)
       {
           Control control = (Control)sender;
           int newHeight = control.Size.Height;
           int newWidth = control.Size.Width;

           //Dont go below a minimum height
           if (newHeight < minControlHeight)
           {
               newHeight = minControlHeight;
           }

           //Dont go below a minimum width
           if (newWidth < minControlWidth)
           {
               newWidth = minControlWidth;               
           }

⌨️ 快捷键说明

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