home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 39 / IOPROG_39.ISO / SOFT / sdkjava40.exe / data1.cab / fg_Samples / Samples / JDirect / funcptr1 / FuncPtr1.java next >
Encoding:
Java Source  |  2000-05-04  |  2.9 KB  |  80 lines

  1. /* (C) Copyright 1995 - 1999 Microsoft Corporation.  All rights reserved.
  2.  *
  3.  * This example demonstrates the use of the Callback class to
  4.  * simulate function pointers in Java. To create a function pointer
  5.  * in Java requires the following steps:
  6.  *
  7.  *   1. Write a class that derives from Callback.
  8.  *   2. Implement a public method named "callback" (all lowercase).
  9.  *      The method must be non-static, accept only integers as parameters
  10.  *      and return an integer, boolean, character, short or void.
  11.  *   3. Create an instance of your new class. Pass that in as the
  12.  *      function pointer argument.
  13.  *
  14.  * This particular example uses two classes: EnumWindowsProc which derives
  15.  * directly from Callback, and SampleEnumWindowsProc which derives
  16.  * from EnumWindowsProc. The parent class (EnumWindowsProc) is abstract
  17.  * and serves only to define the method prototype for "callback."
  18.  * The child class (SampleEnumWindowsProc) provides the actual implementation.
  19.  * Different calls to EnumWindows() are likely to use different
  20.  * child classes; however, they all derive from EnumWindowsProc so they
  21.  * can use the common declaration of EnumWindows (which is prototyped
  22.  * to accept an EnumWindowsProc.)
  23.  *
  24.  */
  25.  
  26. public class FuncPtr1
  27. {
  28.     public static void main(String args[])
  29.     {
  30.         boolean fSuccess = EnumWindows(new SampleEnumWindowsProc(), 0);
  31.         if (!fSuccess) {
  32.             System.err.println("EnumWindows failed.");
  33.         }
  34.     }
  35.  
  36.  
  37.     /** @dll.import("USER32") */
  38.     static native boolean EnumWindows(EnumWindowsProc wndenmprc, int lparam);
  39.  
  40.  
  41. }
  42.  
  43.  
  44.  
  45. //==========================================================================
  46. // An abstract parent class that describes all EnumWindowsProc's but does
  47. // not provide any implementation. 
  48. //==========================================================================
  49. abstract class EnumWindowsProc extends com.ms.dll.Callback
  50. {
  51.     // It is required that this method be named "callback".
  52.     public abstract boolean callback(int hwnd, int lparam);
  53. }
  54.  
  55.  
  56. //==========================================================================
  57. // A specific EnumWindowsProc which simply prints out the window handle
  58. // and title bar text.
  59. //==========================================================================
  60. class SampleEnumWindowsProc extends EnumWindowsProc
  61. {
  62.     public boolean callback(int hwnd, int lparam)
  63.     {
  64.         StringBuffer text = new StringBuffer(50);
  65.         GetWindowText(hwnd, text, text.capacity()+1);
  66.         if (text.length() != 0) {
  67.             System.out.println("hwnd = " +
  68.                                Integer.toHexString(hwnd) +
  69.                                "h: " +
  70.                                text);
  71.         }
  72.         return true;  // return TRUE to continue enumeration.
  73.     }
  74.  
  75.     /** @dll.import("USER32") */
  76.     static native int GetWindowText(int hwnd, StringBuffer text, int cch);
  77.  
  78. }
  79.  
  80.