網頁

2011年6月28日 星期二

C# FindWindow MoveWindow

利用FindWindow MoveWindow 與虛擬鍵盤 達到自動搜尋 另 SetWindowPos

改變窗口的大小和位置(关于MoveWindow()或SetWindowPos())

SetWindowPos()函數使用更靈活,多用於只修改控件位置而大小不變或只修改大小而位置不變的情況:
BOOL SetWindowPos(const CWnd* pWndInsertAfter,int x,int y,int cx,int cy,UINT nFlags); 
第一個參數我不會用,一般設為NULL; 
x、y控件位置;cx、cy控件寬度和高度;
nFlags常用取值:
SWP_NOZORDER:忽略第一個參數;
SWP_NOMOVE:忽略x、y,維持位置不變;
SWP_NOSIZE:忽略cx、cy,維持大小不變;
例:
CWnd *pWnd; 
pWnd = GetDlgItem( IDC_BUTTON1 ); //獲取控件指針,IDC_BUTTON1為控件ID號
pWnd->SetWindowPos( NULL,50,80 ,0,0,SWP_NOZORDER | SWP_NOSIZE ); //把按鈕移到窗口的(50,80)處
pWnd = GetDlgItem( IDC_EDIT1 ); 
pWnd->SetWindowPos( NULL,0,0,100,80,SWP_NOZORDER | SWP_NOMOVE ); / /把編輯控件的大小設為(100,80),位置不變
pWnd = GetDlgItem( IDC_EDIT1 ); 
pWnd->SetWindowPos( NULL,0,0,100,80,SWP_NOZORDER ); //編輯控件的大小和位置都改變
namespace WindowMover
{
    using System.Windows.Forms;

    static class Logic
    {
        [DllImport("user32.dll", EntryPoint = "SetWindowPos")]
        public static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);

        public static void Move()
        {
            const short SWP_NOMOVE = 0X2;
            const short SWP_NOSIZE = 1;
            const short SWP_NOZORDER = 0X4;
            const int SWP_SHOWWINDOW = 0x0040;

            Process[] processes = Process.GetProcesses(".");
            foreach (var process in processes)
            {
                var handle = process.MainWindowHandle;
                var form = Control.FromHandle(handle);

                if (form == null) continue;

                SetWindowPos(handle, 0, 0, 0, form.Bounds.Width, form.Bounds.Height, SWP_NOZORDER | SWP_SHOWWINDOW);
            }
        }
    }
}


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.Runtime.InteropServices; //For DllImport

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        // Get a handle to an application window.
        [DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
        public static extern IntPtr FindWindow(string lpClassName,
            string lpWindowName);
        // Activate an application window.
        [DllImport("USER32.DLL")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);

        [DllImport("USER32.DLL")]
        public static extern bool MoveWindow(
            IntPtr hwnd,
            int x,
            int y,
            int nWidth,
            int nHeight,
            bool bRepaint
            );
        
        bool rtvl;
        private void button1_Click(object sender, EventArgs e)
        {
            //移動目前執行視窗到的位置,並改變其寬高為800
            IntPtr firefoxHandle = FindWindow(null, "Google - Mozilla Firefox");
            SetForegroundWindow(firefoxHandle);

            rtvl = MoveWindow(firefoxHandle, 0, 0, 800, 800, true);

            //輸入搜尋字串 與Enter
            SendKeys.Send("Google Apps");
            SendKeys.Send("{ENTER}");

            // Verify that Firefox is a running process.
            if (firefoxHandle == IntPtr.Zero)
            {
                MessageBox.Show("Firefox Google頁面未開啟");
                return;
            }
        }
    }
} 

另: 利用 WebBrowser 達到Facebook自動登入

沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。