none
CALCULATE IN WINDOWS RRS feed

  • Question

  • Use the following code to show your Windows Calculator
    The code works fine but I make the form of my program at the front (TOP MOST)and the calculator shows up
    Is there a way to make the calculator appear on top of my form

      System.Diagnostics.Process.Start("C:\Windows\system32\calc.exe")

    Friday, April 13, 2018 9:02 PM

Answers

  • Try the following

    Imports System.Runtime.InteropServices
    
    Public Class Form1
        <DllImport("user32.dll")>
        Shared Function SetWindowPos(ByVal hWnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal uFlags As UInteger) As Boolean
        End Function
    
        Private Shared ReadOnly HWND_TOPMOST As New IntPtr(-1)
        Private Const SWP_NOSIZE As UInt32 = &H1
        Private Const SWP_NOMOVE As UInt32 = &H2
        Private Const SWP_SHOWWINDOW As UInt32 = &H40
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim item As Process = Process.Start("C:\Windows\system32\calc.exe")
            SetWindowPos(item.Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE Or SWP_SHOWWINDOW)
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Proposed as answer by IronRazerz Friday, April 13, 2018 9:23 PM
    • Unproposed as answer by IronRazerz Friday, April 13, 2018 9:29 PM
    • Marked as answer by monemas Friday, April 13, 2018 10:06 PM
    Friday, April 13, 2018 9:22 PM
    Moderator
  •  You need to make your application wait until the Calculator window is actually opened and ready before calling the SetWindowPos function.  You must also use the process's MainWindowHandle not it's Handle.  Try changing it as shown here...

    Imports System.Runtime.InteropServices
    
    Public Class Form1
        <DllImport("user32.dll")>
        Shared Function SetWindowPos(ByVal hWnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal uFlags As UInteger) As <MarshalAs(UnmanagedType.Bool)> Boolean
        End Function
    
        Private Shared ReadOnly HWND_TOPMOST As New IntPtr(-1)
        Private Const SWP_NOSIZE As UInt32 = &H1
        Private Const SWP_NOMOVE As UInt32 = &H2
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim CalculatorProcess As Process = Process.Start("calc")
    
            While CalculatorProcess Is Nothing OrElse CalculatorProcess.MainWindowHandle = IntPtr.Zero 'must wait until the calculator window is opened and ready
                System.Threading.Thread.Sleep(200)
            End While
    
            If SetWindowPos(CalculatorProcess.MainWindowHandle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE) Then
                Me.Text = "Calulator is TopMost"
            End If
        End Sub
    End Class
     

     However,  you will need to set your application as NOT TopMost or it will be able to be brought in fron to the Calculator window.


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Friday, April 13, 2018 9:49 PM
    • Marked as answer by monemas Friday, April 13, 2018 10:06 PM
    Friday, April 13, 2018 9:38 PM

