February 2009 - Posts

How to launch application and Press on the button on that window

  1. Launch the App
  2. Loop through Process
  3. Get the process Name Based on our Application Name. Basically this get's the process on your desktop/start bar
  4. If found the same, Get the Window Handle, process.MainWindowHandle
  5. After having the Handle, use the WinAPI 
    • [DllImport("user32.dll")]
    • [return: MarshalAs(UnmanagedType.Bool)]
    • internal static extern bool GetWindowRect(IntPtr hwnd, out Diversity.Web.Test.WebUIUnitTest.Rect lpRect);
    • Create a Struct To hold it.
    • [StructLayout(LayoutKind.Sequential)]
              public struct Rect
              {
                  public int Left{get;set;}
                  public int Top{get;set;}
                  public int Right { get; set; }
                  public int Bottom { get; set; }
                  public override bool Equals(object obj)
                  {
                      return base.Equals(obj);
                  }
                  public override int GetHashCode()
                  {
                      return base.GetHashCode();
                  }
                  public static bool operator ==(Rect first, Rect second)
                  {
                      return ((first.Left == second.Left) && (first.Right == second.Right) && (first.Top == second.Top) && (first.Bottom == second.Bottom));
                  }
                  public static bool operator !=(Rect first, Rect second)
                  {
                      return !(first == second);
                  }
              }
  6. Use the Function and Passed in the Handle, NativeMethods.GetWindowRect(hWnd, out rct);
  7. Use the Library i found earlier, which has mouse simulator, click the x and y that you want to, but please measure the distance first
You might also want to Maximize the Window first and click using the settoforeground API
Share this post: | | | |
Posted by cipto with 2 comment(s)
Filed under:

Global Mouse and Keyboard Recorder

Found a nice Library here.

I just add the serialize and deserialize feature. and it work like a charm, you don't have to pay for this kind of task.

Change the macro.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GlobalMacroRecorder
{

    ///
    /// All possible events that macro can record
    ///
    [Serializable]
    public enum MacroEventType
    {
        MouseMove,
        MouseDown,
        MouseUp,
        MouseWheel,
        KeyDown,
        KeyUp
    }

    ///
    /// Series of events that can be recorded any played back
    ///
    [Serializable]
    public class MacroEvent
    {
        public MacroEventType MacroEventType;
        public MacroKeyEventArgs keyEventArgs;
        public MacroMouseEventArgs mouseEventArgs;
        public int TimeSinceLastEvent;

        public MacroEvent(MacroEventType macroEventType, MacroKeyEventArgs eventArgs, int timeSinceLastEvent)
        {
            MacroEventType = macroEventType;
            keyEventArgs = eventArgs;
            mouseEventArgs = null;
            TimeSinceLastEvent = timeSinceLastEvent;
        }

        public MacroEvent(MacroEventType macroEventType, MacroMouseEventArgs eventArgs, int timeSinceLastEvent)
        {
            MacroEventType = macroEventType;
            keyEventArgs = null;
            mouseEventArgs = eventArgs;
            TimeSinceLastEvent = timeSinceLastEvent;
        }

    }
    [Serializable]
    public class MacroKeyEventArgs
    {
        Keys KeyData; bool SupressKeyPress;
        public MacroKeyEventArgs(Keys theKeyData, bool theSupressKeyPress)
        {
            KeyData = theKeyData;
            SupressKeyPress = theSupressKeyPress;
        }
        public KeyEventArgs GetKeyEventArgs()
        {
            KeyEventArgs args = new KeyEventArgs(KeyData);
            args.SuppressKeyPress = SupressKeyPress;
            return args;
        }
    }
    [Serializable]
    public class MacroMouseEventArgs
    {
        MouseButtons button; int clicks; int x; int y; int delta;
        public MacroMouseEventArgs(MouseButtons thebutton, int theclicks, int thex, int they, int thedelta)
        {
            button = thebutton;
            clicks = theclicks;
            x = thex;
            y = they;
            delta = thedelta;
        }
        public MouseEventArgs GetMouseEventArgs()
        {
            return new MouseEventArgs(button, clicks, x, y, delta);
        }
    }
}
 

add to our button serialize and deserialize Click Event

private void button1_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.ShowDialog();
            MessageBox.Show(folderBrowserDialog1.SelectedPath);
            //Serialize the object to file
            string path=folderBrowserDialog1.SelectedPath + @"\" + txtFileName.Text + ".my";
            MessageBox.Show(path);
            Stream stream = File.Open(path, FileMode.Create);
            BinaryFormatter bformatter = new BinaryFormatter();
            bformatter.Serialize(stream,events);
            stream.Close();
            stream.Dispose();

        }

        private void button2_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            Stream stream = File.Open(openFileDialog1.FileName, FileMode.Open);
            BinaryFormatter bformatter = new BinaryFormatter();
            events=(List<MacroEvent>)bformatter.Deserialize(stream);
            stream.Close();
            stream.Dispose();
            playBackMacroButton_Click(this, new EventArgs() { });
        }

Share this post: | | | |
Posted by cipto with no comments
Filed under:

Cross Browser SelectNodes and SingleNodes , attribute on xml document

 //extending documentelement for selectnodes
if (!window.ActiveXObject) {

    Element.prototype.selectNodes = function(sXPath) {

        var oEvaluator = new XPathEvaluator();

        var oResult = oEvaluator.evaluate(sXPath, this, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);

        var aNodes = new Array();

        if (oResult != null) {

            var oElement = oResult.iterateNext();

            while (oElement) {

                aNodes.push(oElement);

                oElement = oResult.iterateNext();

            }

        }

        return aNodes;

    }

    Element.prototype.selectSingleNode = function(sXPath) {

        var oEvaluator = new XPathEvaluator();

        // FIRST_ORDERED_NODE_TYPE returns the first match to the xpath.

        var oResult = oEvaluator.evaluate(sXPath, this, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);

        if (oResult != null) {

            return oResult.singleNodeValue;

        } else {

            return null;

        }

    }

}

 //when you have result

function dosomethingthengetresult(istrresult,strulr)

{

 var xmlDoc = loadXMLDoc(strResult);
    var xmlNodes = SelectNodes(xmlDoc, '/root/child');
    for (var i = 0; i < xmlNodes.length; i++) {
        alert(GetValueofNode(xmlNodes,i));
        alert(GetSomeId(xmlNodes,i));
        
    }

}

//example of getting value of node

function GetValueofNode(xmlNodes,index) {
    if (window.ActiveXObject)
        return xmlNodes[index].nodeTypedValue;
    else
        return xmlNodes[index].textContent;
}

//example of getting atribute
function GetSomeId(xmlNodes, index) {
    if (window.ActiveXObject)
        return xmlNodes[index].attributes[0].nodeTypedValue;
    else
        return xmlNodes[index].attributes[0].nodeValue;
}

function SelectNodes(xmlDoc, elementPath) {
    if (window.ActiveXObject || window.opera)
        return xmlDoc.selectNodes(elementPath);
    else
        return xmlDoc.documentElement.selectNodes(elementPath);       
}

 

4/3/2008

On Opera you can get the value like ex:

var xmlNodes = xmlDoc.selectNodes("//item");
    for (var i = 0; i < xmlNodes.length; i++) {
        alert(xmlNodesIdea.firstChild.nodeValue);
       
    }

I should warn about setAttribute , attributes ,setAttributeNode

it's buggy. you can see the reference 
here and
here

 

after i do a simple test it runs on all browser,

to set attribute

xmlNodesIdea.setAttribute('attrname', 'value');

to get attribute value

xmlNodesIdea.attributes.getNamedItem('attrname').value

Share this post: | | | |
Posted by cipto with no comments
Filed under:

Work Around for Escape Character FireFox CDATA

My friend ,found out that if you get the escape character  from cdata it will be translated as it is on firefox.

We do put some html tag on xml , and how to get this translated to html tag again?

already tried the unescape or decode uri but seems not running.

So the work around is using a hidden text area.

set the innerhtml of that text area to your escape character html tags.

and then get the already translated one , using the textarea.innerText( IE ) and textarea.textContent(FireFox)

 

Share this post: | | | |
Posted by cipto with 3 comment(s)

SQL Server 2005/2008 Common Table Expression

Now this is something neat.

if you use many subset of data and subquery, your performance may hit. Select something,Increase something by 1,and so on.

Now sql server does support cte, kind like a temporary table can be referenced on your query but must be immidiately referenced.

Some Article said that it's much more light in performance .

I use it for the recursive query, like there's an id and parentid

and you need to concatinate id with the parentid.

So you need like a big loop recursive on your child node and the cte is perfect for this (Faster than UDF , Faster than Sub Query)

other benefits are very easy to program, you can set a limit of the recursive row.

 

;WITH expression_name [ ( column_name [,...n] ) ]

 as

(

<anchor query>

union all

<recursive query join expression_name where parentid equals expression_name.id> 

 )

 select <colum_list> from cte

<for xml> etc

 

 

 

Share this post: | | | |
Posted by cipto with 1 comment(s)
Filed under:

XSLT on client

now this is something that always bothering me for the whole week.Smile 

Hitting rocks. but finally comes up with conclusion that most of browser these days already support XSLTProcessor and the transform.

that goes also with Safari Desktop, beginning from version 3 already support this one,earlier it's not possible to access it via javascript.

 

As i tried Sarissa For Transforming xml to xslt , it's buggy and not using the Supported one XSLTprocessor instead using it's own transformation.

Another Option for this which is running very well and quite small size 8kb. is the XSLT.js

 

XSLT.js support non jquery plugin.

Just a

new Transformation().setXml("document.xml")
       
.setXslt("document.xsl").transform("transformResult");

 

or directly the string on setxml and setxslt

 

Share this post: | | | |
Posted by cipto with no comments
Filed under: