【WindowsAPI之MoveWindow】 C#調整目標表單的位置、大小

來源:互聯網
上載者:User

首先查看一下WindowsAPI給我們的解釋

 函數功能:該函數改變指定視窗的位置和尺寸。對於頂層視窗,位置和尺寸是相對於螢幕的左上方的:對於子視窗,位置和尺寸是相對於父視窗客戶區的左上方座標的。

函數原型:bool MoveWindow(HWND hWnd,int x,int y,int nWidth,int nHeight,bool BRePaint);

 

參數:

hWnd:視窗控制代碼。

x:指定視窗的新位置的左邊界。

Y:指定視窗的新位置的頂部邊界。

nWidth:指定視窗的新的寬度。

nHaight:指定視窗的新的高度。

所在位置:user32.dll

 

需要命名空間 

 

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

 

 

首先是擷取目標表單的控制代碼(什麼是控制代碼?我不知道,但是百度知道 ^_^)

 

        /// <summary>
        /// 擷取表單控制代碼
        /// </summary>
        /// <param name="lpClassName"></param>
        /// <param name="lpWindowName"></param>
        /// <returns></returns>
        [DllImport("User32.dll", EntryPoint = "FindWindow")]
        public extern static IntPtr FindWindow(string lpClassName, string lpWindowName);

 

 

然後就是我們的主角登場啦(MoveWindow)

        /// <summary>
        /// 設定目標表單大小,位置
        /// </summary>
        /// <param name="hWnd">目標控制代碼</param>
        /// <param name="x">目標表單新位置X軸座標</param>
        /// <param name="y">目標表單新位置Y軸座標</param>
        /// <param name="nWidth">目標表單新寬度</param>
        /// <param name="nHeight">目標表單新高度</param>
        /// <param name="BRePaint">是否重新整理表單</param>
        /// <returns></returns>
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int MoveWindow(IntPtr hWnd, int x, int y, int nWidth, int nHeight, bool BRePaint);

 

這裡說一下,API裡面第一個參數寫的類型是HWND ,到C#裡面就要用IntPtr 類表示,指標呢,據說是用委託實現(我也剛接觸,沒試過)

 

有了這兩段代碼,就已經完成一半了

設計表單是這個樣子滴。。

在按鈕的Click事件裡添加如下代碼

            IntPtr intptr = FindWindow(null, "新編WIN32API大全");

            int x = this.GetInt(txtP_x.Text.Trim(), 0);
            int y = this.GetInt(txtP_y.Text.Trim(), 0);
            int nWidth = this.GetInt(txtNWidth.Text.Trim(), 0);
            int nHeight = this.GetInt(txtNHeight.Text.Trim(), 0);

            //調用API
            MoveWindow(intptr, x, y, nWidth, nHeight, true);


GetInt方法是簡單處理一下非數位字串


        /// <summary>
        /// 將字string轉成int類型
        /// </summary>
        /// <param name="value">要轉換的字串</param>
        /// <param name="_default">預設值</param>
        /// <returns></returns>
        public int GetInt(string value, int _default)
        {
            if (int.TryParse(value, out _default))
                return Convert.ToInt32(value);
            else
                return _default;
        }

 

大功告成!! 運行一下試試吧。哦對了,忘了說了,改一下控制代碼名稱再試哦~  可以改成QQ2012之類的,我試過了,可以的。哈哈

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.