Click here to Skip to main content
15,892,697 members
Articles / Programming Languages / C# 4.0

Reading Ultrabook Sensor Data with the Windows 8 Sensor API

Rate me:
Please Sign up or sign in to vote.
5.00/5 (10 votes)
6 Nov 2012CPOL2 min read 29.9K   1.1K   6  
A head start for App Innovation contestants
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RedCell.Windows.Forms.Metro
{
    /// <summary>
    /// Base class for Metro-style user controls.
    /// </summary>
    public partial class UserControl : System.Windows.Forms.UserControl
    {
        #region Initialization
        /// <summary>
        /// Initializes a new instance of the <see cref="UserControl" /> class.
        /// </summary>
        public UserControl()
        {
            InitializeComponent();
            Opacity = 0.2f;
            SetStyle(ControlStyles.UserPaint | ControlStyles.SupportsTransparentBackColor, true);
        }
        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets the opacity.
        /// </summary>
        /// <value>The opacity.</value>
        public float Opacity { get; set; }
        #endregion

        #region Methods
        /// <summary>
        /// Paints the background of the control.
        /// </summary>
        /// <param name="e">A <see cref="T:System.Windows.Forms.PaintEventArgs" /> that contains the event data.</param>
        protected override void OnPaintBackground(PaintEventArgs e)
        {
            base.OnPaintBackground(e);
            var g = e.Graphics;
            using(var brush = new SolidBrush(Color.FromArgb((int)(Opacity*0xff), BackColor)))
                g.FillRectangle(brush, e.ClipRectangle);
        }

        /// <summary>
        /// Raises the <see cref="E:System.Windows.Forms.Control.Paint" /> event.
        /// </summary>
        /// <param name="e">A <see cref="T:System.Windows.Forms.PaintEventArgs" /> that contains the event data.</param>
        protected override void OnPaint(PaintEventArgs e)
        {
            var g = e.Graphics;
            using (var brush = new SolidBrush(Color.FromArgb((int)(Opacity * 0xff), BackColor)))
                g.FillRectangle(brush, e.ClipRectangle);
            base.OnPaint(e);
        }

        /// <summary>
        /// Gets the create params.
        /// </summary>
        /// <value>The create params.</value>
        /// <returns>A <see cref="T:System.Windows.Forms.CreateParams" /> that contains the required creation parameters when the handle to the control is created.</returns>
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                //cp.ExStyle |= 0x00000020; //WS_EX_TRANSPARENT 
                return cp;
            }
        }

        /// <summary>
        /// Invalidates the ex.
        /// </summary>
        protected void InvalidateEx()
        {
            if (Parent == null)
                return;

            Rectangle rc = new Rectangle(this.Location, this.Size);
            Parent.Invalidate(rc, true);
        }
        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer Robotic Assistance Devices / AITX
Canada Canada
Yvan Rodrigues has 30 years of experience in information systems and software development for the industry. He is Senior Concept Designer at Robotic Assistance Devices

He is a Certified Technician (C.Tech.), a professional designation granted by the Institute of Engineering Technology of Ontario (IETO).

Yvan draws on experience as owner of Red Cell Innovation Inc., Mabel's Labels Inc. as Manager of Systems and Development, the University of Waterloo as Information Systems Manager, and OTTO Motors as Senior Systems Engineer and Senior Concept Designer.

Yvan is currently focused on design of embedded systems.

Comments and Discussions