Showing posts with label WinForm. Show all posts
Showing posts with label WinForm. Show all posts

Thursday, 26 June 2014

Catch HTML Page Events in Web Browser Control

Hello,Here is simple example to catch HTML document's control's events in Web browser control in winform.

1. Add browser control in your form. and register its CodumentCompleted  Event in constructor of form.2. Add Html document url in browser control.3. Find HTML control in Webbrowser control's event and register HTMl control's event.4.  Now you can catch HTML control's Event in your winform


// Initialize control and its event
public Form1()
        {
            InitializeComponent();
            brwWebBrowser.DocumentCompleted += WebBrowser1_DocumentCompleted;    
        }

private void WebBrowser1_DocumentCompleted(System.Object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e)
        {
            try
            {
               // Get HTML Element from HTML document
                HtmlElement Elem_btn = brwWebBrowser.Document.GetElementById("btnSave");
                if (Elem_btn != null)
                {
                    Elem_btn.Click += Elem_btn_Click;
                }

            }
            catch (Exception ex)
            {
               
            }
        }

 void Elem_btn_Click(object sender, HtmlElementEventArgs e)
        {
            HtmlElement Elem = brwWebBrowser.Document.GetElementById("txt");

            if ((Elem != null))
            {
                // Get Value from control's attribute
                string Temp_Val = Elem.GetAttribute("value");
                string strVal = Elem.GetAttribute("value");
            }
        }



HTML Page

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>  
</head>
<body>

    <input type="text" id="txt" />
    <input type="button" id="btnSave" value="Save" />

</body>

</html>

Thursday, 30 August 2012

Serialize/Deserialize XML from Class


Class File :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace WindowsFormsApplication2
{
    [Serializable]
    [XmlRoot(ElementName = "Person")]
    public class clsRoot
    {
        public clsRoot() { Address = new Address(); }

        [XmlElement("Name")]
        public string Name { getset; }

        [XmlElement("Job")]
        public string Job { getset; }

        public Address Address { getset; }

    }


    [Serializable]
    [XmlRoot(ElementName = "Address")]
    public class Address
    {
        public Address() { }       
        public Permanant Permanant { getset; }
        public Current Current { getset; }
    }


    [Serializable]
    [XmlRoot(ElementName = "Permanant")]
    public class Permanant
    {
        public Permanant() { }
        [XmlAttribute("Type")]
        public string Type { getset; }
        public string Address { getset; }
        public string City { getset; }
        public string State { getset; }
        public string PinCode { getset; }
    }


    [Serializable]
    [XmlRoot(ElementName = "Current")]
    public class Current
    {
        public Current() { }
        [XmlAttribute("Type")]
        public string Type { getset; }
        public string Address { getset; }
        public string City { getset; }
        public string State { getset; }
        public string PinCode { getset; }
    }

}


Form with Read/Write button
  private void Generate_Click(object sender, EventArgs e)
        {
            clsRoot clsRoot = new clsRoot();
            clsRoot.Name = "Ajay Patel";
            clsRoot.Job = "Software Developer";

            Address ADDr = new Address();
            Permanant PR = new Permanant();
            Current Cur = new Current();

            PR.Type = "Permanant";

            PR.Address = "Panchvati";
            PR.City = "Kalol";
            PR.State = "Gujarat";
            PR.PinCode = "382722";

            Cur.Type = "Current";
            Cur.Address = "Panchvati";
            Cur.City = "Kalol";
            Cur.State = "Gujarat";
            Cur.PinCode = "382722";

            ADDr.Permanant = PR;
            ADDr.Current = Cur;

            clsRoot.Address = ADDr;

            System.Xml.Serialization.XmlSerializer xmlSerl = new
               System.Xml.Serialization.XmlSerializer(clsRoot.GetType());
            StreamWriter SW = new StreamWriter(txtXML.Text);
            xmlSerl.Serialize(SW, clsRoot);
            SW.Close();


        }
        private void txtXML_MouseDoubleClick(object sender,
                                                            MouseEventArgs e)
        {
            FileDilog.ShowDialog();
            txtXML.Text = FileDilog.FileName;
        }

        private void txtRead_Click(object sender, EventArgs e)
        {
            clsRoot clsRoot = new clsRoot();
            XmlSerializer xmlSerl = new XmlSerializer(clsRoot.GetType());
            TextReader objTxtRead = new StreamReader(txtXML.Text);
            clsRoot = (clsRoot)xmlSerl.Deserialize(objTxtRead);
            objTxtRead.Close();
        }

XML :
<?xml version="1.0" encoding="utf-8"?>
<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Name>Ajay Patel</Name>
  <Job>Software Developer</Job>
  <Address>
    <Permanant Type="Permanant">
      <Address>Panchvati</Address>
      <City>Kalol</City>
      <State>Gujarat</State>
      <PinCode>382722</PinCode>
    </Permanant>
    <Current Type="Current">
      <Address>Panchvati</Address>
      <City>Kalol</City>
      <State>Gujarat</State>
      <PinCode>382722</PinCode>
    </Current>
  </Address>
</Person>

Tuesday, 1 May 2012

Print Content of Panel in winform


private void btnPrint_Click(object sender, EventArgs e)
        {
         Pd.PrintPage += PrintPage;
         Pd.DefaultPageSettings.PaperSize = new PaperSize("Custom",
                                                           panel1.Width+20,
                                                           panel1.Height+15 );
         Pd.Print();
        }
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern long BitBlt(IntPtr hdcDest, int nXDest,
                                 int nYDest, int nWidth,
                                 int nHeight, IntPtr hdcSrc,
                                 int nXSrc, int nYSrc,
                                 int dwRop);

        private void CaptureScreen(Panel pnl)
        {
            Graphics mygraphics = pnl.CreateGraphics();
            Size s = pnl.Size;
            memoryImage = new Bitmap(s.Width, s.Height, mygraphics);
            Graphics memoryGraphics = Graphics.FromImage(memoryImage);
            IntPtr dc1 = mygraphics.GetHdc();
            IntPtr dc2 = memoryGraphics.GetHdc();
            BitBlt(dc2, 0, 0, pnl.Width, pnl.Height, dc1, 0, 0, 13369376);
            mygraphics.ReleaseHdc(dc1);
            memoryGraphics.ReleaseHdc(dc2);
        }
private void PrintPage(System.Object sender,
                       System.Drawing.Printing.PrintPageEventArgs e)
        {
            CaptureScreen(panel1);
            e.Graphics.DrawImage(memoryImage, 5, 5);
        }