Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Here are what the arguments to MoveMemory mean:

  dest—Pointer to the destination buffer
  src—Pointer to the source buffer
  length—Number of bytes to move

To allocate memory, we used GlobalAlloc to get a handle to a memory area; to use that memory, we used GlobalLock to get a pointer to the memory area. To unlock memory, you pass a pointer to that memory to GlobalUnlock:

Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long

To free memory, you pass a memory handle to GlobalFree:

Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long

Let’s see this at work. Here, we add the declarations of the functions we’ll use to the program we developed in the previous topic, as well as a buffer, inbuffer, to store the data we read:

Const DataLength = 40
Const GMEM_MOVEABLE = &H2

Dim outbuffer As String * DataLength
Dim inbuffer As String * DataLength

Dim memHandle As Long
Dim memPointer As Long

Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long,_
    ByVal dwBytes As Long) As Long
Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) _
    As Long
Private Declare Sub MoveMemory Lib "kernel32" Alias "RtlMoveMemory" 
    (ByVal dest As Any, ByVal src As Any, ByVal length As Long)
Private Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) _
    As Long
Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) _
    As Long

Then, when the user clicks Command2, we can use the memory pointer we created in the program in the previous topic, hMemoryPointer, with MoveMemory, to copy the string to the buffer:

Private Sub Command2_Click()

    Call MoveMemory(inbuffer, hMemoryPointer, DataLength)
...

Now we can move the string to Text2 from inbuffer:

Private Sub Command2_Click()

    Call MoveMemory(inbuffer, hMemoryPointer, DataLength)

    Text2.Text = inbuffer
...

Finally, we unlock and free the memory we’ve used:

Private Sub Command2_Click()

     Call MoveMemory(inbuffer, hMemoryPointer, DataLength)

     Text2.Text = inbuffer

     GlobalUnlock (hMemoryPointer)
     GlobalFree (hMemory)

End Sub

That’s it—when you run the program, as shown in Figure 23.10, and click the Store Text In Memory button, the text in the top text box, Text1, is stored in memory. When you click the Read Text From Memory button, the string is read back and displayed in the bottom text box, Text2, as you see in Figure 23.10. Now we’re using memory with the Windows API.


Figure 23.10  Storing and reading memory data.

The code for this example is located in the winmemory folder on this book’s accompanying CD-ROM.

Making A Window Topmost

You can use SetWindowPos to make a window “topmost,” which means it’ll always stay on top of other windows:

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

Here’s what the arguments to this function mean:

  hwnd—Handle of the window to work with.
  hWndInsertAfter—Handle to the window that will precede this window in the Z-order. This parameter can be a window handle set to one of the following values: HWND_BOTTOM (= 1; places the window at the bottom of the Z-order), HWND_TOP (= 0; places the window at the top of the Z-order), HWND_TOPMOST (= –1; places the window above all non-topmost windows), or HWND_NOTOPMOST (= –2; repositions the window to the top of all non-topmost windows).
  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.
  cy—Specifies the new height of the window.
  wFlags—Specifies sizing and positioning options, as shown in Table 23.5.
Table 23.5 SetWindowPos flags.
Constant Meaning
SWP_DRAWFRAME (= &H20) Draws a frame (defined when the window was created) around the window.
SWP_FRAMECHANGED (= &H20) Sends a WM_NCCALCSIZE message to the window, even if the window’s size is not being changed. If this flag is not specified, WM_NCCALCSIZE is sent only when the window’s size is being changed.
SWP_HIDEWINDOW (=&H80) Hides the window.
SWP_NOACTIVATE (= &H10) Does not activate the window. If this flag is not set, the window is activated and moved to the top of either the topmost or the non-topmost group (depending on the setting of the hWndInsertAfter parameter).
SWP_NOCOPYBITS (= &H100) Discards the entire contents of the client area. If this flag is not specified, the valid contents of the client area are saved and copied back into the client area after the window is sized or repositioned.
SWP_NOMOVE (= 2) Retains current position (ignores the x and y parameters).
SWP_NOOWNERZORDER (= &H200) Does not change the owner window’s position in the Z-order.
SWP_NOREDRAW (= 8) Does not redraw changes. If this flag is set, no repainting of any kind occurs.
SWP_NOREPOSITION (= &H200) Same as SWP_NOOWNERZORDER.
SWP_NOSIZE (= 1) Retains current size (ignores the cx and cy parameters).
SWP_NOZORDER (= 4) Retains current ordering (ignores hWndInsertAfter).
SWP_SHOWWINDOW (=&H40) Displays the window.

Let’s see an example. Here, we’ll size a window to 100 x 100 pixels and make it a topmost window. We start by declaring SetWindowPos in our program, as well as the constants we’ll use, SWP_SHOWWINDOW, SWP_DRAWFRAME, and HWND_TOPMOST:

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

Const HWND_TOPMOST = -1
Const SWP_SHOWWINDOW = &H40
Const SWP_DRAWFRAME = &H20


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.