home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 39 / IOPROG_39.ISO / SOFT / sdkjava40.exe / data1.cab / fg_Samples / Samples / JDirect / tbaricon / TBarIcon.java < prev   
Encoding:
Java Source  |  2000-05-04  |  9.0 KB  |  325 lines

  1. /* (C) Copyright 1995 - 1999 Microsoft Corporation.  All rights reserved.
  2.  *
  3.  * This example sets up a TaskBar icon and uses a WndProc
  4.  * to capture mouse messages.
  5.  *
  6.  */
  7.  
  8.  
  9. import com.ms.dll.*;
  10.  
  11.  
  12. /** @dll.import(auto) */
  13. public class TBarIcon
  14. {
  15.  
  16.     public static void main(String args[])
  17.     {
  18.  
  19.         System.out.println("TBarIcon sample. Bring up the system taskbar");
  20.         System.out.println("to see the icon (an exclamation point) inserted");
  21.         System.out.println("by this application. Click on the icon on to");
  22.         System.out.println("send mouse messages to the application.");
  23.         System.out.println();
  24.         System.out.println("Press Ctrl-C in this window to quit.");
  25.  
  26.  
  27.         int hicon = LoadIcon(0, IDI_EXCLAMATION);
  28.  
  29.  
  30.         // Note: passing in a root address rather than BreakHandler itself.
  31.         // That's to prevent BreakHandler from being garbage-collected
  32.         // as soon as SetConsoleCtrlHandler returns.
  33.         SetConsoleCtrlHandler(DllLib.addrOf(Root.alloc(new BreakHandler())),
  34.                               true);
  35.         
  36.  
  37.         WNDCLASS wndclass = new WNDCLASS();
  38.  
  39.         wndclass.lpfnWndProc   = DllLib.addrOf(TBarWndProcRoot);
  40.         wndclass.lpszClassName = "MyWindow";
  41.  
  42.         if (0 == RegisterClass(wndclass)) {
  43.             throw new RuntimeException("RegisterClass failed.");
  44.         }
  45.  
  46.         m_hwnd = CreateWindow(wndclass.lpszClassName,
  47.                               "MyWindow",
  48.                               WS_OVERLAPPEDWINDOW,
  49.                               0,0,
  50.                               0,0,
  51.                               0, 0, 0, 0);
  52.         if (0 == m_hwnd) {
  53.             throw new RuntimeException("CreateWindow failed.");
  54.         }
  55.  
  56.         NOTIFYICONDATA nid = new NOTIFYICONDATA();
  57.         nid.hWnd   = m_hwnd;
  58.         nid.uID    = 1;
  59.         nid.uFlags = nid.NIF_MESSAGE|nid.NIF_ICON|nid.NIF_TIP;
  60.         nid.uCallbackMessage = WM_MYTBARICON;
  61.         nid.hIcon  = hicon;
  62.         nid.szTip  = "TBarIcon Java Sample. Hit Ctrl-C to quit.";
  63.  
  64.         if (!Shell_NotifyIcon(NIM_ADD, nid)) {
  65.             throw new RuntimeException("Could not add taskbar icon.");
  66.         }
  67.         m_fTBarIconActive = true;
  68.  
  69.  
  70.         MSG msg = new MSG();
  71.         while (GetMessage(msg, 0, 0, 0)) {
  72.             TranslateMessage(msg);
  73.             DispatchMessage(msg);
  74.         }
  75.  
  76.         
  77.  
  78.  
  79.  
  80.  
  81.     }
  82.  
  83.  
  84.     static int     m_hwnd;
  85.     static boolean m_fTBarIconActive = false;
  86.  
  87.     static void destroyTBarIcon()
  88.     {
  89.         if (m_fTBarIconActive) {
  90.             m_fTBarIconActive = false;
  91.             NOTIFYICONDATA nid = new NOTIFYICONDATA();
  92.             nid.hWnd = m_hwnd;
  93.             nid.uID = 1;
  94.             nid.uFlags = nid.NIF_ICON;
  95.             Shell_NotifyIcon(NIM_DELETE, nid);
  96.         }
  97.     }
  98.  
  99.  
  100.     static final int TBarWndProcRoot = Root.alloc(new TBarWndProc());
  101.  
  102.     static final int WM_MYTBARICON = 0x500;
  103.  
  104.  
  105.     /** @dll.import("KERNEL32") */
  106.     static native boolean SetConsoleCtrlHandler(int     pHandlerRoutine,
  107.                                                 boolean fAdd);
  108.  
  109.  
  110.     /** @dll.import("SHELL32") */
  111.     static native boolean Shell_NotifyIcon(int dwMsg, NOTIFYICONDATA pnid);
  112.  
  113.     static final int NIM_ADD    = 0x00000000;
  114.     static final int NIM_MODIFY = 0x00000001;
  115.     static final int NIM_DELETE = 0x00000002;
  116.  
  117.  
  118.  
  119.  
  120.  
  121.     /** @dll.import("USER32") */
  122.     static native int LoadIcon(int hinstance, int iconid);
  123.  
  124.     static final int IDI_EXCLAMATION = 32515;
  125.  
  126.  
  127.  
  128.     /** @dll.import("USER32") */
  129.     static native boolean GetMessage(MSG msg,
  130.                                      int hwnd,
  131.                                      int uMsgFilterMin,
  132.                                      int uMsgFilterMax);
  133.     /** @dll.import("USER32") */
  134.     static native boolean TranslateMessage(MSG msg);
  135.     /** @dll.import("USER32") */
  136.     static native int     DispatchMessage(MSG msg);
  137.  
  138.  
  139.     /** @dll.import("USER32") */
  140.     static native int RegisterClass(WNDCLASS wc);
  141.  
  142.  
  143.     /** @dll.import("USER32") */
  144.     static native int CreateWindowEx(int  dwExStyle,
  145.                                      String lpszClassName,
  146.                                      String lpszWindowName,
  147.                                      int    style,
  148.                                      int    x,
  149.                                      int    y,
  150.                                      int    nWidth,
  151.                                      int    nHeight,
  152.                                      int    hwndParent,
  153.                                      int    hMenu,
  154.                                      int    hInst,
  155.                                      int    pvParam);
  156.     static int CreateWindow(String lpszClassName,
  157.                             String lpszWindowName,
  158.                             int    style,
  159.                             int    x,
  160.                             int    y,
  161.                             int    nWidth,
  162.                             int    nHeight,
  163.                             int    hwndParent,
  164.                             int    hMenu,
  165.                             int    hInst,
  166.                             int    pvParam)
  167.     {
  168.         return CreateWindowEx(0, lpszClassName, lpszWindowName, style,
  169.                               x, y, nWidth, nHeight, hwndParent, hMenu, hInst,
  170.                               pvParam);
  171.     }
  172.  
  173.  
  174.     static final int WS_OVERLAPPEDWINDOW = 0x00CF0000;
  175.  
  176. }
  177.  
  178.  
  179. //===================================================================
  180. // Callback to receive window messages from the Taskbar Icon.
  181. //===================================================================
  182.  
  183. /** @dll.import(auto) */
  184. class TBarWndProc extends Callback 
  185. {
  186.  
  187.     static final int WM_DESTROY       = 0x0002;
  188.     static final int WM_LBUTTONDOWN   = 0x0201;
  189.     static final int WM_LBUTTONUP     = 0x0202;
  190.     static final int WM_LBUTTONDBLCLK = 0x0203;
  191.     static final int WM_RBUTTONDOWN   = 0x0204;
  192.     static final int WM_RBUTTONUP     = 0x0205;
  193.     static final int WM_RBUTTONDBLCLK = 0x0206;
  194.  
  195.     public int callback(int hwnd, int message, int wParam, int lParam)
  196.     {
  197.         switch (message)
  198.         {
  199.             case WM_DESTROY:
  200.                 System.out.println("Posting WM_QUIT.");
  201.                 PostQuitMessage(0);
  202.                 return 0;
  203.  
  204.  
  205.             case TBarIcon.WM_MYTBARICON:
  206.                 {
  207.                     switch (lParam) {
  208.                         case WM_LBUTTONDOWN:
  209.                             System.out.println("Left button clicked.");
  210.                             break;
  211.  
  212.                         case WM_LBUTTONDBLCLK:
  213.                             System.out.println("Left button double-clicked.");
  214.                             break;
  215.  
  216.                         case WM_RBUTTONDOWN:
  217.                             System.out.println("Right button clicked.");
  218.                             break;
  219.  
  220.                         case WM_RBUTTONDBLCLK:
  221.                             System.out.println("Right button double-clicked.");
  222.                             break;
  223.  
  224.                     }
  225.                 }
  226.                 return 0;
  227.  
  228.         }
  229.         
  230.         return DefWindowProc(hwnd, message, wParam, lParam);
  231.     }
  232.  
  233.  
  234.  
  235.     /** @dll.import("USER32") */
  236.     static native void PostQuitMessage(int nExitCode);
  237.  
  238.     /** @dll.import("USER32") */
  239.     static native int DefWindowProc(int hwnd, int msg, int wParam, int lParam);
  240.  
  241. }
  242.  
  243.  
  244.  
  245. //===================================================================
  246. // Handler for ctrl-C.
  247. //===================================================================
  248.  
  249. /** @dll.import(auto) */
  250. class BreakHandler extends Callback
  251. {
  252.  
  253.     static final int CTRL_C_EVENT     = 0;
  254.     static final int CTRL_BREAK_EVENT = 1;
  255.  
  256.     public boolean callback(int ctrlType)
  257.     {
  258.         if (ctrlType == CTRL_C_EVENT || ctrlType == CTRL_BREAK_EVENT) {
  259.             TBarIcon.destroyTBarIcon();
  260.             return false;
  261.         }
  262.  
  263.         return false;
  264.  
  265.     }
  266.  
  267.     /** @dll.import("USER32") */
  268.     static native void PostQuitMessage(int nExitCode);
  269.  
  270. }
  271.  
  272.  
  273.  
  274. /** @dll.struct(auto) */
  275. class NOTIFYICONDATA
  276. {
  277.     public int     cbSize = DllLib.sizeOf(getClass());
  278.     public int     hWnd;
  279.     public int     uID;
  280.     public int     uFlags;
  281.     public int     uCallbackMessage;
  282.     public int     hIcon;
  283.     /** @dll.structmap([type=TCHAR[64]]) */
  284.     public String  szTip;
  285.  
  286.  
  287.     static public final int NIF_MESSAGE = 0x00000001;
  288.     static public final int NIF_ICON    = 0x00000002;
  289.     static public final int NIF_TIP     = 0x00000004;
  290.  
  291.     
  292. }
  293.  
  294.  
  295.  
  296. /** @dll.struct(auto) */
  297. class WNDCLASS
  298. {
  299.     int        style;
  300.     int        lpfnWndProc;
  301.     int        cbClsExtra;
  302.     int        cbWndExtra;
  303.     int        hInstance;
  304.     int        hIcon;
  305.     int        hCursor;
  306.     int        hbrBackground;
  307.     String     lpszMenuName;
  308.     String     lpszClassName;
  309. }
  310.  
  311. /** @dll.struct(auto) */
  312. class MSG
  313. {
  314.     int     hwnd;
  315.     int     message;
  316.     int     wParam;
  317.     int     lParam;
  318.     int     time;
  319.     int     ptx;
  320.     int     pty;
  321. }
  322.  
  323.  
  324.  
  325.