14/09/2024

Cách gọi Console từ ứng dụng AutoCAD | How to call Console form Winforms or WPF app

Ứng dụng được phát triển bởi đội ngũ AutoLISP Thật là đơn giản
   

Thông tin thêm: 👉👉👉

Ứng dụng Winforms/WPF/Class Library

Thì việc gọi và tương tác với Console sẽ như sau:


1 Khai báo hàm DllImport để sử dụng chức năng DllImport

(Copy nội dung sau)
Code:
        [DllImport("kernel32.dll")]
        private static extern void FreeConsole();

        [DllImport("kernel32.dll")]
        private static extern bool AttachConsole(int dwProcessId);

        private const int ATTACH_PARENT_PROCESS = -1;

        [DllImport("kernel32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool AllocConsole();


2 Chỉnh sửa tệp tin MyCommands.cs

(Copy nội dung sau)
Code:
// (C) Copyright 2024 by
// from www.lisp.vn
using Autodesk.AutoCAD.Runtime;
using System;
using System.Runtime.InteropServices;

[assembly: CommandClass(typeof(AJS_Console.MyCommands))]

namespace AJS_Console
{
    public class MyCommands
    {
        [DllImport("kernel32.dll")]
        private static extern void FreeConsole();

        [DllImport("kernel32.dll")]
        private static extern bool AttachConsole(int dwProcessId);

        private const int ATTACH_PARENT_PROCESS = -1;

        [DllImport("kernel32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool AllocConsole();

        [CommandMethod("AJSGetConsole", CommandFlags.Modal)]
        public void cmd_AJSGetConsole()
        {
            if (!AttachConsole(ATTACH_PARENT_PROCESS))
                AllocConsole();
        }

        [CommandMethod("AJSPrintSomeThing", CommandFlags.Modal)]
        public void cmd_AJSPrintSomeThing_To_Console()
        {
            Console.InputEncoding = System.Text.Encoding.UTF8;
            Console.OutputEncoding = System.Text.Encoding.UTF8;
            Console.WriteLine("---------------------");
            Console.WriteLine("Current times is: " + DateTime.Now.ToUniversalTime());
            Console.WriteLine("Nhập chuỗi ký tự: ");
            var str = Console.ReadLine();
            Console.WriteLine("Chuỗi ký tự nhập: " + str);
            Console.WriteLine("PrintSomeThing Done");
            Console.WriteLine("By www.lisp.vn");
            Console.WriteLine("---------------------");
        }
    }
}


Link Github


Nguồn tham khảo


---------------------------------------------------------------------------------------------
Ứng dụng được phát triển bởi đội ngũ AutoLISP Thật là đơn giản - Tác giả ứng dụng in D2P

    

Mọi thông tin xin liên hệ Fanpage AutoLISP Thật là đơn giản!
Cảm ơn bạn đã theo dõi!

Không có nhận xét nào:

Đăng nhận xét

Hàm COUNTIFS đếm theo nhiều điều kiện trong Excel | COUNTIFS, COUNTIF | AutoLISP Reviewer

Hàm COUNTIFS đếm theo nhiều điều kiện trong Excel Cuối tháng cần biết phòng Kế toán có bao nhiêu người đi làm đủ công, bao nhiêu đơn hàng ...