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

📄 home.cs

📁 在mobile上面实现iphone的ui
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using Microsoft.WindowsMobile.Status;

//大概里面所有的界面布局,该主页只完成了绘制,和构造的功能

namespace iPhoneUI
{
    public partial class Home : Form
    {
        string path = "";
        Bitmap backImage;
        Bitmap topBar;
        Bitmap topLock;
        Bitmap batteryLevelMedium ;
        Bitmap batteryLevelLow ;
        Bitmap batteryLevelVeryLow1;
        Bitmap batteryLevelVeryLow2;
        Bitmap batteryLevelVeryHigh;
        Bitmap batteryLevelHigh;
        Bitmap signalLevelVeryHigh;
        Bitmap signalLevelHigh;
        Bitmap signalLevelMedium;
        Bitmap signalLevelLow;
        Bitmap signalLevelVerylow1;
        Bitmap signalLevelVerylow2;
        
        
        Bitmap slideAni;
        Font timeFont;
        Font dateFont;
        Font captionFont;
        string time = "";
        Graphics gxBuffer;
        Bitmap offBitmap;        
        string date = "";
        string caption = "China Mobile";

        Brush whiteBrush;        

        bool _HideBattery = false;
        bool _HideSignal = false;

        private SlideButton arrowButton;

        int LastXAnimation = 0;
        MainMenu _iPhoneMainMenu; 
        

        public Home()
        {
            InitializeComponent();

            path = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
            //得到当前的根目录
            backImage = new Bitmap(path + @"\BMP\wallpaper.bmp");
            topBar = new Bitmap(path + @"\BMP\topbar.bmp");
            topLock = new Bitmap(path + @"\BMP\toplock.bmp");

            batteryLevelVeryHigh = new Bitmap(path + @"\BMP\BatteryLevelVeryHigh.bmp");
            batteryLevelHigh    = new Bitmap(path + @"\BMP\BatteryLevelHigh.bmp");
            batteryLevelMedium  = new Bitmap(path + @"\BMP\BatteryLevelMedium.bmp");
            batteryLevelLow     = new Bitmap(path + @"\BMP\BatteryLevelLow.bmp");
            batteryLevelVeryLow1 = new Bitmap(path + @"\BMP\BatteryLevelVeryLow1.bmp");
            batteryLevelVeryLow2 = new Bitmap(path + @"\BMP\BatteryLevelVeryLow2.bmp");

            signalLevelVeryHigh = new Bitmap(path + @"\BMP\SignalLevelVeryHigh.bmp");
            signalLevelHigh = new Bitmap(path + @"\BMP\SignalLevelHigh.bmp");
            signalLevelMedium = new Bitmap(path + @"\BMP\SignalLevelMedium.bmp");
            signalLevelLow = new Bitmap(path + @"\BMP\SignalLevelLow.bmp");
            signalLevelVerylow1 = new Bitmap(path + @"\BMP\SignalLevelVeryLow1.bmp");
            signalLevelVerylow2 = new Bitmap(path + @"\BMP\SignalLevelVeryLow2.bmp");

            
            slideAni = new Bitmap(path + @"\BMP\slideAni.bmp");
            //slideAni = new Bitmap(path + @"\BMP\SignalLevelMedium.bmp");

            arrowButton = new SlideButton(this, new Point(16, 274));
            arrowButton.Image = new Bitmap(path + @"\BMP\arrow.bmp");
            arrowButton.ImageDown = new Bitmap(path + @"\BMP\arrowDown.bmp");

            timeFont = new Font("Tahoma", 10, FontStyle.Regular);
            dateFont = new Font("Tahoma", 9, FontStyle.Regular);
            captionFont=new Font("Tahoma", 22, FontStyle.Regular);
            whiteBrush = new SolidBrush(Color.White);
            
            time = DateTime.Now.ToString("hh:mm");
            date = DateTime.Now.DayOfWeek.ToString() + ", " + DateTime.Now.ToString("MMMM") + " " + DateTime.Now.Day.ToString();
            //caption = new string("China Mobile");
            offBitmap = new Bitmap(this.Width, this.Height);

            _iPhoneMainMenu = new MainMenu();            

            timerSlideAnimation.Enabled = true;//
            timerSlideAnimation.Interval = 2;

          //  ShowTaskBar(false);
        }

        /*public static void ShowTaskBar(bool bShow)//
        {
            {
                IntPtr h = PlatformAPIs.FindWindowW("HHTaskBar", "");
                PlatformAPIs.ShowWindow(h, bShow? (int)PlatformAPIs.ShowWindowFlags.SW_SHOW:(int)PlatformAPIs.ShowWindowFlags.SW_HIDE);
                PlatformAPIs.EnableWindow(h, false);
            }
        }*/
    

        private void Form1_ParentChanged(object sender, EventArgs e)
        {

        }

        protected override void OnPaint(PaintEventArgs e)
            //貌似把所有的图案都绘到内存里了
        {
            if (arrowButton.unLock)//如果解锁,则显示主菜单界面    
            {
                arrowButton.unLock = false; 
                _iPhoneMainMenu.ShowDialog();                
                arrowButton.ResetPosition();                
            }
            
            {
                //首先要知道所有图像都是向Graphics上面绘制
                gxBuffer = Graphics.FromImage(offBitmap);

                //Graphics gxBuffer = e.Graphics;
                gxBuffer.Clear(this.BackColor);
                //Draw background first
                gxBuffer.DrawImage(backImage, 0, 0);//画出背景
                DrawTop(ref gxBuffer);   
                    
                DrawAlpha(gxBuffer, topBar, 170, 0, topLock.Height);//完成绘制半透明的效果

                time = DateTime.Now.ToString("hh:mm");
                DrawTime(time, gxBuffer, topLock.Height +4);

                DrawCaption(caption, gxBuffer, topLock.Height + 12);

                SizeF size = gxBuffer.MeasureString(date, dateFont);
                int x = this.Width / 2 - (int)size.Width / 2;

                gxBuffer.DrawString(date, dateFont, whiteBrush, x, 70);
           
                DrawAlphaAnimation(gxBuffer, slideAni, 160, 0, 258);//这个负责画按钮外面的动态框
                this.Invalidate();//触发paint事件

                arrowButton.Paint(gxBuffer);
                e.Graphics.DrawImage(offBitmap, 0, 0);                
            }

            if (_iPhoneMainMenu.CloseApp)
                Close();
        }

        private void DrawTop(ref Graphics  gxBuffer)
        {
            //this.Invalidate(new Rectangle(0, 0, topLock.Width + signal.Width + batteryLevelHigh.Width, topLock.Height));
            //DrawAlpha(gxBuffer, signalLevelVeryHigh, 200, 0, 0);
            DrawAlpha(gxBuffer, GetGPSSignalImage(), 200, 0, 0);
            DrawAlpha(gxBuffer, topLock, 200, signalLevelVeryHigh.Width, 0);
            DrawAlpha(gxBuffer, GetBatteryImage(), 200, topLock.Width + signalLevelVeryHigh.Width, 0);            
        }

        private Bitmap GetBatteryImage()
        {            
            switch (SystemState.PowerBatteryStrength)
            {
                case BatteryLevel.VeryHigh:
                    return batteryLevelVeryHigh;
                    
                case BatteryLevel.High:
                    return batteryLevelHigh;
                    
                case BatteryLevel.Medium:
                    return batteryLevelMedium;
                    
                case BatteryLevel.Low:
                    return batteryLevelLow;
                    
                case BatteryLevel.VeryLow:
                    _HideBattery = !_HideBattery;
                    if (_HideBattery)
                        return batteryLevelVeryLow1;
                    else
                        return batteryLevelVeryLow2;                                        
            }
            return null;
        }

        private Bitmap GetGPSSignalImage()
        {
            int SignalStrength = SystemState.PhoneSignalStrength;

            if (SignalStrength > 80)
                return signalLevelVeryHigh;

            if (SignalStrength > 60)
                return signalLevelHigh;

            if (SignalStrength > 50)
                return signalLevelMedium;

            if (SignalStrength > 20)
                return signalLevelLow;

            //else
            {
                _HideSignal = !_HideSignal;
                if (_HideSignal)
                    return signalLevelVerylow1;
                else
                    return signalLevelVerylow2;
            }

            return null;
        }

    


        protected override void OnPaintBackground(PaintEventArgs e)
        {
            //base.OnPaintBackground(e);
        }

        private void DrawTime(string time, Graphics gx, int y)
        {

            SizeF size = gx.MeasureString(time, timeFont);
            //int x = this.Width / 2 - (int)size.Width / 2;
            int x = this.Width - (int)size.Width - 2; //加入size之后实现位置随字体改变
            gx.DrawString(time, timeFont, new SolidBrush(Color.White), x, y);

        }

        private void DrawCaption(string caption, Graphics gx, int y)
        {

            SizeF size = gx.MeasureString(caption, captionFont);
            //int x = this.Width / 2 - (int)size.Width / 2;
            int x = this.Width/2 - (int)size.Width/2;
            gx.DrawString(caption, captionFont, new SolidBrush(Color.White), x, y);

        }



        //调用: DrawAlpha(gxBuffer, topBar, 170, 0, topLock.Height);
        /* 本函数是该程序的核心,trans透明度,X、Y是目标位图上的逻辑坐标
         */
         
        private void DrawAlpha(Graphics gx, Bitmap image, byte transp, int x, int y)
        {
            using (Graphics gxSrc = Graphics.FromImage(image))
            {
                IntPtr hdcDst = gx.GetHdc();
                IntPtr hdcSrc = gxSrc.GetHdc();
                BlendFunction blendFunction = new BlendFunction();
                blendFunction.BlendOp = (byte)BlendOperation.AC_SRC_OVER;   // Only supported blend operation
                blendFunction.BlendFlags = (byte)BlendFlags.Zero;           // Documentation says put 0 here
                blendFunction.SourceConstantAlpha = transp;                 // Constant alpha factor
                blendFunction.AlphaFormat = (byte)0;                        // Don't look for per pixel alpha
                PlatformAPIs.AlphaBlend(hdcDst, x, y, image.Width, image.Height, hdcSrc, 0, 0, image.Width, image.Height, blendFunction);
                gx.ReleaseHdc(hdcDst);    // Required cleanup to GetHdc()
                gxSrc.ReleaseHdc(hdcSrc);       // Required cleanup to GetHdc()
            }
        }

        

        private void DrawAlphaAnimation(Graphics gx, Bitmap image, byte transp, int x, int y)
        {
            LastXAnimation = LastXAnimation + 240; //实现高亮滚动显示
            if (LastXAnimation >= slideAni.Width)
            {
                LastXAnimation = 0;
            }

            //call :  DrawAlphaAnimation(gxBuffer, slideAni, 160, 0, 258);
            int frame = LastXAnimation;

            using (Graphics gxSrc = Graphics.FromImage(image))
            {
                //this.Invalidate(new Rectangle(0, 258, slideAni.Width, slideAni.Height));
                IntPtr hdcDst = gx.GetHdc();
                IntPtr hdcSrc = gxSrc.GetHdc();
                BlendFunction blendFunction = new BlendFunction();
                blendFunction.BlendOp = (byte)BlendOperation.AC_SRC_OVER;   // Only supported blend operation
                blendFunction.BlendFlags = (byte)BlendFlags.Zero;           // Documentation says put 0 here
                blendFunction.SourceConstantAlpha = transp;// Constant alpha factor
                blendFunction.AlphaFormat = (byte)0;                        // Don't look for per pixel alpha
              //  PlatformAPIs.AlphaBlend(hdcDst, x, y, image.Width, image.Height, hdcSrc, 0, y + frame, image.Width, image.Height, blendFunction);
                PlatformAPIs.AlphaBlend(hdcDst, x - frame, y, image.Width, image.Height, hdcSrc, 0, 0, image.Width, image.Height, blendFunction);
                //x-frame与滚动高亮显示相关
                gx.ReleaseHdc(hdcDst);    // Required cleanup to GetHdc()
                gxSrc.ReleaseHdc(hdcSrc);       // Required cleanup to GetHdc()
            }
        }

        private void hScrollBar_ValueChanged(object sender, EventArgs e)
        {
            this.Invalidate();
        }        

        private void Form1_Load(object sender, EventArgs e)
        {

        }        

        private void Home_Activated(object sender, EventArgs e)
        {

        }

        private void Home_Closing(object sender, CancelEventArgs e)
        {
//            ShowTaskBar(true);
        }        
    }
}

⌨️ 快捷键说明

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