欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#?SetWindowPos函數(shù)實例詳解

 更新時間:2024年03月22日 10:46:10   作者:不在同一頻道上的呆子  
在C#中,SetWindowPos函數(shù)用于設(shè)置窗口的位置和大小,這篇文章主要介紹了C#?SetWindowPos函數(shù)實例詳解,本文給大家介紹的非常詳細,需要的朋友可以參考下

在C#中,SetWindowPos函數(shù)用于設(shè)置窗口的位置和大小。

原型:

[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);

參數(shù)說明:

  • hWnd: 要設(shè)置位置和大小的窗口的句柄(handle)。
  • hWndInsertAfter: 確定窗口的 Z 順序,即窗口如何在 Z 順序中放置。通??梢允褂靡韵轮抵唬?ul>
  • IntPtr.Zero: 將窗口放在 Z 順序的頂部。
  • new IntPtr(-1): 將窗口放在所有非頂層窗口的頂部。
  • new IntPtr(-2): 將窗口放在所有窗口的頂部。
  • 其他窗口句柄:將窗口放在指定窗口的頂部。
  • XY: 窗口的新位置的左上角的 X 和 Y 坐標。
  • cxcy: 窗口的新寬度和高度。
  • uFlags: 設(shè)置窗口位置和大小的標志。可以使用以下標志的組合來控制窗口的行為:
    • SWP_NOSIZE: 維持當(dāng)前尺寸(忽略 cx 和 cy 參數(shù))。
    • SWP_NOMOVE: 維持當(dāng)前位置(忽略 X 和 Y 參數(shù))。
    • SWP_NOZORDER: 維持當(dāng)前 Z 順序(忽略 hWndInsertAfter 參數(shù))。
    • SWP_SHOWWINDOW: 如果窗口在調(diào)用時是隱藏的,顯示窗口。
    • SWP_HIDEWINDOW: 隱藏窗口。
    • SWP_NOACTIVATE: 不激活窗口。僅適用于SWP_SHOWWINDOW標志。
    • SWP_DRAWFRAME: 在調(diào)整窗口大小時,重繪窗口邊框(通常與SWP_FRAMECHANGED一起使用)。
    • SWP_NOOWNERZORDER: 不改變擁有者窗口的 Z 順序。
    • SWP_NOSENDCHANGING: 防止窗口接收WM_WINDOWPOSCHANGING消息。
    • SWP_FRAMECHANGED: 使系統(tǒng)發(fā)送WM_NCCALCSIZE消息,即使窗口的尺寸沒有改變。
    • SWP_NOCOPYBITS: 防止窗口重繪。
    • SWP_NOREPOSITION: 不重新定位窗口,即使大小或位置發(fā)生變化。
  • 示例用法:

    1、改變窗口大?。?/p>

    using System;
    using System.Runtime.InteropServices;
    // 定義常量和方法簽名
    public class Win32
    {
        [DllImport("user32.dll")]
        public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
        public static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
        public const uint SWP_SHOWWINDOW = 0x40;
    }
    // 獲取窗口句柄并調(diào)用 SetWindowPos 函數(shù)改變窗口大小
    IntPtr hWnd = // 窗口句柄
    int newX = // 新的 X 坐標
    int newY = // 新的 Y 坐標
    int newWidth = // 新的寬度
    int newHeight = // 新的高度
    Win32.SetWindowPos(hWnd, Win32.HWND_TOPMOST, newX, newY, newWidth, newHeight, Win32.SWP_SHOWWINDOW);

    其中窗口句柄可以使用FindWindow函數(shù)獲取窗口句柄(后面同),如:

    [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        IntPtr hWnd = Win32.FindWindow(null, "窗口標題"); // 替換為窗口的類名或標題

    2、移動到屏幕的左上角:

    using System;
    using System.Runtime.InteropServices;
    // 定義常量和方法簽名
    public class Win32
    {
        public const int SWP_NOSIZE = 0x0001;
        public const int SWP_NOMOVE = 0x0002;
        public const int SWP_NOZORDER = 0x0004;
        public const int SWP_SHOWWINDOW = 0x0040;
        [DllImport("user32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
    }
    // 在代碼中調(diào)用 SetWindowPos 函數(shù)將窗口移動到屏幕左上角
    IntPtr hWnd = // 窗口句柄
    Win32.SetWindowPos(hWnd, IntPtr.Zero, 0, 0, 0, 0, Win32.SWP_NOSIZE | Win32.SWP_SHOWWINDOW);

    3、使其成為Topmost窗口并移動到屏幕的左上角:

    using System;
    using System.Runtime.InteropServices;
    // 定義常量和方法簽名
    public class Win32
    {
        public const int SWP_NOSIZE = 0x0001;
        public const int SWP_NOMOVE = 0x0002;
        public const int SWP_NOZORDER = 0x0004;
        public const int SWP_SHOWWINDOW = 0x0040;
        public const int HWND_TOPMOST = -1;
        public const int HWND_NOTOPMOST = -2;
        [DllImport("user32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
    }
    // 在代碼中調(diào)用 SetWindowPos 函數(shù)將窗口移動到屏幕左上角并設(shè)置為Topmost
    IntPtr hWnd = // 窗口句柄
    Win32.SetWindowPos(hWnd, Win32.HWND_TOPMOST, 0, 0, 0, 0, Win32.SWP_NOSIZE | Win32.SWP_SHOWWINDOW);

    4、顯示窗口:

    using System;
    using System.Runtime.InteropServices;
    // 定義常量和方法簽名
    public class Win32
    {
        public const int SW_SHOWNORMAL = 1;
        [DllImport("user32.dll")]
        public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
    }
    // 獲取窗口句柄并調(diào)用 ShowWindow 函數(shù)顯示窗口
    IntPtr hWnd = // 窗口句柄
    Win32.ShowWindow(hWnd, Win32.SW_SHOWNORMAL);

    5、隱藏窗口:

    using System;
    using System.Runtime.InteropServices;
    // 定義常量和方法簽名
    public class Win32
    {
        public const int SW_HIDE = 0;
        [DllImport("user32.dll")]
        public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
    }
    // 獲取窗口句柄并調(diào)用 ShowWindow 函數(shù)隱藏窗口
    IntPtr hWnd = // 窗口句柄
    Win32.ShowWindow(hWnd, Win32.SW_HIDE);

    到此這篇關(guān)于C# SetWindowPos函數(shù)的文章就介紹到這了,更多相關(guān)C# SetWindowPos函數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

    您可能感興趣的文章:

    相關(guān)文章

    • C#數(shù)據(jù)結(jié)構(gòu)之單鏈表(LinkList)實例詳解

      C#數(shù)據(jù)結(jié)構(gòu)之單鏈表(LinkList)實例詳解

      這篇文章主要介紹了C#數(shù)據(jù)結(jié)構(gòu)之單鏈表(LinkList)實現(xiàn)方法,結(jié)合實例形式較為詳細的分析了單鏈表的原理、定義與C#具體實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
      2015-11-11
    • 用C#操縱IIS(代碼)

      用C#操縱IIS(代碼)

      用C#操縱IIS(代碼)...
      2007-03-03
    • C#打開php鏈接傳參然后接收返回值的關(guān)鍵代碼

      C#打開php鏈接傳參然后接收返回值的關(guān)鍵代碼

      這篇文章主要介紹了C#打開php鏈接傳參然后接收返回值的關(guān)鍵代碼,代碼簡單易懂,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
      2016-08-08
    • c#各種Timer類的區(qū)別與用法介紹

      c#各種Timer類的區(qū)別與用法介紹

      System.Threading.Timer 是一個簡單的輕量計時器,它使用回調(diào)方法并由線程池線程提供服務(wù)。在必須更新用戶界面的情況下,建議不要使用該計時器,因為它的回調(diào)不在用戶界面線程上發(fā)生
      2013-10-10
    • Unity相機移動之屏幕邊緣檢測

      Unity相機移動之屏幕邊緣檢測

      這篇文章主要為大家詳細介紹了Unity相機移動之屏幕邊緣檢測,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
      2020-02-02
    • Unity實現(xiàn)10天簽到系統(tǒng)

      Unity實現(xiàn)10天簽到系統(tǒng)

      這篇文章主要為大家詳細介紹了Unity實現(xiàn)10天簽到系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
      2021-04-04
    • Unity實現(xiàn)打磚塊游戲

      Unity實現(xiàn)打磚塊游戲

      這篇文章主要為大家詳細介紹了Unity實現(xiàn)打磚塊游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
      2022-05-05
    • C#并行庫Parallel類介紹

      C#并行庫Parallel類介紹

      這篇文章介紹了C#并行庫Parallel類,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
      2022-06-06
    • C#中如何限制TextBox控件內(nèi)輸入值的范圍

      C#中如何限制TextBox控件內(nèi)輸入值的范圍

      這篇文章主要介紹了C#中如何限制TextBox控件內(nèi)輸入值的范圍,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
      2023-01-01
    • C#中Socket通信編程的異步實現(xiàn)流程分析

      C#中Socket通信編程的異步實現(xiàn)流程分析

      Socket編程的異步實現(xiàn)是指按照異步過程來實現(xiàn)Socket編程,即在完成了一次調(diào)用后通過狀態(tài)、通知和回調(diào)來告知調(diào)用者,本文給大家介紹C#中Socket通信編程的異步實現(xiàn)流程分析,感興趣的朋友一起看看吧
      2024-12-12

    最新評論