Showing posts with label VB. Show all posts
Showing posts with label VB. Show all posts

Saturday, January 13, 2007

Making any Window Transparent on XP

Intro
This little piece of code written in VB6 allows you to make any window transparent.
Make Firefox, Opera, Internet Explorer, Windows Media Player, Google Talk, Yahoo Messenger, Windows Explorer, basically any window, transparent on Windows XP.

*Transparent Opera

Download
+Zipped binary for Windows XP from here Transp.ZIP [5 KB]
+Source code from planet-source-code

Usage


  • Run the Program

  • Place the mouse over the title bar of the window which you want to make transparent.

  • Now leaving the mouse in the title bar, press the Ctrl button (Normally on the lower right side of the keyboard), now you can see the window name shown in the software.

  • Now press the "Set Transparent" Button

  • Adjust the Transparency Slider to the required transparency. (Dont set below 100 you will loose sight of the window)

  • Press the "Set Opaque" Button to make the window opaque again

  • To do for another window repeat the same steps.



Source

Const GWL_EXSTYLE = -20
Const WS_EX_LAYERED = &H80000
Const GWL_STYLE = (-16)
Const WS_VISIBLE = &H10000000
Const LWA_ALPHA = &H2

Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Option Explicit

Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function SetLayeredWindowAttributes Lib "user32" (ByVal hwnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long


'@ http://digitalpbk.blogspot.com/2007/01/making-any-window-transparent-on-xp.html


Private Type POINTAPI
x As Long
y As Long
End Type

Public xhwnd As Long

Private Sub Command1_Click()
SetWindowLong xhwnd, GWL_EXSTYLE, WS_EX_LAYERED
SetLayeredWindowAttributes xhwnd, 0, Slider1.Value, LWA_ALPHA
End Sub

Private Sub Command2_Click()
SetWindowLong xhwnd, GWL_EXSTYLE, 0
End Sub


Private Sub Label2_Click()
ShellExecute Me.hwnd, "", "http://digitalpbk.blogspot.com/2007/01/making-any-window-transparent-on-xp.html?ref=EXE", "", "", 1
End Sub

Private Sub Slider1_Change()
SetLayeredWindowAttributes xhwnd, 0, Slider1.Value, LWA_ALPHA 'Change the transparency level
End Sub

Private Sub Timer1_Timer()
Dim xy As POINTAPI
Dim retStr As String * 80

If GetAsyncKeyState(vbKeyControl) Then
Call GetCursorPos&(xy)
xhwnd = WindowFromPoint(xy.x, xy.y)
Call GetWindowText(xhwnd, retStr, 100)
If xhwnd = Me.hwnd Then 'Protect self from being transparent
Label1.Caption = "Making me transparent is not good!"
xhwnd = 0
Else
Label1.Caption = retStr & " " & xy.x & " " & xy.y & " " & xhwnd
End If
End If
End Sub

Anything else?
Lemme know ...leave a comment.

Friday, December 15, 2006

Drag 'n Drop Files from explorer to VB

Introduction
This piece of code allows to make programs in Visual Basic 6, to have a drag and drop support of files directly from the folders. This is an alternative to the old File Open mechanism. This is way cooler...Code tested on Windows XP and on Visual Basic 6. Any information about other versions of windows or vb? Leave a comment...

Download
Download the code from planet source code.

The Code
The main part of the code is the module, here is the module code:
DragDrop.bas


Private Const WM_DROPFILES = &H233
'&H233 is the windows message id for the drop files message.
'It is the value of the uMsg parameter in the window procedure call.


Private Const GWL_WNDPROC = (-4)
'The index parameter to the SetWindowLong function
'that specifies to change a windows message handler procedure.


Private Declare Sub DragAcceptFiles Lib "shell32.dll" _
(ByVal hwnd As Long, ByVal fAccept As Long)
'DragAcceptFiles enables or disables a form or window to accept files.
'fAccept = 1 Enables.


Private Declare Function DragQueryFile Lib "shell32.dll" Alias "DragQueryFileA" _
(ByVal HDROP As Long, ByVal UINT As Long, ByVal lpStr As String, ByVal ch As Long) As Long
'DragQueryFile gives the information to us about the dropped file.
'lpStr outputs the filename.


Private Declare Sub DragFinish Lib "shell32.dll" _
(ByVal HDROP As Long)
'This function frees the resources used during the drag operation

Private PrevProc As Long
'Variable to store the address of the default window procedure

Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long

Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" _
(ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, _
ByVal msg As Long, ByVal wParam As Long, _
ByVal lParam As Long) As Long

Private Function HookForm(ByVal hwnd As Long)
PrevProc = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf WindowProc)
'Setting our new windowProc function, now all message to window goes through WindowProc.
'Return value is the address of the previous function. ie,
'the AddressOf default window proc function

End Function
'Our Custom WindowProc Function
Private Function WindowProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If uMsg = WM_DROPFILES Then 'If we have got a drop
Dropped wParam 'wparam stores the Hdrop handle
End If
WindowProc = CallWindowProc(PrevProc, hwnd, uMsg, wParam, lParam)
'Call the default window procedure !IMPORTANT
End Function

'Remove our default window procedure.
Private Function UnHookForm(ByVal hwnd As Long)
If PrevProc <> 0 Then
SetWindowLong hwnd, GWL_WNDPROC, PrevProc
PrevProc = 0
End If
End Function

''' interface api '''
Public Sub EnableDragDrop(ByVal hwnd As Long)
DragAcceptFiles hwnd, 1
HookForm (hwnd)
End Sub

Public Sub DisableDragDrop(ByVal hwnd As Long)
DragAcceptFiles hwnd, 0
UnHookForm hwnd
End Sub

Public Sub Dropped(ByVal HDROP As Long)
Dim strFilename As String * 511
Call DragQueryFile(HDROP, 0, strFilename, 511) 'Get the filename.

'!! replace with your function below ....
Form1.GotADrop (strFilename)
End Sub

... and here is the form code ...

Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
(ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, _
ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Private Sub Command1_Click()
ShellExecute Me.hwnd, "", "http://digitalpbk.blogspot.com", "", "", 1
End Sub

Private Sub Form_Load()
EnableDragDrop Me.hwnd
End Sub

Public Sub GotADrop(ByVal strfile As String)
Label1.Caption = strfile
End Sub

Private Sub Form_Unload(Cancel As Integer)
DisableDragDrop Me.hwnd
End Sub

Thats all folks
Enjoy drag 'n drop...

Friday, November 24, 2006

Opening/Closing of CD Drive from VB

Opening and closing of the CD drive can be achieved easily in visual basic by the following code


Private Declare Function mciSendString Lib "winmm.dll" _
Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, _
ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long

Dim strReturn As String

Public Sub OpenCDDrive() 'Call me to open the CD Drive
Call mciSendString("set CDAudio door open", strReturn, 127, 0)
End Sub

Public Sub CloseCDDrive() 'Call me to close the CD Drive
Call mciSendString("set CDAudio door closed", strReturn, 127, 0)
End Sub


mciSendString is an alias of function mciSendStringA in winmm.dll

I guess the code speaks for itself. (Tested on XP, VB6).

Related:
In Linux, from the command line:

To open the drive:
eject DriveNameHere

To close the drive:
eject -t DriveNameHere

If your not sure what the drive name is, just look in /etc/fstab:
cat /etc/fstab

In my case I would use:
eject /media/cdrom
and
eject -t /media/cdrom