All replies

  • Try the following

    Imports System.Runtime.InteropServices
    
    Public Class Form1
        <DllImport("user32.dll")>
        Shared Function SetWindowPos(ByVal hWnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal uFlags As UInteger) As Boolean
        End Function
    
        Private Shared ReadOnly HWND_TOPMOST As New IntPtr(-1)
        Private Const SWP_NOSIZE As UInt32 = &H1
        Private Const SWP_NOMOVE As UInt32 = &H2
        Private Const SWP_SHOWWINDOW As UInt32 = &H40
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim item As Process = Process.Start("C:\Windows\system32\calc.exe")
            SetWindowPos(item.Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE Or SWP_SHOWWINDOW)
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Proposed as answer by IronRazerz Friday, April 13, 2018 9:23 PM
    • Unproposed as answer by IronRazerz Friday, April 13, 2018 9:29 PM
    • Marked as answer by monemas Friday, April 13, 2018 10:06 PM
    Friday, April 13, 2018 9:22 PM
    Moderator
  • Try the following

    Imports System.Runtime.InteropServices
    
    Public Class Form1
        <DllImport("user32.dll")>
        Shared Function SetWindowPos(ByVal hWnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal uFlags As UInteger) As Boolean
        End Function
    
        Private Shared ReadOnly HWND_TOPMOST As New IntPtr(-1)
        Private Const SWP_NOSIZE As UInt32 = &H1
        Private Const SWP_NOMOVE As UInt32 = &H2
        Private Const SWP_SHOWWINDOW As UInt32 = &H40
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim item As Process = Process.Start("C:\Windows\system32\calc.exe")
            SetWindowPos(item.Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE Or SWP_SHOWWINDOW)
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


     NOT WORK
    Friday, April 13, 2018 9:32 PM
  •  You need to make your application wait until the Calculator window is actually opened and ready before calling the SetWindowPos function.  You must also use the process's MainWindowHandle not it's Handle.  Try changing it as shown here...

    Imports System.Runtime.InteropServices
    
    Public Class Form1
        <DllImport("user32.dll")>
        Shared Function SetWindowPos(ByVal hWnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal uFlags As UInteger) As <MarshalAs(UnmanagedType.Bool)> Boolean
        End Function
    
        Private Shared ReadOnly HWND_TOPMOST As New IntPtr(-1)
        Private Const SWP_NOSIZE As UInt32 = &H1
        Private Const SWP_NOMOVE As UInt32 = &H2
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim CalculatorProcess As Process = Process.Start("calc")
    
            While CalculatorProcess Is Nothing OrElse CalculatorProcess.MainWindowHandle = IntPtr.Zero 'must wait until the calculator window is opened and ready
                System.Threading.Thread.Sleep(200)
            End While
    
            If SetWindowPos(CalculatorProcess.MainWindowHandle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE) Then
                Me.Text = "Calulator is TopMost"
            End If
        End Sub
    End Class
     

     However,  you will need to set your application as NOT TopMost or it will be able to be brought in fron to the Calculator window.


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Friday, April 13, 2018 9:49 PM
    • Marked as answer by monemas Friday, April 13, 2018 10:06 PM
    Friday, April 13, 2018 9:38 PM
  •  PS - Also see my reply in your last thread.  You marked the wrong post as the answer in that thread.  Please correct it.  Thanks.

    If you say it can`t be done then i`ll try it

    Friday, April 13, 2018 9:42 PM
  • Try the following

    Imports System.Runtime.InteropServices
    
    Public Class Form1
        <DllImport("user32.dll")>
        Shared Function SetWindowPos(ByVal hWnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal uFlags As UInteger) As Boolean
        End Function
    
        Private Shared ReadOnly HWND_TOPMOST As New IntPtr(-1)
        Private Const SWP_NOSIZE As UInt32 = &H1
        Private Const SWP_NOMOVE As UInt32 = &H2
        Private Const SWP_SHOWWINDOW As UInt32 = &H40
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim item As Process = Process.Start("C:\Windows\system32\calc.exe")
            SetWindowPos(item.Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE Or SWP_SHOWWINDOW)
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


     NOT WORK
    Please do not write in all caps and also it worked on two computers running Win10 which is why I provided this, why it does not work on your machine I have no clue.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, April 13, 2018 9:51 PM
    Moderator
  • Try the following

    Imports System.Runtime.InteropServices
    
    Public Class Form1
        <DllImport("user32.dll")>
        Shared Function SetWindowPos(ByVal hWnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal uFlags As UInteger) As Boolean
        End Function
    
        Private Shared ReadOnly HWND_TOPMOST As New IntPtr(-1)
        Private Const SWP_NOSIZE As UInt32 = &H1
        Private Const SWP_NOMOVE As UInt32 = &H2
        Private Const SWP_SHOWWINDOW As UInt32 = &H40
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim item As Process = Process.Start("C:\Windows\system32\calc.exe")
            SetWindowPos(item.Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE Or SWP_SHOWWINDOW)
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


     NOT WORK

    Please do not write in all caps and also it worked on two computers running Win10 which is why I provided this, why it does not work on your machine I have no clue.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    I'm sorry
    The problem is I put 3 form on top

    Friday, April 13, 2018 10:01 PM