11.07.2015 Views

101 Tech Tips - Visual Studio Magazine - One-Stop Source Shop

101 Tech Tips - Visual Studio Magazine - One-Stop Source Shop

101 Tech Tips - Visual Studio Magazine - One-Stop Source Shop

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

For even more tricks and tips go towww.vbpj.com or www.vcdj.comVB4/32, VB5, VB6Level: IntermediateGet the Drive Serial NumberYou can get the serial number of your hard drive, floppy disk, orCD-ROM easily without any additional ActiveX component. First,start a VB project, add a standard module, and place a CommandButton control on the form:'-- Module codePrivate Declare Function GetVolumeInformation _Lib "kernel32" Alias "GetVolumeInformationA" _(ByVal lpRootPathName As String, _ByVal pVolumeNameBuffer As String, _ByVal nVolumeNameSize As Long, _lpVolumeSerialNumber As Long, _lpMaximumComponentLength As Long, _lpFileSystemFlags As Long, _ByVal lpFileSystemNameBuffer As String, _ByVal nFileSystemNameSize As Long) As LongPublic Function GetSerialNumber( _ByVal sDrive As String) As LongIf Len(sDrive) ThenIf InStr(sDrive, "\\") = 1 Then' Make sure we end in backslash for UNCIf Right$(sDrive, 1) "\" ThensDrive = sDrive & "\"End IfElse' If not UNC, take first letter as drivesDrive = Left$(sDrive, 1) & ":\"End IfElse' Else just use current drivesDrive = vbNullStringEnd If' Grab S/N -- Most params can be NULLCall GetVolumeInformation( _sDrive, vbNullString, 0, GetSerialNumber, _ByVal 0&, ByVal 0&, vbNullString, 0)End Function'-- Form codePrivate Sub Command1_Click()Dim Drive As StringDrive = InputBox("Enter drive for checking SN")MsgBox Hex$(GetSerialNumber(Drive))End SubVB3, VB4, VB5, VB6Level: Intermediate—Predrag Dervisevic, Krusevac, YugoslaviaSelect Areas Within a Graphics WindowGraphics applications sometimes require users to select a rectangularregion of a picture or drawing visually. You need to providea resizing box manipulated by the pointer at run time that onlyinteracts temporarily with the graphics displayed already (downloadthis code).By assigning vbInvert to the PictureBox DrawMode propertybefore selection dragging, you can restore the background graphicsby redrawing the same rectangle. Once the selection draggingcompletes, mRect contains the selected rectangle coordinates.You can use the same technique to select a circular region orcreate the “rubber band” effect.—James Menesez, Templeton, Calif.VB6Level: BeginningRead a Complete Text File in <strong>One</strong> PassTypically, you read and process a text file by using a loop and VB’sLine Input statement:Do While Not Eof(1)Line Input #1, myStringVar$' process the line hereLoopHowever, you might want to defer processing or keep a copy ofall the lines read for repeat processing or selective editing beforewriting them out again. You can achieve this quite easily by usingVB’s Get# and Split( ) statements to read the entire file at once andsplit it into an array containing all the lines. For example, thisfunction returns the complete contents of a file as a string:Public Function ReadFile(ByVal FileName As String) _As StringDim hFile As LongDim bBuf() As BytehFile = FreeFileOpen FileName For Binary Access Read As #hFileIf LOF(hFile) > 0 ThenReDim bBuf(1 To LOF(hFile)) As ByteGet #hFile, , bBufClose #hFileReadFile = StrConv(bBuf, vbUnicode)End IfEnd FunctionThis code snippet drops the contents into an array, using the linebreak (vbCrLf) as a delimiter:Dim sLines() As StringDim sAll As StringDim i As Long' Read the contents of some filesAll = ReadFile("c:\form1.frm")' Split into individual linessLines = Split(sAll, vbCrLf)You can then process the file as desired; for example, you cansearch for specific lines:For i = LBound(sLines) to UBound(sLines)If Instr(1, "SomeText", sLines(i), _vbTextCompare) ThensLines(i) = "SomeOtherText"End IfNext iVB4, VB5, VB6Level: Beginning—John Cullen, Pedroucos, PortugalAdd Controls to a Project QuicklyVB’s Add File dialog supports only a single selection of codemodules or OCXs, so you must painstakingly select each individualfile and control one at a time.<strong>One</strong> of my previous tech tips publicized the fact that you candrag FRM, BAS, CLS, or CTL files from Windows Explorer to theProjects window in VB and VB adds them instantly to the project.What I didn’t mention is that you can also drag OCX controls fromExplorer and drop them on the VB6 Toolbox to add OCX controlsto your project just as quickly and easily.—Darin Higgins, Fort Worth, TexasSEPTEMBER 2001 Supplement to <strong>Visual</strong> <strong>Studio</strong> <strong>Magazine</strong> 21

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!