.Net code in Purebasic?

Just starting out? Need help? Post your questions and find answers here.
stmdbe2019
User
User
Posts: 89
Joined: Mon Aug 31, 2009 2:11 pm

.Net code in Purebasic?

Post by stmdbe2019 »

How to write this following code exactly in PureBasic?

1 - My form1 is full screen on top (behind my form1 all other applications running, none can take on top place except my application)
2 - Just behind my window i have Google chrome
3 - now if i close my window i land on Google chrome without need to find Google chrome out of all other windows.

Code: Select all

Public Class Form1
  Declare Auto Function FindWindow Lib "User32.dll" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
  Declare Auto Function SetForegroundWindow Lib "User32.dll" (ByVal Hwnd As IntPtr) As Long
  'Private Declare Function SetForegroundWindow Lib "user32.dll" (ByVal hwnd As Int32) As Int32
  Declare Auto Function FindWindowEx Lib "User32.dll" (ByVal hwndParent As IntPtr, ByVal hwndChildAfter As IntPtr, ByVal lpszClass As String, ByVal lpszWindow As String) As IntPtr
  'Declare Auto Function SetWindowPos Lib "User32.dll" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long)  
  Declare Auto Function SetWindowPos Lib "User32.dll" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long)
  Const HWND_TOPMOST = -1
  Const HWND_NOTOPMOST = -2
  Const SWP_NOSIZE = &H1
  Const SWP_NOMOVE = &H2
  Const SWP_NOACTIVATE = &H10
  Const SWP_SHOWWINDOW = &H40

  Declare Auto Function ShowWindow Lib "User32.dll" (handle As IntPtr, nCmdShow As Integer) As Boolean
  Declare Auto Function IsIconic Lib "User32.dll" (handle As IntPtr) As Boolean

  ' Method 1
  Private Sub StartOrShowProcess(ByVal strProcessName As String)
    Try
      Dim handle As IntPtr
      Dim proc As Process() = Process.GetProcessesByName(strProcessName)
      If proc.Count > 0 Then
        For Each procP As Process In proc
          handle = procP.MainWindowHandle
          ' Do we have handle and minimized or not minimized?
          If handle <> 0 Then
            ShowWindow(handle, 3)
            SetForegroundWindow(handle)
          End If

        Next
      Else 'Not running or started...
        Process.Start(strProcessName)
      End If

    Catch ex As Exception
      'Handle your error...
    End Try
  End Sub

  Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Me.TopMost = True
    StartOrShowProcess("chrome")
  End Sub

  Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
    Me.Hide()
    End
  End Sub


End Class

-----
Registered PureBasic Coder.
User avatar
RSBasic
Moderator
Moderator
Posts: 1218
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: .Net code in Purebasic?

Post by RSBasic »

Untested:

Code: Select all

EnableExplicit

Procedure GetPID(Name.s)
  Protected result.l, Snapshot.l, ProcessFound.l, PN$, Process.PROCESSENTRY32
  Process\dwSize = SizeOf(PROCESSENTRY32)
  Snapshot = CreateToolhelp32Snapshot_(#TH32CS_SNAPPROCESS, 0)
  If Snapshot
    ProcessFound = Process32First_(Snapshot, Process)
    While ProcessFound
      PN$ = UCase(PeekS(@Process\szExeFile, #PB_Any, #PB_Unicode))
      Debug Process\th32ProcessID
      If UCase(Name) = GetFilePart(PN$) : result = #True : Break : EndIf
      ProcessFound = Process32Next_(Snapshot, Process)
    Wend
    CloseHandle_(Snapshot)
  EndIf
  ProcedureReturn Process\th32ProcessID
EndProcedure

; Method 1
Procedure StartOrShowProcess(strProcessName.s)
  Protected handle
  Protected PID
  Protected hwnd
  
  PID = GetPID(strProcessName)
  If PID
    handle = FindWindow_("Chrome_WidgetWin_1", 0)
    ; Do we have handle and minimized or not minimized?
    If handle <> 0
        ShowWindow_(handle, 3)
        SetForegroundWindow_(handle)
        SetWindowPos_(handle, #HWND_TOPMOST, 0, 0, 0, 0, #SWP_NOMOVE | #SWP_NOSIZE)
      EndIf
  Else ;Not running or started...
    RunProgram(strProcessName)
  EndIf
  
EndProcedure

Procedure Form1_Load()
  SetWindowPos_(WindowID(#YOURWINDOWID), #HWND_TOPMOST, 0, 0, 0, 0, #SWP_NOMOVE | #SWP_NOSIZE)
  StartOrShowProcess("chrome.exe")
EndProcedure

Procedure Label1_Click()
  End
EndProcedure

Image
Image
Post Reply