Simulation de console pour windows mobile

Description

Pour faire court, lorsqu'on développe en .NET sous WM, on ne dispose pas des mêmes outils que sous windows: pas de console, pas de véritable chrono à se mettre sous la dent pour trouver les processus qui sont trop lents...
Bref, la classe que je vous propose bien que très simple permet de remplir quelques lacunes. Je l'ai faite (baclé ?) en 2h, avec moins 2 mois d' "expérience" en C#... et pourtant elle me rend de grands services !

C'est la 2ième fois que j'essais de poster ce code... comme je n'ai pas envie de re écrire ce que j'ai déjà fait 2 fois (sur mon site et ici), voici le lien direct sur mon site internet où j'explique pourquoi et comment se servir de la classe etc...

http://newprograms.free.fr/CSHARP/index.php

Source / Exemple :


using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Drawing;                   // Point
using System.Runtime.InteropServices;   // DllImport
using System.Windows.Forms;             // Form
using System.IO;                        // StreamWriter

namespace OCagenda
{
    class ClassDebugForWM
    {
        [DllImport("coredll.dll")]
        public static extern int GetTickCount();

        List<long> lstart = new List<long>();

        Panel pchrono = new Panel();
        TextBox lchrono = new TextBox();
        int unite;
        string ini = "";

        public void init(Color c, string repini)
        {
            ini = repini;
            lchrono.Parent = pchrono;
            pchrono.Location = new Point(50, 100);
            pchrono.BackColor = c;
            lchrono.Multiline = true;
            lchrono.BackColor = Color.White;
            lchrono.Font = new Font("Arial", 7, FontStyle.Regular);
            lchrono.WordWrap = false;
            lchrono.ScrollBars = ScrollBars.Both;
            unite = lchrono.Height;
            pchrono.ClientSize = new Size(122, unite * 7 + 2);
            lchrono.Location = new Point(1, 1 + unite);
            lchrono.Size = new Size(120, unite * 6);
            pchrono.MouseMove += new MouseEventHandler(TitreFenetre_MouseMove);
            pchrono.MouseDown += new MouseEventHandler(TitreFenetre_MouseDown);
            pchrono.MouseUp += new MouseEventHandler(TitreFenetre_MouseUp);
            FenetreUtile = Screen.PrimaryScreen.WorkingArea;
            //
            Button b3 = new Button();
            b3.Location = new Point(1, 1);
            b3.Size = new Size(unite, unite);
            b3.Font = lchrono.Font;
            b3.Text = "-";
            b3.Parent = pchrono;
            b3.Click += delegate(Object sender, EventArgs e)
            {
                if (b3.Text == "-")
                {
                    pchrono.ClientSize = new Size(122, unite * 1 + 2);
                    lchrono.Height = unite * 0;
                    b3.Text = "+";
                }
                else 
                {
                    pchrono.ClientSize = new Size(122, unite * 7 + 2);
                    lchrono.Height = unite * 6;
                    b3.Text = "-";
                }
            };
            //
            Button b1 = new Button();
            b1.Location = new Point(1 +unite, 1);
            b1.Size = new Size(unite, unite);
            b1.Font = lchrono.Font;
            b1.Text = "0";
            b1.Parent = pchrono;
            b1.Click += delegate(Object sender, EventArgs e)
            {
                lchrono.Text = "";
            };
            //
            Button b2 = new Button();
            b2.Location = new Point(1 + unite * 2, 1);
            b2.Size = new Size(unite, unite);
            b2.Font = lchrono.Font;
            b2.Text = "10";
            b2.Parent = pchrono;
            b2.Click += delegate(Object sender, EventArgs e)
            {
                string[] st = lchrono.Text.Split('\r');

                if (st.Length > 10)
                {
                    lchrono.Text = st[0];

                    for (int i = 1; i < 10; i++)
                        lchrono.Text += "\r\n" + st[i].Substring(1, st[i].Length-1);
                }
            };
            //
            Button b4 = new Button();
            b4.Location = new Point(1 + unite * 3, 1);
            b4.Size = new Size(unite, unite);
            b4.Font = lchrono.Font;
            b4.Text = "s";
            b4.Parent = pchrono;
            b4.Click += delegate(Object sender, EventArgs e)
            {
                WriteIni();
            };
        }

        // --------------------------------------------------------------------------------------------------
        
        public void WriteIni()
        {
            try
            {
                using (StreamWriter sw = File.CreateText(ini + "\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".txt"))
                {
                    string[] st = lchrono.Text.Split('\r');

                    if (st.Length > 1)
                    {
                        sw.WriteLine(st[0]);

                        for (int i = 1; i < st.Length; i++)
                            sw.WriteLine(st[i].Substring(1, st[i].Length - 1));
                    }
                    sw.Close();
                }
            }
            catch
            {
                // si l'écriture dans le mobile échoue, je ne fais rien !!!!
            }
        }
        
        // --------------------------------------------------------------------------------------------------

        Point MousePositionInCaption;

        private void TitreFenetre_MouseDown(object sender, MouseEventArgs e)
        {
            MousePositionInCaption.X = e.X;
            MousePositionInCaption.Y = e.Y;
        }

        Rectangle FenetreUtile;
        Boolean NouveauTraitement = true;
        Point NouvellePositionForm = new Point();
        Point AnciennePosition = new Point();
        //static Point this_Location = new Point();

        private void TitreFenetre_MouseMove(object sender, MouseEventArgs e)
        {
            if (NouveauTraitement)
            {
                NouveauTraitement = false;
                // e.X, e.Y = position de la souris dans le controle
                NouvellePositionForm.X = pchrono.Location.X + (e.X - MousePositionInCaption.X);
                NouvellePositionForm.Y = pchrono.Location.Y + (e.Y - MousePositionInCaption.Y);

                if (!AnciennePosition.Equals(NouvellePositionForm))
                {
                    AnciennePosition.X = NouvellePositionForm.X;
                    AnciennePosition.Y = NouvellePositionForm.Y;
                }

                NouveauTraitement = true;
            }
        }

        private void TitreFenetre_MouseUp(object sender, MouseEventArgs e)
        {
            pchrono.Top = Math.Min(Math.Max(0, NouvellePositionForm.Y), Screen.PrimaryScreen.WorkingArea.Height -24 - pchrono.Height);
            pchrono.Left = Math.Min(Math.Max(0, NouvellePositionForm.X), Screen.PrimaryScreen.WorkingArea.Width - pchrono.Width);
        }

        // --------------------------------------------------------------------------------------------------

        public void show(string t, Boolean Clear)
        {
            if (Clear || (lchrono.Text == ""))
                lchrono.Text = t;
            else
                lchrono.Text = t + "\r\n" + lchrono.Text;
        }

        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

        public void show(Form f, string t, Boolean Clear)
        {
            show(t, Clear);
            pchrono.Parent = f;
            pchrono.Visible = true;
            pchrono.BringToFront();
        }

        // --------------------------------------------------------------------------------------------------
        
        public void start()
        {
            lstart.Add(GetTickCount());
        }

        // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
        
        public void end(Form f, string s, Boolean Clear)
        {
            long lduree = GetTickCount() - lstart[lstart.Count - 1];
            string t = lstart.Count.ToString() + " ; " +lduree.ToString() + (s != "" ? " ; " + s : "");
            show(f, t, Clear);
            lstart.RemoveAt(lstart.Count - 1);
        }

        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

        public void end(string s, Boolean Clear)
        {
            long lduree = GetTickCount() - lstart[lstart.Count - 1];
            string t = lstart.Count.ToString() + " ; " + lduree.ToString() + (s != "" ? " ; " + s : "");
            show(t, Clear);
            lstart.RemoveAt(lstart.Count - 1);
        }

        // --------------------------------------------------------------------------------------------------
    }
}

Conclusion :


http://newprograms.free.fr/CSHARP/index.php

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.