⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tij0193.html

📁 学习java的经典书籍
💻 HTML
📖 第 1 页 / 共 3 页
字号:
      "Thinking in Java", 0);
  }
}</PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Since
the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>MessageBeep(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>MessageBox(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
functions are now declared as static in a different class, you must call them
specifying their scope. You might think that you must use the approach above to
map 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>all
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">of
the <A NAME="Index3101"></A>Win32
API (functions, constants, and data types) to Java classes. Fortunately, you
don&#8217;t have to.
</FONT><a name="_Toc408018828"></a><P></DIV>
<A NAME="Heading601"></A><H3 ALIGN=LEFT>
The
com.ms.win32 package
</H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
Win32 API is fairly big &#8211; on the order of a thousand functions,
constants, and data types. Of course, you do not want to write the Java
equivalent of every single Win32 API function. Microsoft took care of this,
distributing a Java package that maps the Win32 API to Java classes using
J/Direct. This package, named <A NAME="Index3102"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>com.ms.win32</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
is installed in your classpath during the installation of the Java SDK 2.0 if
you select it in the setup options. The package is made up of large number of
Java classes that reproduce the constants, data structures, and functions of
the Win32 API. The three richest classes are 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>User32.class</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Kernel32.class</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
and 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Gdi32.class</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
These contain the core of the Win32 API. To use them, just import them in your
Java code. The 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>ShowMsgBox</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
example above can be rewritten using 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>com.ms.win32</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
as follows (I also took care of the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>UnsatisfiedLinkError</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
in a more civilized way):
</FONT><P></DIV>

<font color="#990000"><PRE><font color="#0000ff">import</font> com.ms.win32.*;

<font color="#0000ff">public</font> <font color="#0000ff">class</font> UseWin32Package {
  <font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> main(String args[]) {
    <font color="#0000ff">try</font> {
      User32.MessageBeep(
        winm.MB_ICONEXCLAMATION);
      User32.MessageBox(0,
        "Created by the MessageBox() Win32 func",
        "Thinking in Java",
        winm.MB_OKCANCEL |
        winm.MB_ICONEXCLAMATION);
    } <font color="#0000ff">catch</font>(UnsatisfiedLinkError e) {
      System.out.println("Can&#8217;t link Win32 API");
      System.out.println(e);
    }
  }
}</PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
package is imported in the first line. The 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>MessageBeep(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>MessageBox(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
functions can now be called with no other declarations. In 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>MessageBeep(&#160;)
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">you
can see that importing the package has also declared the Win32 constants. These
constants are defined in a number of Java interfaces, all named winx (x is the
first letter of the constant you want to use). 
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">At
the time of this writing, the classes in the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>com.ms.win32</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
package are still under development, but usable nonetheless.
</FONT><a name="_Toc408018829"></a><P></DIV>
<A NAME="Heading602"></A><H3 ALIGN=LEFT>
Marshaling<P><A NAME="Index3103"></A></H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Marshaling
means converting a function argument from its native binary representation into
some language-independent format, and then converting this generic
representation into a binary format that is appropriate to the called function.
In the example above, we called the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>MessageBox(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
function and passed it a couple of 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">s.
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>MessageBox(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is a C function, and the binary layout of Java 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">s
is not the same as C strings, but the arguments are nonetheless correctly
passed. That&#8217;s because J/Direct takes care of converting a Java 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
into a C string before calling the C code. This happens with all standard Java
types. Below is a table of the implicit conversions for simple data types:
</FONT><P></DIV>
<DIV ALIGN=LEFT><TABLE BORDER>
<COLGROUP>
      <COL width="55">
      <COL width="185">
</COLGROUP>
<TR VALIGN="TOP">
<TD WIDTH=55 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Java</B></FONT><P></DIV>
</TD>
<TD WIDTH=185 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>C</B></FONT><P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD WIDTH=55 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>byte</B></FONT><P></DIV>
</TD>
<TD WIDTH=185 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">BYTE
or CHAR
</FONT><P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD WIDTH=55 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>short</B></FONT><P></DIV>
</TD>
<TD WIDTH=185 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">SHORT
or WORD
</FONT><P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD WIDTH=55 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>int</B></FONT><P></DIV>
</TD>
<TD WIDTH=185 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">INT,
UINT, LONG, ULONG, or DWORD
</FONT><P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD WIDTH=55 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>char</B></FONT><P></DIV>
</TD>
<TD WIDTH=185 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">TCHAR</FONT><P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD WIDTH=55 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>long</B></FONT><P></DIV>
</TD>
<TD WIDTH=185 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">__int64</FONT><P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD WIDTH=55 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>float</B></FONT><P></DIV>
</TD>
<TD WIDTH=185 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Float</FONT><P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD WIDTH=55 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>double</B></FONT><P></DIV>
</TD>
<TD WIDTH=185 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Double</FONT><P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD WIDTH=55 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>boolean</B></FONT><P></DIV>
</TD>
<TD WIDTH=185 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">BOOL</FONT><P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD WIDTH=55 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String</B></FONT><P></DIV>
</TD>
<TD WIDTH=185 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">LPCTSTR
(Allowed as return value only in ole mode)
</FONT><P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD WIDTH=55 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -