CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 26 of 26
  1. #16
    Join Date
    Oct 2009
    Posts
    16

    Re: Resize 3rd Party Application

    Code:
    Public Class Form1
        Const LWA_COLORKEY As Int32 = 1
        Const LWA_ALPHA As Int32 = 2
        Const GWL_EXSTYLE As Int32 = (-20)
        Const WS_EX_LAYERED As Int32 = 524288
        Private Declare Function apiGetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Int32, ByVal nIndex As Int32) As Int32
        Private Declare Function apiSetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Int32, ByVal nIndex As Int32, ByVal dwNewLong As Int32) As Int32
        Private Declare Function apiSetLayeredWindowAttributes Lib "user32" Alias "SetLayeredWindowAttributes" (ByVal hWnd As Int32, ByVal crKey As Int32, ByVal bAlpha As Byte, ByVal dwFlags As Int32) As Int32
        Dim nhwnd As Int32
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim p As New System.Diagnostics.Process
            p = Process.Start("notepad")
            p.WaitForInputIdle()
            nhwnd = p.MainWindowHandle.ToInt32
            TrackBar1.Maximum = 255
            TrackBar1.Value = 100
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Transparency(nhwnd, 100) 'Set transparency to 100, or anywhere between 0-255.  Use a trackbar if you want.
        End Sub
    
        Private Sub Transparency(ByVal hwnd As Int32, ByVal sOpacity As Int32)
            Dim Ret As Int32 = apiGetWindowLong(hwnd, GWL_EXSTYLE)  'Set the window style to 'Layered'
            Ret = Ret Or WS_EX_LAYERED
            apiSetWindowLong(hwnd, GWL_EXSTYLE, Ret)
            apiSetLayeredWindowAttributes(hwnd, 0, sOpacity, LWA_ALPHA) 'Set the opacity of the layered window
        End Sub
    
        Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
            Transparency(nhwnd, TrackBar1.Value)
        End Sub
    End Class
    first of all im sorry for bringing up an old topic
    im new at vb and rly dont know how to do this with an already started application
    could somebody rewrite it to make it work with and already started application?

  2. #17
    Join Date
    Jun 2004
    Location
    NH
    Posts
    678

    Re: Resize 3rd Party Application

    Code:
    Public Class Form1
        Const LWA_COLORKEY As Int32 = 1
        Const LWA_ALPHA As Int32 = 2
        Const GWL_EXSTYLE As Int32 = (-20)
        Const WS_EX_LAYERED As Int32 = 524288
        Private Declare Function apiGetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Int32, ByVal nIndex As Int32) As Int32
        Private Declare Function apiSetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Int32, ByVal nIndex As Int32, ByVal dwNewLong As Int32) As Int32
        Private Declare Function apiSetLayeredWindowAttributes Lib "user32" Alias "SetLayeredWindowAttributes" (ByVal hWnd As Int32, ByVal crKey As Int32, ByVal bAlpha As Byte, ByVal dwFlags As Int32) As Int32
       Private Declare Function apiFindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Int32
    
        Dim nhwnd As Int32 = 0 'Handle of window
    
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            TrackBar1.Maximum = 255
            TrackBar1.Value = 100
    
    
            nhwnd = Me.Handle.ToInt32 'Set handle to this application
    
           'Or uncomment, and insert any application title below
           'nhwnd = apiFindWindow(Nothing, "Application Title Here")
    
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Transparency(nhwnd, 100) 'Set transparency to 100, or anywhere between 0-255.  Use a trackbar if you want.
        End Sub
    
        Private Sub Transparency(ByVal hwnd As Int32, ByVal sOpacity As Int32)
            Dim Ret As Int32 = apiGetWindowLong(hwnd, GWL_EXSTYLE)  'Set the window style to 'Layered'
            Ret = Ret Or WS_EX_LAYERED
            apiSetWindowLong(hwnd, GWL_EXSTYLE, Ret)
            apiSetLayeredWindowAttributes(hwnd, 0, sOpacity, LWA_ALPHA) 'Set the opacity of the layered window
        End Sub
    
        Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
            Transparency(nhwnd, TrackBar1.Value)
        End Sub
    End Class

  3. #18
    Join Date
    Oct 2009
    Posts
    16

    Re: Resize 3rd Party Application

    Thanks!
    1 more question
    can i pick the window by process name instead of form name?

  4. #19
    Join Date
    Jun 2004
    Location
    NH
    Posts
    678

    Re: Resize 3rd Party Application

    Code:
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            nhwnd = HandleFromProcessName("Running process name here")
            TrackBar1.Maximum = 255
            TrackBar1.Value = 100
        End Sub
    
        Private Function HandleFromProcessName(ByVal pName As String) As Int32
            For Each p As Process In Process.GetProcesses
                If p.ProcessName = pName Then HandleFromProcessName = p.MainWindowHandle.ToInt32
            Next
        End Function

  5. #20
    Join Date
    Oct 2009
    Posts
    16

    Re: Resize 3rd Party Application

    hehe tyvm again
    and is there any other way to make it trasparent but without using apis?

  6. #21
    Join Date
    Jun 2004
    Location
    NH
    Posts
    678

    Re: Resize 3rd Party Application

    I don't think so.
    If there is a way in .NET, then it's not very well known.

  7. #22
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Resize 3rd Party Application

    There is no way IMHO, that there is a non - API solution to making a third party window transparant.

  8. #23
    Join Date
    Oct 2009
    Posts
    16

    Re: Resize 3rd Party Application

    as i tought, anyway thanks for helping me out
    P.S. doesnt work with every application

  9. #24
    Join Date
    Jun 2004
    Location
    NH
    Posts
    678

    Re: Resize 3rd Party Application

    P.S. doesnt work with every application
    I'm sure it doesn't.
    However, there are several possible reasons.

    A. The application is doing a custom paint operation, or it's non regular window form.
    Not much you can do about that without going way overboard.

    B. The process loop is not obtaining the correct main window handle(or common zero .net bug).
    In which case you have to use a bunch more API for getting the process information.
    It would be more reliable, but still not failsafe.
    However, you would get almost all windows covered.
    I use API for this usually.

    C. You've simply used it wrong, and the handle is zero.

    D. Your reply may have meant, that not all applications(processes) with the same name become transparent.
    If so, you can extend that so that all processes with the same name get the transparency applied.

    E. Other


    _
    Last edited by TT(n); October 10th, 2009 at 06:38 AM.

  10. #25
    Join Date
    Oct 2009
    Posts
    16

    Re: Resize 3rd Party Application

    Quote Originally Posted by TT(n) View Post
    A. The application is doing a custom paint operation
    this is the reason why it doesnt work with the application which i want to make trasparent

    also noticed that it doesnt work well with the applications which have a 3D render field like 3DS max or 3D games

  11. #26
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Resize 3rd Party Application

    DirectX draws directly into memory on the GPU, not the screen buffer area. It gets to the screen buffer automagically. You can't intercept that
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

Page 2 of 2 FirstFirst 12

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