2018年7月3日 星期二

[c#]WindowsForm嵌入CMD功能 小黑窗功能

環境設定windows Form專案 加入元件Panel  元件命名為:pnConsole

標頭新增一個 CommonMethod 的類別 ((檔案內文在文章最下面

========================================================

在主框架中 加入:
public Form1()
{
    IntPtr windowHandle;
    InitializeComponent();
    CommonMethod.AllocConsole();
    windowHandle = CommonMethod.FindWindow(null, System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
    IntPtr closeMenu = CommonMethod.GetSystemMenu(windowHandle, IntPtr.Zero);
    Console.SetWindowPosition(0, 0);
    uint SC_CLOSE = 0xF060;
    CommonMethod.RemoveMenu(closeMenu, SC_CLOSE, 0x0);
    CommonMethod.RemoveMenu(closeMenu, 0x1000000, 0x0);
    CommonMethod.SetParent(windowHandle, pnConsole.Handle);
    CommonMethod.MoveWindow(windowHandle, 0, -27, this.pnConsole.Width, this.pnConsole.Height + 27, true);
    this.componentInitial();
}

========================================================
新增以下檔案
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
 
namespace Example_CS_Reconlib
{
    class CommonMethod
    {
        public const int GWL_STYLE = (-16);
        public const int WS_VISIBLE = 0x10000000;
        /// <summary>
        /// //啟動視窗
        /// </summary>
        /// <returns></returns>
        [DllImport("Kernel32.dll")]
        public static extern bool AllocConsole();
        /// <summary>
        /// /釋放窗口,即關閉
        /// </summary>
        /// <returns></returns>
        [DllImport("kernel32.dll", EntryPoint = "FreeConsole")]
        public static extern bool FreeConsole();
        /// <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);
        /// <summary>
        /// //取出視窗運行的功能表
        /// </summary>
        /// <param name="hWnd"></param>
        /// <param name="bRevert"></param>
        /// <returns></returns>
        [DllImport("user32.dll", EntryPoint = "GetSystemMenu")]
        public extern static IntPtr GetSystemMenu(IntPtr hWnd, IntPtr bRevert);
 
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        public static extern int GetMenuItemCount(IntPtr hMenu);
 
        /// <summary>
        /// 灰掉按鈕
        /// </summary>
        /// <param name="hMenu"></param>
        /// <param name="uPosition"></param>
        /// <param name="uFlags"></param>
        /// <returns></returns>
        [DllImport("user32.dll", EntryPoint = "RemoveMenu")]
        public extern static IntPtr RemoveMenu(IntPtr hMenu, uint uPosition, uint uFlags);
        /// <summary>
        /// 修改標題
        /// </summary>
        /// <param name="strMessage"></param>
        /// <returns></returns>
        [DllImport("Kernel32.dll")]
        public static extern bool SetConsoleTitle(string strMessage);
 
        /// <summary>
        /// 設置控制台(Console)在什麼元件上顯示
        /// </summary>
        /// <param name="hWndChild">Console表單</param>
        /// <param name="hWndNewParent">顯示Console的元件</param>
        /// <returns></returns>
        [DllImport("user32.dll", SetLastError = true)]
        public static extern long SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
 
        [DllImport("user32.dll", SetLastError = true)]
        public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
        /// <summary>
        /// Remove border and whatnot
        /// </summary>
        /// <param name="hwnd"></param>
        /// <param name="nIndex"></param>
        /// <param name="dwNewLong"></param>
        /// <returns></returns>
        [DllImport("user32.dll", EntryPoint = "SetWindowLongA", SetLastError = true)]
        public static extern long SetWindowLong(IntPtr hwnd, int nIndex, long dwNewLong);
        /// <summary>
        /// 改變表單的大小
        /// </summary>
        /// <param name="hwnd"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="cx"></param>
        /// <param name="cy"></param>
        /// <param name="repaint"></param>
        /// <returns></returns>
        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool MoveWindow(IntPtr hwnd, int x, int y, int cx, int cy, bool repaint);
        /// <summary>
        /// 設置字體
        /// </summary>
        /// <param name="hConsoleOutput"></param>
        /// <param name="wAttributes"></param>
        /// <returns></returns>
        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern int SetConsoleTextAttribute(IntPtr hConsoleOutput, int wAttributes);
 
        [DllImport("kernel32.dll")]
        public static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode);
 
    }
}
 

沒有留言:

張貼留言

[SQL]顯示千分位與小數顯示

  CONVERT ( data_type [ ( length ) ] , expression [ , style ] ) CONVERT style參數說明 1  (expression為 money 或 smallmoney型別): 0 : 預設,保留小數位後兩位,並四捨...