.NET Logo
Welcome Guest Search | Active Topics | Members | Log In | Register

การเขียนโปรแกรมเพื่อควบคุมโปรแกรม Options · View
paedotnet
Posted: Saturday, September 13, 2008 2:24:09 PM

Rank: มือเทพ
Groups: Member

Joined: 12/6/2007
Posts: 345
Location: bkk

การส่งค่าเพื่อควบคุมโปรแกรมตามเราต้องการ
สมมติว่าต้องการคำนวนค่าตัวเลขใน โปรแกรม Calculator ซึ่งจะมีวิธีการ
สองวิธีคือ
 1. เปิดโปรแกรม Calculator ขึ้นมาแล้วก็กดปุ่มตัวเลขต่างๆ เอง
  2. เขียนโปรแกรมสั่งให้โปรแกรมควบคุม Calculator อีกทีโดยที่เราไม่ต้อง
     กดปุ่มตัวเลขบน โปรแกรม Calculator เราแค่สร้าง form ขึ้นมาแล้วมีปุ่ม
   Button แค่ปุ่มเดี่ยวเพื่อสำหรับเปิดปิดโปรแกรม Calculator และสั่งให้ประมวลผลการ
   บวกค่าเพื่อให้ได้ผลลัพธ์ที่ต้องการ

วิธีการ
ถ้าเป็นการสั่งให้เปิด โปรแกรม Calculator ขึ้นมาก็จะใช้คำสั่งของคลาส
Process เพื่อเปิด โปรแกรม
และจะใช้ Sendkey เพื่อเป็นการส่งค่าเข้าไปในโปรแกรมดังนี้

1. สร้าง Window Form ขึ้นมา และลากปุ่ม button มาวาง 1 ปุ่มดับเบิล
    คลิกที่ปุ่มแล้วเขียนโค้ดดังนี้

  Process p = Process.Start("calc.exe");


จะต้องมีการเรียกใช้ using System.Diagnostics; ด้วย
2. ถ้าขั้นตอนที่ 1 ถ้าเราสั่งให้รันดูแล้วคลิกปุ่มจะเห็นว่าสามารถที่จะสั่งให้โปรแกรม
    Calulator เปิดการทำงานได้ แต่เราต้องการสั่งให้โปรแกรมสามารถคำนวนผลได้ก็ให้เขียนโค้ดดังนี้
   สิ่งแรกจะต้องสร้าง เมธอด FindWindow,SetForegroundWindow ก่อนตามโค้ดดังนี้

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr FindWindow(string classname, string windowname);
        [DllImport("user32.dll")]
        public static extern bool SetForegroundWindow(IntPtr p);

3. จากนั้นใน event button1_click ก็เขียนคำสั่งเพื่อเรียกใช้เมธอด Findwindow,SetForegroundWindow
    ดังนี้
  ***ชื่อ classname,windowname ดูได้จาก SPY++ (ดูวิธีการใช้งานตรงได้ที่นี่    http://www.codetoday.net/yaf_postst1136_-SPY.aspx  )

  Process p = Process.Start("calc.exe");
            Thread.Sleep(1000);//รอเวลา ก่อนที่จะสั่งโปรแกรม CalCulator ทำงาน
            IntPtr open_calculator = FindWindow("SciCalc", "Calculator Plus");
            if (open_calculator == IntPtr.Zero)
            {
                MessageBox.Show("This window cannot open");
                return;
            }
            else
            {
                SetForegroundWindow(open_calculator); //set Calculator ให้เป็น window หลักในการรับค่า
                SendKeys.SendWait("3");//ส่งค่าไปที่ โปรแกรม Calculator
                SendKeys.SendWait("*");
                SendKeys.SendWait("5");
                SendKeys.SendWait("=");
            }

ผลลัพธ์จะได้ดังรูป

 

ตัวอย่างโค้ดทั้งหมด

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
namespace WindowsFormsApplication6
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Process p = Process.Start("calc.exe");
            Thread.Sleep(1000);//รอเวลา ก่อนที่จะสั่งโปรแกรม CalCulator ทำงาน
            IntPtr open_calculator = FindWindow("SciCalc", "Calculator Plus");
            if (open_calculator == IntPtr.Zero)
            {
                MessageBox.Show("This window cannot open");
                return;
            }
            else
            {
                SetForegroundWindow(open_calculator); //set Calculator ให้เป็น window หลักในการรับค่า
                SendKeys.SendWait("3");//ส่งค่าไปที่ โปรแกรม Calculator
                SendKeys.SendWait("*");
                SendKeys.SendWait("5");
                SendKeys.SendWait("=");
            }
        }
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr FindWindow(string classname, string windowname);
        [DllImport("user32.dll")]
        public static extern bool SetForegroundWindow(IntPtr p);
    }
}

 

 

 

 

 

 

 



[With great power comes great responsibility]
balungka
Posted: Tuesday, September 30, 2008 11:08:34 AM
Rank: มือฝึกหัด
Groups: Member

Joined: 9/30/2008
Posts: 4
Location: th

ขอถามนิดหนึ่งครับ ถ้าเขียนแบบนี้ สมมุติระหว่างที่สั่งให้เครื่องคิดเลขทำงานอยู่แล้ว เรา สลับไปใช้โปรแกรมอื่น อยากทราบว่า โปรแกรมเครื่องคิดเลขจะยังทำงานตามที่เรา สั่งหรือไม่ครับ

คือที่ผมอยากได้ คือประมาณว่า ถึงจะย่อโปรแกรมเครื่องคิดเลข แล้วไปทำอย่างอื่น โปรแกรมที่เราเขียนก็ยังสามารถ สั่งเครื่องคิดเลขได้นะครับ

ยังงัยก็แนะนำด้วยนะครับ

paedotnet
Posted: Tuesday, September 30, 2008 11:48:24 AM

Rank: มือเทพ
Groups: Member

Joined: 12/6/2007
Posts: 345
Location: bkk

ไม่ได้ครับ แต่ถ้าอยากทำงาน แบบ Multi Window โดยใช้วิธีนี้ก็ต้องสร้างเป็น Thread อะครับกำหนดคลาส Thread ให้แต่ละ Window



[With great power comes great responsibility]
balungka
Posted: Wednesday, October 01, 2008 11:15:14 AM
Rank: มือฝึกหัด
Groups: Member

Joined: 9/30/2008
Posts: 4
Location: th

ขอบคุณครับ _/|\_

(อืมถ้าจะให้ดีน่าจะมีตัวอย่างก็จะดีมากเลยครับท่าน อิอิ ไม่รู้ขอมากไปป่าว )

paedotnet
Posted: Wednesday, October 01, 2008 11:48:07 PM

Rank: มือเทพ
Groups: Member

Joined: 12/6/2007
Posts: 345
Location: bkk

ลองดูนะครับ

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
namespace HookProgramming
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            CalculatorClass c = new CalculatorClass("Calc.exe");
            Thread MThread = new Thread(new ThreadStart(c.RunProcess));
            MThread.Start();
    
        }
      
    }
    public class CalculatorClass
    {
        Process p;
        public CalculatorClass(string process_name)
        {
             p = Process.Start(process_name);
       
        }
        public void RunProcess()
        {
            Thread.Sleep(3000);
            IntPtr open2 = FindWindow("SciCalc", "Calculator Plus");
            if (p.Responding)
            {
             
                if (open2 == IntPtr.Zero)
                {

                }
                else
                {
                    if (SetForegroundWindow(open2))
                    {
                        SendKeys.SendWait("3");
                        SendKeys.SendWait("*");
                        SendKeys.SendWait("5");
                        SendKeys.SendWait("=");
                    }
                    else
                    {
                      
                        SetForegroundWindow(p.MainWindowHandle);
                        SendKeys.SendWait("3");
                        SendKeys.SendWait("*");
                        SendKeys.SendWait("5");
                        SendKeys.SendWait("=");

                     
                    }
                }
            }
            else
            {
            }
          
        }
        [DllImport("USER32.DLL", CharSet = CharSet.Auto)]
        public static extern IntPtr FindWindow(string classname, string windowname);
        [DllImport("USER32.DLL")]
        public static extern bool SetForegroundWindow(IntPtr p);

    }
 
}

แต่ว่ามันยังไม่ค่อย work เท่าไหร่นะครับ ในโค้ดนี้ถ้า รันแล้วจะมี Calculator application เกิดขึ้นมา โดย Calculator นี้จะแสดงผลการคำนวนที่เราใส่เข้าไป ตามเวลา Thread.Sleep ที่เรากำหนดไว้ซึ่งเราสามารถที่จะไปคลิกเลือก application อื่นๆก่อนได้ พอถึง เวลา ที่กำหนดแล้วมันจะเด้งมาหน้า Calculator และแสดงผล



[With great power comes great responsibility]
balungka
Posted: Thursday, October 02, 2008 9:07:34 AM
Rank: มือฝึกหัด
Groups: Member

Joined: 9/30/2008
Posts: 4
Location: th

พอเห็นแสงสว่างรำไรแล้วครับ เดียวจะไปงมต่อครับ  ขอบคุณครับ

Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Main Forum RSS : RSS

YAFVision Theme Created by Jaben Cargman (Tiny Gecko)
Powered by Yet Another Forum.net version 1.9.1.8 (NET v2.0) - 3/29/2008
Copyright © 2003-2008 Yet Another Forum.net. All rights reserved.


Sponsored by