Click here to Skip to main content
15,891,925 members
Articles / Game Development

Guess My Drawing!

Rate me:
Please Sign up or sign in to vote.
4.87/5 (21 votes)
9 Jan 2013CPOL3 min read 48.2K   697   34  
A Windows Forms application to share a whiteboard with many clients with only one drawer, in a gamy way.
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Data;

namespace WindowsFormsApplication1
{
    public class MyPanel : Panel
    {
        public static Pen p;
        public string pen_color = "blue";
        private bool mouse_down = false;
        public static Point last_point = Point.Empty;
        private Graphics g;
        ArrayList pointss;
        public static UdpClient client;

        public MyPanel()
        {
            g = this.CreateGraphics();
            p = new Pen(Color.FromName(pen_color));
            g.Clip.MakeInfinite();
            pointss = new ArrayList();
        }
        protected override void OnMouseDown(MouseEventArgs e)
        {
            mouse_down = true;
        }
        protected override void OnMouseUp(MouseEventArgs e)
        {
            mouse_down = false;
            if (Form1.isDrawer)
            {
                string msg = "draw";
                for (int i = 0; i < pointss.Count; i++)
                {
                    Point poi = (Point)pointss[i];
                    msg += ("x" + poi.X + "y" + poi.Y);
                }
                msg += "x";
                pointss.Clear();
                byte[] data = Encoding.ASCII.GetBytes(msg);
                if (client != null)
                    client.Send(data, data.Length);
            }
        }
        protected override void OnMouseMove(MouseEventArgs e)
        {
            if (Form1.isDrawer)
            {
                if (last_point.Equals(Point.Empty))
                    last_point = new Point(e.X, e.Y);
                if (mouse_down)
                {

                    IntPtr hwnd = this.Handle;

                    using (Graphics graphics = Graphics.FromHwnd(hwnd))
                    {
                        Point pMousePos = new Point(e.X, e.Y);
                        graphics.DrawLine(p, pMousePos, last_point);
                        Console.WriteLine(pMousePos + "  last  " + last_point);
                        pointss.Add(pMousePos);
                    }
                }
                last_point = new Point(e.X, e.Y);
            }
        }
    }
    
}

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
Software Developer
United States United States
Codes > Bio

Comments and Discussions