CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2014
    Posts
    61

    Needing create a Modal Dialog like UAC with a background screenshot

    Based in this example i'm wanting create a Modal Dialog Form inside new desktop created by CreateDesktop api. Until now i'm able to show Modal Dialog Form inside new desktop, but i don't know how create a background window with screenshot for also show inside new desktop together Modal Dialog Form.

    So, someone can help me with this task?

    Thank you to all.

    DlgMain.cs

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Windows.Forms;
    using System.Threading.Tasks;
    
    namespace ScreenLockerDemo
    {
        public partial class DlgMain : Form
        {
            public DlgMain()
            {
                InitializeComponent();
            }
    
            [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),
            }
    
    
    
            private void buttonLockScreen_Click(object sender, EventArgs e)
            {
                // old desktop's handle, obtained by getting the current desktop assigned for this thread
                IntPtr hOldDesktop = GetThreadDesktop(GetCurrentThreadId());
    
                // new desktop's handle, assigned automatically by CreateDesktop
                IntPtr hNewDesktop = CreateDesktop("RandomDesktopName", IntPtr.Zero, IntPtr.Zero, 0, (uint)DESKTOP_ACCESS.GENERIC_ALL, IntPtr.Zero);
    
                // switching to the new desktop
                SwitchDesktop(hNewDesktop);     
    
    
    
                // running on a different thread, this way SetThreadDesktop won't fail
                Task.Factory.StartNew(() =>
                {
                    // assigning the new desktop to this thread - so the Modal Dialog Form will be shown in the new desktop)
                    SetThreadDesktop(hNewDesktop);  
    
                    Background Bkgfrm = new Background();
                    Bkgfrm.Show();                                
    
                    DlgLock DialogLock = new DlgLock();
                    DialogLock.ShowDialog(Bkgfrm);
    
                }).Wait();  // waits for the task to finish
                // end of modal form
    
                // if got here, the form is closed => switch back to the old desktop
                SwitchDesktop(hOldDesktop);
    
                // disposing the secure desktop since it's no longer needed
                CloseDesktop(hNewDesktop);
    
            }
        }
    }
    DlgLock.cs

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace ScreenLockerDemo
    {
        public partial class DlgLock : Form
        {
            public DlgLock()
            {
                InitializeComponent();
            }
    
            private void buttonClose_Click(object sender, EventArgs e)
            {
                Close();
            }
        }
    }
    Background.cs

    Code:
    namespace ScreenLockerDemo
    {
        public partial class Background : Form
        {
            public Background()
            {
                InitializeComponent();
            }
    
            private void print(Bitmap BM, PaintEventArgs e)
            {
                Graphics graphicsObj = e.Graphics;
                graphicsObj.DrawImage(BM, 60, 10);
                // graphicsObj.Dispose();
            }
    
            public void Background_Load(object sender, EventArgs e)
            {
                Paint += new PaintEventHandler(Background_Paint);
            }
    
            private void Background_Paint(object sender, PaintEventArgs e)
            {
                Rectangle rect = Screen.PrimaryScreen.Bounds;
                int color = Screen.PrimaryScreen.BitsPerPixel;
                PixelFormat pf;
                pf = PixelFormat.Format32bppArgb;
                Bitmap BM = new Bitmap(rect.Width, rect.Height, pf);
           //   Bitmap BM = new Bitmap(Image.FromFile(@"C:\Users\NZT48\Desktop\ScreenLockerDemo\bin\Debug\1.jpg"), rect.Width, rect.Height);
    
                Graphics g = Graphics.FromImage(BM);
                g.CopyFromScreen(rect.Left, rect.Top, 0, 0, rect.Size);
                Bitmap bitamp = new Bitmap(BM);
                print(bitamp, e);
            }
    
            private void Background_Shown(object sender, EventArgs e)
            {
               // Paint += new PaintEventHandler(Background_Paint);
            }
        }
    }
    Last edited by FL4SHC0D3R; December 2nd, 2016 at 12:18 PM.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Needing create a Modal Dialog like UAC with a background screenshot

    So you are trying to display a user's desktop within a modal dialog, is that correct?

  3. #3
    Join Date
    Apr 2014
    Posts
    61

    Re: Needing create a Modal Dialog like UAC with a background screenshot

    Quote Originally Posted by Arjay View Post
    So you are trying to display a user's desktop within a modal dialog, is that correct?
    New desktop will have a background window that show a screenshot ( inside this window ) of old desktop. And Modal Dialog Form must be appear as son of background window.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Needing create a Modal Dialog like UAC with a background screenshot

    What's the point of doing this? What problem are you trying to solve?

  5. #5
    Join Date
    Apr 2014
    Posts
    61

    Re: Needing create a Modal Dialog like UAC with a background screenshot

    I edited my code above.
    Now i want get screenshot of old desktop, but only is captured screen of new desktop.
    So now, how get screenshot of old desktop and not of new desktop created?

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Needing create a Modal Dialog like UAC with a background screenshot

    Quote Originally Posted by FL4SHC0D3R View Post
    I edited my code above.
    Now i want get screenshot of old desktop, but only is captured screen of new desktop.
    So now, how get screenshot of old desktop and not of new desktop created?
    As asked earlier... What's the point of doing this? What problem are you trying to solve?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured