none
How to amend this code to maximized my minimized (to toolbar) app's screen(which it has a picturebox1) when user finishes capturing active screen which will be displayed the captured image in picturebox1 in my app's screen?. RRS feed

  • Question

  • Hi all,

    I am using the following code to let user of my app to use hotkeys to capture the active screen 

    This is the main part of it 

    Protected Overrides Sub WndProc(ByRef m As Message)
            MyBase.WndProc(m)
            Const WM_HOTKEY = &H312
            If m.Msg = WM_HOTKEY AndAlso m.WParam = nAtom Then
                Try
                    Dim r As New RECT
                    Dim hWndForeground As IntPtr = GetForegroundWindow()
                    GetWindowRect(hWndForeground, r)
                    Dim img As New Bitmap(r.Right - r.Left, r.Bottom - r.Top)
                    Dim gr As Graphics = Graphics.FromImage(img)
                    gr.CopyFromScreen(New Point(r.Left, r.Top), Point.Empty, img.Size)
                    Me.Show()
                    source = img
                    PictureBox1.Image = source
                          Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End If

    Now anyone help me to know how to amend this code to maximized my minimized (to toolbar) app's screen(which it has a picturebox1) when user finishes capturing active screen using hotkey in the above code which will be displayed the captured image in picturebox1 in my app's screen?.

    This is the whole code of my app

    Imports System.Net.Mail
    Imports Microsoft.Win32
    Imports System.Runtime.InteropServices
    Public Class Form1
        <DllImport("User32", ExactSpelling:=True, CharSet:=CharSet.Auto)> Public Shared Function GetForegroundWindow() As IntPtr
        End Function
        <DllImport("Kernel32", CharSet:=CharSet.Unicode, SetLastError:=True)> Public Shared Function GlobalAddAtom(ByVal atomName As String) As Short
        End Function
        <DllImport("User32", CharSet:=CharSet.Auto, SetLastError:=True)> Public Shared Function RegisterHotKey(ByVal hWnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Integer) As Boolean
        End Function
        <DllImport("User32", CharSet:=CharSet.Auto, SetLastError:=True)> Public Shared Function UnregisterHotKey(ByVal hWnd As IntPtr, ByVal id As Integer) As Boolean
        End Function
        <StructLayout(LayoutKind.Sequential)> Public Structure RECT
            Public Sub New(ByVal left As Integer, ByVal top As Integer, ByVal right As Integer, ByVal bottom As Integer)
                Me.Left = left
                Me.Top = top
                Me.Right = right
                Me.Bottom = bottom
            End Sub
            Public Left As Integer
            Public Top As Integer
            Public Right As Integer
            Public Bottom As Integer
        End Structure
        <DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> Public Shared Function GetWindowRect(ByVal hWnd As IntPtr, <[In](), Out()> ByRef rect As RECT) As Boolean
        End Function
        Public Const MOD_ALT = &H1
        Public Const MOD_CONTROL = &H2
        Public Const MOD_SHIFT = &H4
        Public Const MOD_WIN = &H8
        Public Const MOD_NOREPEAT = &H4000
        Public Const WM_HOTKEY = &H312
        Private nAtom As Short
        Protected Overrides Sub WndProc(ByRef m As Message)
            MyBase.WndProc(m)
            Const WM_HOTKEY = &H312
            If m.Msg = WM_HOTKEY AndAlso m.WParam = nAtom Then
                Try
                    Dim r As New RECT
                    Dim hWndForeground As IntPtr = GetForegroundWindow()
                    GetWindowRect(hWndForeground, r)
                    Dim img As New Bitmap(r.Right - r.Left, r.Bottom - r.Top)
                    Dim gr As Graphics = Graphics.FromImage(img)
                    gr.CopyFromScreen(New Point(r.Left, r.Top), Point.Empty, img.Size)
                    source = img
                    PictureBox1.Image = source
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End If
        End Sub
        Private SelectSiz As Size
        Private SelectPos As System.Drawing.Point
        Private Selecting As Boolean = False
        Private Selected As Boolean = False
        Private source As Image
        Dim XOffs As Integer
        Dim YOffs As Integer
        Dim filename As String = String.Format("Image{0:yyyyMMddHHmmss}.png", DateTime.Now)
        Dim filePath = (IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), (filename)))
        Private Function cc() As Bitmap
            Dim s As Screen = Screen.PrimaryScreen
            Dim img As New Bitmap(s.Bounds.Width, s.Bounds.Height)
            Dim gr As Graphics = Graphics.FromImage(img)
            gr.CopyFromScreen(s.Bounds.Location, Point.Empty, s.Bounds.Size)
            Return img
        End Function
        Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As FormClosedEventArgs) Handles MyBase.FormClosed
            UnregisterHotKey(Handle, nAtom)
        End Sub
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            nAtom = GlobalAddAtom("HotKey")
            RegisterHotKey(Handle, nAtom, MOD_ALT, Keys.R) 
        End Sub
        Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseDown
            Selecting = True
            SelectPos = New Point(e.Location)
            SelectSiz = New Size(1, 1)
            Me.Refresh()
        End Sub
        Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseMove
            If Selecting Then
                SelectSiz = New Size(e.Location.X - SelectPos.X, e.Location.Y - SelectPos.Y)
                Selected = True
                Me.Refresh()
            End If
        End Sub
        Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseUp
            Selecting = False
        End Sub
        Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles PictureBox1.Paint
            If Selected Then
                e.Graphics.DrawRectangle(Pens.Black, GetSelection(SelectPos, SelectSiz))
            End If
        End Sub
        Public Function GetSelection(ByVal Start As Point, ByVal Range As Size) As Rectangle
            Dim X As Integer = Start.X
            Dim X1 As Integer = Range.Width
            If X1 < 0 Then
                X1 = Math.Abs(X1)
                X -= X1
            End If
            Dim Y As Integer = Start.Y
            Dim Y1 As Integer = Range.Height
            If Y1 < 0 Then
                Y1 = Math.Abs(Y1)
                Y -= Y1
            End If
            Return New Rectangle(X, Y, X1, Y1)
        End Function
        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            Try
                PictureBox1.Image = source
                Selected = False  
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Sub
        Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
            Try
                Dim img As Image = Me.PictureBox1.Image
                img.RotateFlip(RotateFlipType.Rotate90FlipNone)
                Me.PictureBox1.Image = img
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Sub
     End Class

      Thank advance..






    • Edited by Max45-1 Friday, August 4, 2017 1:32 AM
    Thursday, August 3, 2017 1:54 AM

Answers


  • Me.WindowState = System.Windows.Forms.FormWindowState.Maximized
    Me.Activate()

    should work.

    • Edited by Castorix31 Friday, August 4, 2017 2:16 AM
    • Marked as answer by Max45-1 Friday, August 4, 2017 3:58 PM
    Friday, August 4, 2017 2:15 AM

All replies

  • Hi omanxp45-2,

    According to your description, you want to capture active screen and pop up the screenshot, then you can modify this, am I right?

    If yes, please refer to the following code, button1-click is to capture active screen, button2-click is to save the image and use Paint tool to open image.

     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim area As Rectangle
            Dim capture As System.Drawing.Bitmap
            Dim graph As Graphics
            capture = New System.Drawing.Bitmap(area.Width, area.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
            graph = Graphics.FromImage(capture)
            graph.CopyFromScreen(area.X, area.Y, 0, 0, area.Size, CopyPixelOperation.SourceCopy)
            PictureBox1.Image = capture
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Dim save As New SaveFileDialog
            Try
                save.Title = "Save File"
                save.FileName = "Screenshot"
                save.Filter = "Png |*.Png"
                If save.ShowDialog() = DialogResult.OK Then
                    PictureBox1.Image.Save(save.FileName, System.Drawing.Imaging.ImageFormat.Png)
                    Dim filepath As String
                    filepath = "D:\test\Screenshot.png"
                    Process.Start("mspaint.exe ", filepath)
                End If
            Catch ex As Exception
            End Try
        End Sub

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, August 3, 2017 7:29 AM
    Moderator
  • Hi omanxp45-2,

    According to your description, you want to capture active screen and pop up the screenshot, then you can modify this, am I right?


    Hi 

    Cherry Bu

    No sorry

    see my edit



    • Edited by Max45-1 Thursday, August 3, 2017 1:59 PM
    Thursday, August 3, 2017 1:59 PM
  • A simple

    Me.Activate()

    doesn't work ?

    Thursday, August 3, 2017 2:26 PM
  • A simple

    Me.Activate()

    doesn't work ?

    How to maximized my minimized (to toolbar) app's screen(which it has a picturebox1) when user finishes capturing active screen which will displaying the captured image in picturebox1 in my app's screen?.


    Friday, August 4, 2017 1:29 AM
  • A simple

    Me.Activate()

    What happened when you tried Me.Acticate?  What happened when you tried Me.WindowState = System.Windows.Forms.FormWindowState.Maximized? 

    You should show exactly what you tried and you should describe the exact result.   Just telling people to look at your edit is pointless if you haven't made it clear what you have changed. 

    Friday, August 4, 2017 2:03 AM

  • Me.WindowState = System.Windows.Forms.FormWindowState.Maximized
    Me.Activate()

    should work.

    • Edited by Castorix31 Friday, August 4, 2017 2:16 AM
    • Marked as answer by Max45-1 Friday, August 4, 2017 3:58 PM
    Friday, August 4, 2017 2:15 AM

  • Me.WindowState = System.Windows.Forms.FormWindowState.Maximized
    Me.Activate()

    should work.

    It works,

    But is it necessary to use "Me.Activate()"?

    Protected Overrides Sub WndProc(ByRef m As Message) MyBase.WndProc(m) Const WM_HOTKEY = &H312 If m.Msg = WM_HOTKEY AndAlso m.WParam = nAtom Then Try Dim r As New RECT Dim hWndForeground As IntPtr = GetForegroundWindow() GetWindowRect(hWndForeground, r) Dim img As New Bitmap(r.Right - r.Left, r.Bottom - r.Top) Dim gr As Graphics = Graphics.FromImage(img) gr.CopyFromScreen(New Point(r.Left, r.Top), Point.Empty, img.Size)

    Me.WindowState = FormWindowState.Normal source = img PictureBox1.Image = source Catch ex As Exception MsgBox(ex.Message) End Try End If End Sub



    • Edited by Max45-1 Friday, August 4, 2017 3:59 PM
    Friday, August 4, 2017 3:46 PM
  • But is it necessary to use "Me.Activate()"?

    On my OS (Windows 10 1703 15063.483 ), it doesn't work correctly without

    (it works once then it stops working...)

    Friday, August 4, 2017 4:08 PM