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

  1. /* (C) Copyright 1995 - 1999 Microsoft Corporation.  All rights reserved.
  2.  *
  3.  * This example demonstrates a more advanced use of the Callback class.
  4.  * In particular, it shows how the DllLib.ptrToStruct() method can be
  5.  * used to get around the limitation that the callback can only take
  6.  * integer parameters. The ptrToStruct() method acts like a C-style cast.
  7.  *
  8.  * 
  9.  */
  10.  
  11. import com.ms.dll.*;
  12.  
  13. /** @dll.import("USER32",auto) */
  14. public class FuncPtr2
  15. {
  16.     public static void main(String args[])
  17.     {
  18.         EnumFonts(GetDC(GetDesktopWindow()),
  19.                   null,
  20.                   new SampleEnumFontsProc(),
  21.                   0);
  22.     }
  23.  
  24.  
  25.  
  26.     private static native int GetDesktopWindow();
  27.     private static native int GetDC(int hwnd);
  28.  
  29.     /** @dll.import("GDI32") */
  30.     private static native int EnumFonts(int           hdc,
  31.                                         String        faceName,
  32.                                         EnumFontsProc wndenfprc,
  33.                                         int           lparam);
  34.  
  35. }
  36.  
  37.  
  38.  
  39. //----------------------------------------------------------------------
  40. // A specific ENUMFONTPROC. All this does is print the font face name.
  41. //----------------------------------------------------------------------
  42. class SampleEnumFontsProc extends EnumFontsProc
  43. {
  44.     public int enumFontsProc(LOGFONT    pLOGFONT,
  45.                              TEXTMETRIC pTEXTMETRIC,
  46.                              int        dwType,
  47.                              int        lparam)
  48.     {
  49.         System.out.println(pLOGFONT.lfFaceName);
  50.         return 1;  // returning non-zero to continue enumeration.
  51.     }
  52.  
  53.  
  54.  
  55. }
  56.  
  57.  
  58. //----------------------------------------------------------------------
  59. // An abstract class used to describe ENUMFONTPROC's in general.
  60. // In particular, it takes care of converting the "callback" arguments
  61. // (which must all be declared as integers), into @dll.struct's for
  62. // easy parsing by the real callback function ("enumFontsProc").
  63. //----------------------------------------------------------------------
  64. abstract class EnumFontsProc extends com.ms.dll.Callback
  65. {
  66.     // It is required that this method be named "callback".
  67.     public int callback(int pLOGFONT, int pTEXTMETRIC, int dwType, int lparam)
  68.     {
  69.         return enumFontsProc((LOGFONT)DllLib.ptrToStruct(LOGFONT.class, pLOGFONT),
  70.                              (TEXTMETRIC)DllLib.ptrToStruct(TEXTMETRIC.class, pTEXTMETRIC),
  71.                              dwType,
  72.                              lparam);
  73.     }
  74.  
  75.     public abstract int enumFontsProc(LOGFONT    pLOGFONT,
  76.                                       TEXTMETRIC pTEXTMETRIC,
  77.                                       int        dwType,
  78.                                       int        lparam);
  79. }
  80.  
  81.  
  82.  
  83.  
  84. /** @dll.struct(auto) */
  85. class LOGFONT {
  86.     int     lfHeight;
  87.     int     lfWidth;
  88.     int     lfEscapement;
  89.     int     lfOrientation;
  90.     int     lfWeight;
  91.     byte    lfItalic;
  92.     byte    lfUnderline;
  93.     byte    lfStrikeOut;
  94.     byte    lfCharSet;
  95.     byte    lfOutPrecision;
  96.     byte    lfClipPrecision;
  97.     byte    lfQuality;
  98.     byte    lfPitchAndFamily;
  99.  
  100.     /** @dll.structmap([type=TCHAR[32]]) */
  101.     String  lfFaceName;
  102. }
  103.  
  104.  
  105.  
  106. /** @dll.struct(auto) */
  107. class TEXTMETRIC
  108. {
  109.     int        tmHeight;
  110.     int        tmAscent;
  111.     int        tmDescent;
  112.     int        tmInternalLeading;
  113.     int        tmExternalLeading;
  114.     int        tmAveCharWidth;
  115.     int        tmMaxCharWidth;
  116.     int        tmWeight;
  117.     int        tmOverhang;
  118.     int        tmDigitizedAspectX;
  119.     int        tmDigitizedAspectY;
  120.     char       tmFirstChar;
  121.     char       tmLastChar;
  122.     char       tmDefaultChar;
  123.     char       tmBreakChar;
  124.     byte       tmItalic;
  125.     byte       tmUnderlined;
  126.     byte       tmStruckOut;
  127.     byte       tmPitchAndFamily;
  128.     byte       tmCharSet;
  129. }
  130.  
  131.