Results 1 to 27 of 27

Thread: Help with Hwnds...

  1. #1

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Help with Hwnds...

    Sorry if the title is misleading, but i couldnt think of a name for it. I have a client called Trillian (like aim+msn+yahoo all in one buddylist)..

    Well, i have this code ATM to find the thing and shrink it

    VB Code:
    1. Option Explicit
    2. Const SWP_HIDEWINDOW = &H80
    3. Const SWP_SHOWWINDOW = &H40
    4. Private Declare Function SetWindowPos Lib "user32" (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) As Long
    5. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    6. Dim tWnd As Long
    7. Dim frmname As String
    8. Private Sub Form_Load()
    9. frmname = "ConsoleWindowClass"
    10. tWnd = FindWindow(frmname, vbNullString) ' Change stuff in quotes so it will shrink it!! i.e. for paint it would be "paint"
    11.     MsgBox tWnd
    12.     If tWnd <> 0 Then
    13.         SetWindowPos tWnd, 0, 0, 0, 0, 0, SWP_HIDEWINDOW
    14.     Else: MsgBox frmname & " not found!"
    15.     End If
    16. End Sub
    17.  
    18. Private Sub Form_Unload(Cancel As Integer)
    19.     SetWindowPos tWnd, 0, 200, 0, 100, 100, SWP_SHOWWINDOW
    20.     MsgBox tWnd
    21. End Sub

    well, this is for shrinking Dos windows and resizing. Im wondering, is it possible to find the windows coordinates before resizing it, so i place it exactly as it was?

  2. #2
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: Help with Hwnds...

    Look for GetWindowRect in MSDN. you can use that.

  3. #3

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Help with Hwnds...

    thanks bud. Almost there i think (at least for now)

    my setwindowpos has 5 variables that go with it:
    hwnd As Long, hWndInsertAfter As Long, x As Long, y As Long, cx As Long, cy As Long, wFlags As Long

    but getwindowrect only uses 4..not sure what to do..
    VB Code:
    1. Option Explicit
    2. Const SWP_HIDEWINDOW = &H80
    3. Const SWP_SHOWWINDOW = &H40
    4. Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
    5. Private Declare Function SetWindowPos Lib "user32" (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) As Long
    6. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    7. Dim tWnd As Long
    8. Dim frmname As String
    9.  
    10. Private Type RECT
    11.         Left As Long
    12.         Top As Long
    13.         Right As Long
    14.         Bottom As Long
    15. End Type
    16. Dim Sizeofwin As RECT
    17.  
    18. Private Sub Form_Load()
    19. frmname = "ConsoleWindowClass"
    20. tWnd = FindWindow(frmname, vbNullString) ' Change stuff in quotes so it will shrink it!! i.e. for paint it would be "paint"
    21.     MsgBox tWnd
    22.     GetWindowRect tWnd, Sizeofwin
    23.     If tWnd <> 0 Then
    24.         SetWindowPos tWnd, 0, 0, 0, 0, 0, SWP_HIDEWINDOW
    25.     Else: MsgBox frmname & " not found!"
    26.     End If
    27. End Sub
    28.  
    29. Private Sub Form_Unload(Cancel As Integer)
    30.     SetWindowPos tWnd, Left, Top, Right, Bottom, , SWP_SHOWWINDOW
    31.     MsgBox tWnd
    32. End Sub

  4. #4
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: Help with Hwnds...

    Your Sizeofwin contains the four coordinates you need:

    (Sizeofwin.Left, Sizeofwin.Top) are the coordinates of the top left corner of the window.
    (Sizeofwin.Bottom, Sizeofwin.Right) are coordinates of the bottom right corner.

    So you use them in pairs. I am not sure what the x, cx, y and cy represent in the SetWindowPos.

  5. #5
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: Help with Hwnds...

    Ahh, here are the explanations:

    Code:
    · X
    Specifies the new position of the left side of the window.
    
    · Y
    Specifies the new position of the top of the window.
    
    · cx
    Specifies the new width of the window, in pixels.
    
    · cy
    Specifies the new height of the window, in pixels.
    So you would have:

    X = Sizeofwin.Left
    Y = Sizeofwin.Top
    CX = Sizeofwin.Right - Sizeofwin.Left
    CY = Sizeofwin.Top - Sizeofwin.Bottom

  6. #6

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Help with Hwnds...

    so what am i doing wrong then?

    VB Code:
    1. Option Explicit
    2. Const SWP_HIDEWINDOW = &H80
    3. Const SWP_SHOWWINDOW = &H40
    4. Private Declare Function GetWindowRect Lib "User32" (ByVal hWnd As Long, lpRect As RECT) As Long
    5. Private Declare Sub SetWindowPos Lib "User32" _
    6.     (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, _
    7.     ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long)
    8. Private Declare Function FindWindow Lib "User32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    9. Dim tWnd As Long
    10. Dim frmname As String
    11.  
    12. Private Type RECT
    13.         Left As Long
    14.         Top As Long
    15.         Right As Long
    16.         Bottom As Long
    17. End Type
    18.  
    19. Private Sub Command1_Click(Index As Integer)
    20. frmname = "ConsoleWindowClass"
    21. tWnd = FindWindow(frmname, vbNullString) ' Change stuff in quotes so it will shrink it!! i.e. for paint it would be "paint"
    22.     MsgBox tWnd
    23.     GetWindowRect tWnd, Sizeofwin    'oLeft = Sizeofwin.Left: oTop = Sizeofwin.Top: oRight = Sizeofwin.Right: oBottom = Sizeofwin.Bottom
    24.     If tWnd <> 0 Then
    25.         SetWindowPos tWnd, 0, 0, 0, 0, 0, SWP_SHOWWINDOW
    26.     Else: MsgBox frmname & " not found!"
    27.     End If
    28. End Sub
    29.  
    30. Private Sub Command2_Click()
    31.   SetWindowPos tWnd, Sizeofwin.Left, Sizeofwin.Top, Sizeofwin.Right - Sizeofwin.Left, Sizeofwin.Top - Sizeofwin.Bottom, Sizeofwin.Top - Sizeofwin.Bottom, SWP_SHOWWINDOW
    32. End Sub

  7. #7
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: Help with Hwnds...

    try this instead (for command2)

    SetWindowPos tWnd, -2, Sizeofwin.Left, Sizeofwin.Top, Sizeofwin.Right - Sizeofwin.Left, Sizeofwin.Top - Sizeofwin.Bottom, SWP_SHOWWINDOW

  8. #8
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: Help with Hwnds...

    or this if the above doesn't work:

    SetWindowPos tWnd, HWND_TOP, Sizeofwin.Left, Sizeofwin.Top, Sizeofwin.Right - Sizeofwin.Left, Sizeofwin.Top - Sizeofwin.Bottom, SWP_SHOWWINDOW

  9. #9

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Help with Hwnds...

    whats hwnd_top const value?

    also, i think this is happening because the values arent being saved. I.E. it keeps the values until i call them again when its minimized. so i tried this:

    VB Code:
    1. Option Explicit
    2. Const SWP_HIDEWINDOW = &H80
    3. Const SWP_SHOWWINDOW = &H40
    4. Private Declare Function GetWindowRect Lib "User32" (ByVal hWnd As Long, lpRect As RECT) As Long
    5. Private Declare Sub SetWindowPos Lib "User32" _
    6.     (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, _
    7.     ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long)
    8. Private Declare Function FindWindow Lib "User32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    9. Dim tWnd As Long
    10. Dim frmname As String
    11.  
    12. Private Type RECT
    13.         Left As Long
    14.         Top As Long
    15.         Right As Long
    16.         Bottom As Long
    17. End Type
    18. Dim sizeofwin As RECT
    19. Dim Oleft As Long, oTop As Long, oRight As Long, oBottom As Long
    20.  
    21. Private Sub Command1_Click(Index As Integer)
    22. frmname = "ConsoleWindowClass"
    23. tWnd = FindWindow(frmname, vbNullString) ' Change stuff in quotes so it will shrink it!! i.e. for paint it would be "paint"
    24.     MsgBox tWnd
    25.     GetWindowRect tWnd, sizeofwin    'oLeft = Sizeofwin.Left: oTop = Sizeofwin.Top: oRight = Sizeofwin.Right: oBottom = Sizeofwin.Bottom
    26.     Oleft = sizeofwin.Left: oTop = sizeofwin.Top: oRight = sizeofwin.Right: oBottom = sizeofwin.Bottom
    27.     If tWnd <> 0 Then
    28.         SetWindowPos tWnd, 0, 0, 0, 0, 0, SWP_SHOWWINDOW
    29.     Else: MsgBox frmname & " not found!"
    30.     End If
    31. End Sub
    32.  
    33. Private Sub Command2_Click()
    34.   SetWindowPos tWnd, -2, Oleft, oTop, oRight - Oleft, oTop - oBottom, SWP_SHOWWINDOW
    35. End Sub
    but it still not working..what should i do next?

  10. #10
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Help with Hwnds...

    Try plugging some real numbers. I don't think this is right, but I'm not sure.
    VB Code:
    1. oRight - Oleft

    I think you'd just specify coordinates instead of subtracting them.

    VB Code:
    1. oTop, oLeft, oBottom, oRight ' May be close

    (Sizeofwin.Left, Sizeofwin.Top) are the coordinates of the top left corner of the window.
    (Sizeofwin.Bottom, Sizeofwin.Right) are coordinates of the bottom right corner.

  11. #11

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Help with Hwnds...

    almost...
    VB Code:
    1. Option Explicit
    2. Const SWP_HIDEWINDOW = &H80
    3. Const SWP_SHOWWINDOW = &H40
    4. Private Declare Function GetWindowRect Lib "User32" (ByVal hWnd As Long, lpRect As RECT) As Long
    5. Private Declare Sub SetWindowPos Lib "User32" _
    6.     (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, _
    7.     ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long)
    8. Private Declare Function FindWindow Lib "User32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    9. Dim tWnd As Long
    10. Dim frmname As String
    11.  
    12. Private Type RECT
    13.         Left As Long
    14.         Top As Long
    15.         Right As Long
    16.         Bottom As Long
    17. End Type
    18. Dim sizeofwin As RECT
    19. Dim Oleft As Long, oTop As Long, oRight As Long, oBottom As Long
    20.  
    21. Private Sub Command1_Click(Index As Integer)
    22. frmname = "ConsoleWindowClass"
    23. tWnd = FindWindow(frmname, vbNullString) ' Change stuff in quotes so it will shrink it!! i.e. for paint it would be "paint"
    24.     MsgBox tWnd
    25.     GetWindowRect tWnd, sizeofwin    'oLeft = Sizeofwin.Left: oTop = Sizeofwin.Top: oRight = Sizeofwin.Right: oBottom = Sizeofwin.Bottom
    26.     Oleft = sizeofwin.Left: oTop = sizeofwin.Top: oRight = sizeofwin.Right: oBottom = sizeofwin.Bottom
    27.     If tWnd <> 0 Then
    28.         SetWindowPos tWnd, 0, 0, 0, 0, 0, SWP_SHOWWINDOW
    29.     Else: MsgBox frmname & " not found!"
    30.     End If
    31. End Sub
    32.  
    33. Private Sub Command2_Click()
    34.   SetWindowPos tWnd, -2, Oleft, oTop, oRight, oBottom, SWP_SHOWWINDOW
    35. End Sub
    its on the screen now, but still not how i left it

  12. #12
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: Help with Hwnds...

    If your window is fixed size, you can hardoce the CX and CY as they are width and height. X and Y represent the position.

    Regarding dglienas post, this is the explanation of the API:

    Code:
    Parameter Information 
    
    · hWnd
    Identifies the window.
    
    · lpRect
    Points to a RECT structure that receives the screen coordinates of the upper-left and lower-right corners of the window.
    as they are coordinates, they have to be subtracted.

  13. #13

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Help with Hwnds...

    VB Code:
    1. Private Sub Command2_Click()
    2.   SetWindowPos tWnd, -2, Oleft, oTop, oRight - Oleft, oBottom - oTop, SWP_SHOWWINDOW
    3. End Sub
    still not right, but what is the -2? its what got it on the screen

  14. #14
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: Help with Hwnds...

    Try comparing real position values of the window with the values that the GetWindowRect returns.

  15. #15
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: Help with Hwnds...

    Also, try this (in command2):

    SetWindowPos tWnd, -2, 0, 0, 0, 0, SWP_SHOWWINDOW

  16. #16

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Help with Hwnds...

    how should i compare them? how do i see its 'real' coordinates?

  17. #17
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: Help with Hwnds...

    -2 is the value of the HWND_TOP constant. It brings it to the top.

  18. #18
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Help with Hwnds...

    Post the values for each of these, and we should be able to see what's going on.

    OLeft=
    oTop=
    oRight=
    OBottom=

  19. #19

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Help with Hwnds...

    okay
    Before minimizing:
    44:58:713:396
    After opening:
    44:58:713:732

    so its this last one thats messing up.

  20. #20
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: Help with Hwnds...

    Did you try this: SetWindowPos tWnd, -2, 0, 0, 0, 0, SWP_SHOWWINDOW

    I think that should leave it unchanged, and automatically open it in the original location.

  21. #21

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Help with Hwnds...

    yes, that places the form with properties of 0,0,0,0
    making it invisible..

  22. #22
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: Help with Hwnds...

    Try using these two lines:

    Call SetWindowPos(tWnd, -1, 0, 0, 0, 0, 3)
    Call SetWindowPos(tWnd, -2, 0, 0, 0, 0, 3)

    They should bring the window to the top, without resizing it.

  23. #23

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Help with Hwnds...

    well, in command1 when i hide it..
    SetWindowPos tWnd, 0, 0, 0, 0, 0, SWP_SHOWWINDOW
    doesnt that make it 0000, so bringing it to the front without resizing will leave nothing.

    let me try it though

  24. #24
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: Help with Hwnds...

    Actually, you are doing it wrong when minimizing. You are setting its size to Zero, instead of minimizing it.

  25. #25
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: Help with Hwnds...

    Tada! Done:

    VB Code:
    1. Option Explicit
    2. Const SWP_HIDEWINDOW = &H80
    3. Const SWP_SHOWWINDOW = &H40
    4. Private Declare Function GetWindowRect Lib "User32" (ByVal hWnd As Long, lpRect As RECT) As Long
    5. Private Declare Sub SetWindowPos Lib "User32" _
    6.     (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, _
    7.     ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long)
    8. Private Declare Function FindWindow Lib "User32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    9. Dim tWnd As Long
    10. Dim frmname As String
    11.  
    12. Private Type RECT
    13.         Left As Long
    14.         Top As Long
    15.         Right As Long
    16.         Bottom As Long
    17. End Type
    18. Dim sizeofwin As RECT
    19. Dim Oleft As Long, oTop As Long, oRight As Long, oBottom As Long
    20.  
    21. Private Sub Command1_Click()
    22. frmname = "Notepad"
    23. tWnd = FindWindow(frmname, vbNullString) ' Change stuff in quotes so it will shrink it!! i.e. for paint it would be "paint"
    24.     MsgBox tWnd
    25.     GetWindowRect tWnd, sizeofwin    'oLeft = Sizeofwin.Left: oTop = Sizeofwin.Top: oRight = Sizeofwin.Right: oBottom = Sizeofwin.Bottom
    26.     Oleft = sizeofwin.Left: oTop = sizeofwin.Top: oRight = sizeofwin.Right: oBottom = sizeofwin.Bottom
    27.     If tWnd <> 0 Then
    28.         SetWindowPos tWnd, -2, Oleft, oTop, oRight - Oleft, oBottom - oTop, SWP_HIDEWINDOW
    29.     Else
    30.         MsgBox frmname & " not found!"
    31.     End If
    32. End Sub
    33.  
    34. Private Sub Command2_Click()
    35.   SetWindowPos tWnd, -1, Oleft, oTop, oRight - Oleft, oBottom - oTop, SWP_SHOWWINDOW
    36.   SetWindowPos tWnd, -2, Oleft, oTop, oRight - Oleft, oBottom - oTop, SWP_SHOWWINDOW
    37.   DoEvents
    38. End Sub

  26. #26

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Help with Hwnds...

    Great!!Thanks alot there..

    SetWindowPos tWnd, -1, Oleft, oTop, oRight - Oleft, oBottom - oTop, SWP_SHOWWINDOW
    SetWindowPos tWnd, -2, Oleft, oTop, oRight - Oleft, oBottom - oTop, SWP_SHOWWINDOW
    why do you have both there? just a mistake?

  27. #27
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: Help with Hwnds...

    Because I'm stupid. That's a dumb way of setting a window on top. The line with -1 turns on the Always on top, and the -2 turns it off, so it just brings it to the top.

    A simple way is to use only one line which just brings the window on top of the ZOrder

    SetWindowPos tWnd, 0, Oleft, oTop, oRight - Oleft, oBottom - oTop, SWP_SHOWWINDOW

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