NEWS

[C#] Chạy ứng dụng trên Virtual Desktop (màn hình ảo) Winform

[C#] Chạy ứng dụng trên Virtual Desktop (màn hình ảo) Winform
Đăng bởi: Thảo Meo - Lượt xem: 2863 19:04:57, 27/05/2022EBOOK

Xin chào các bạn, bài viết hôm nay mình sẻ hướng dẫn các bạn, cách chạy ứng dụng của mình trên Desktop ảo trong lập trình C#, Winform

[C#] Run Application in Virtual Desktop Winform

Khi các bạn chạy ứng dụng của mình sẻ chạy độc lập trên một màn hình Desktop khác.

virtual_desktop_csharp

Khi chạy ứng dụng ở giao diện này:

Các ứng dụng hiện tại của bạn vd như: zalo sẻ không chụp được màn hình, hoặc không sử dụng được phím PrintScreen, và nhiều chức năng khác.

Giúp ứng dụng tăng bảo mật, chống keylogger...

Source code c# virtual Desktop:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RunAppinVirtualDesktop
{
    internal static class Program
    {
        [DllImport("user32.dll")]
        public static extern IntPtr CreateDesktop(string lpszDesktop, IntPtr lpszDevice, IntPtr pDevmode, int dwFlags, uint dwDesiredAccess, IntPtr lpsa);

        [DllImport("user32.dll")]
        private static extern bool SwitchDesktop(IntPtr hDesktop);

        [DllImport("user32.dll")]
        public static extern bool CloseDesktop(IntPtr handle);

        [DllImport("user32.dll")]
        public static extern bool SetThreadDesktop(IntPtr hDesktop);

        [DllImport("user32.dll")]
        public static extern IntPtr GetThreadDesktop(int dwThreadId);

        [DllImport("kernel32.dll")]
        public static extern int GetCurrentThreadId();

        enum DESKTOP_ACCESS : uint
        {
            DESKTOP_NONE = 0,
            DESKTOP_READOBJECTS = 0x0001,
            DESKTOP_CREATEWINDOW = 0x0002,
            DESKTOP_CREATEMENU = 0x0004,
            DESKTOP_HOOKCONTROL = 0x0008,
            DESKTOP_JOURNALRECORD = 0x0010,
            DESKTOP_JOURNALPLAYBACK = 0x0020,
            DESKTOP_ENUMERATE = 0x0040,
            DESKTOP_WRITEOBJECTS = 0x0080,
            DESKTOP_SWITCHDESKTOP = 0x0100,

            GENERIC_ALL = (DESKTOP_READOBJECTS | DESKTOP_CREATEWINDOW | DESKTOP_CREATEMENU |
                            DESKTOP_HOOKCONTROL | DESKTOP_JOURNALRECORD | DESKTOP_JOURNALPLAYBACK |
                            DESKTOP_ENUMERATE | DESKTOP_WRITEOBJECTS | DESKTOP_SWITCHDESKTOP),
        }
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            IntPtr hOldDesktop = GetThreadDesktop(GetCurrentThreadId());
            IntPtr hNewDesktop = CreateDesktop("RandomDesktopName", IntPtr.Zero, IntPtr.Zero, 0, (uint)DESKTOP_ACCESS.GENERIC_ALL, IntPtr.Zero);

            SwitchDesktop(hNewDesktop);        


            Task.Factory.StartNew(() =>
            {
                SetThreadDesktop(hNewDesktop);

                Application.Run(new Form1());
            }).Wait();

            SwitchDesktop(hOldDesktop);
            CloseDesktop(hNewDesktop);
           
        }
    }
}

Thanks for watching!

DOWNLOAD SOURCE

Tags: virtual desktop c#màn hình ảo windows c#

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Chạy ứng dụng trên Virtual Desktop (màn hình ảo) Winform
Đăng bởi: Thảo Meo - Lượt xem: 2863 19:04:57, 27/05/2022EBOOK