View Javadoc

1   /**
2    * 
3    */
4   package org.sirius.server.win32.classes;
5   
6   import javax.jws.WebService;
7   
8   import org.sirius.server.win32.constants.IMKConsts;
9   import org.sirius.server.win32.constants.IWMConsts;
10  import org.sirius.server.win32.core.types.WinDefExt.WINDOWPLACEMENT;
11  
12  import com.sun.jna.Pointer;
13  
14  /**
15   * @author Myk Kolisnyk
16   * .
17   */
18  @WebService
19  public class Window extends Common implements IWMConsts, IMKConsts {
20  
21      /**
22  	 * .
23  	 */
24      public Window() {
25          // TODO Auto-generated constructor stub
26      }
27  
28      /**
29       * .
30       * @param hwnd .
31       */
32      public final void activate(final long hwnd) {
33          HWND handle = longToHwnd(hwnd);
34          getUser32().SetForegroundWindow(handle);
35      }
36  
37      /**
38       * .
39       * @param hwnd .
40       * @param button .
41       * @param x .
42       * @param y .
43       * @param isControl .
44       * @param isAlt .
45       * @param isShift .
46       */
47      public final void click(final long hwnd, final int button, final int x,
48              final int y, final boolean isControl, final boolean isAlt,
49              final boolean isShift) {
50          mouseDown(hwnd, button, x, y, isControl, isAlt, isShift);
51          mouseUp(hwnd, button, x, y, isControl, isAlt, isShift);
52      }
53  
54      /**
55       * .
56       * @param hwnd .
57       */
58      public final void close(final long hwnd) {
59          HWND handle = longToHwnd(hwnd);
60          WPARAM wParam = new WPARAM();
61          LPARAM lParam = new LPARAM();
62  
63          wParam.setValue(0);
64          lParam.setValue(0);
65          getUser32().PostMessage(handle, WM_CLOSE, wParam, lParam);
66      }
67  
68      /**
69       * .
70       * @param hwnd .
71       * @param button .
72       * @param x .
73       * @param y .
74       * @param isControl .
75       * @param isAlt .
76       * @param isShift .
77       */
78      public final void doubleClick(
79              final long hwnd,
80              final int button,
81              final int x,
82              final int y,
83              final boolean isControl,
84              final boolean isAlt,
85              final boolean isShift) {
86          int message = 0;
87          int flags = 0;
88          switch (button) {
89              case 0:
90                  message = WM_LBUTTONDBLCLK;
91                  break;
92              case 1:
93                  message = WM_RBUTTONDBLCLK;
94                  break;
95              case 2:
96                  message = WM_MBUTTONDBLCLK;
97                  break;
98              default:
99                  message = WM_LBUTTONDBLCLK;
100                 break;
101         }
102 
103         if (isControl) {
104             flags |= MK_CONTROL;
105         }
106         if (isShift) {
107             flags |= MK_SHIFT;
108         }
109         sendMessage(hwnd, message, flags, makeLParam(x, y).intValue());
110     }
111 
112     /**
113      * .
114      * @param hwnd .
115      * @return .
116      */
117     public final RECT getClientRect(final long hwnd) {
118         RECT result = new RECT();
119         HWND handle = longToHwnd(hwnd);
120         getUser32().GetClientRect(handle, result);
121         return result;
122     }
123 
124     public final long getDesktopWindow(){
125         return 0;
126     }
127     
128     /**
129      * .
130      * @param hwnd .
131      * @return .
132      */
133     public final long getMenu(final long hwnd) {
134         HWND handle = longToHwnd(hwnd);
135         HMENU hmenu = getUser32().GetMenu(handle);
136         return Pointer.nativeValue(hmenu.getPointer());
137     }
138 
139     /**
140      * .
141      * @param hwnd .
142      * @return .
143      */
144     public final RECT getRect(final long hwnd) {
145         RECT result = new RECT();
146         HWND handle = longToHwnd(hwnd);
147         getUser32().GetWindowRect(handle, result);
148         return result;
149     }
150 
151     /**
152      * .
153      * @param hwnd .
154      * @param revert .
155      * @return .
156      */
157     public final long getSystemMenu(final long hwnd, final boolean revert) {
158         HWND handle = longToHwnd(hwnd);
159         HMENU hmenu = getUser32().GetSystemMenu(handle, revert);
160         return Pointer.nativeValue(hmenu.getPointer());
161     }
162 
163     /**
164      * .
165      * @param hwnd .
166      * @return .
167      */
168     public final String getText(final long hwnd) {
169         HWND handle = longToHwnd(hwnd);
170         int length = getUser32().GetWindowTextLength(handle) + 1;
171         char[] buf = new char[length];
172 
173         getUser32().GetWindowText(handle, buf, length);
174         String text = String.valueOf(buf).trim();
175         return text;
176     }
177 
178     /**
179      * .
180      * @param hwnd .
181      * @param placement .
182      * @return .
183      */
184     public final boolean getWindowPlacement(final long hwnd,
185             final WINDOWPLACEMENT placement) {
186         HWND handle = longToHwnd(hwnd);
187         return getUser32().GetWindowPlacement(handle, placement);
188     }
189 
190     /**
191      * .
192      * @param hwnd .
193      * @return .
194      */
195     public final boolean isEnabled(final long hwnd) {
196         HWND handle = longToHwnd(hwnd);
197         return getUser32().IsWindowEnabled(handle);
198     }
199 
200     /**
201      * .
202      * @param hwnd .
203      * @return .
204      */
205     public final boolean isMaximized(final long hwnd) {
206         HWND handle = longToHwnd(hwnd);
207         WINDOWPLACEMENT placement = new WINDOWPLACEMENT();
208         getUser32().GetWindowPlacement(handle, placement);
209         return placement.showCmd == SW_MAXIMIZE;
210     }
211 
212     /**
213      * .
214      * @param hwnd .
215      * @return .
216      */
217     public final boolean isMinimized(final long hwnd) {
218         HWND handle = longToHwnd(hwnd);
219         WINDOWPLACEMENT placement = new WINDOWPLACEMENT();
220         getUser32().GetWindowPlacement(handle, placement);
221         return placement.showCmd == SW_SHOWMINIMIZED;
222     }
223 
224     /**
225      * .
226      * @param hwnd .
227      * @return .
228      */
229     public final boolean isNormal(final long hwnd) {
230         HWND handle = longToHwnd(hwnd);
231         WINDOWPLACEMENT placement = new WINDOWPLACEMENT();
232         getUser32().GetWindowPlacement(handle, placement);
233         return placement.showCmd == SW_NORMAL;
234     }
235 
236     /**
237      * .
238      * @param hwnd .
239      * @return .
240      */
241     public final boolean isUnicode(final long hwnd) {
242         HWND handle = longToHwnd(hwnd);
243         return getUser32().IsWindowUnicode(handle);
244     }
245 
246     /**
247      * .
248      * @param hwnd .
249      * @return .
250      */
251     public final boolean isVisible(final long hwnd) {
252         HWND handle = longToHwnd(hwnd);
253         return getUser32().IsWindowVisible(handle);
254     }
255 
256     /**
257      * .
258      * @param hwnd .
259      * @return .
260      */
261     public final boolean isWindow(final long hwnd) {
262         HWND handle = longToHwnd(hwnd);
263         return getUser32().IsWindow(handle);
264     }
265 
266     /**
267      * .
268      * @param hwnd .
269      * @param key .
270      */
271     public final void keyDown(final long hwnd, final int key) {
272         sendMessage(hwnd, WM_KEYDOWN, key, 0);
273     }
274 
275     /**
276      * .
277      * @param hwnd .
278      * @param key .
279      */
280     public final void keyPress(final long hwnd, final int key) {
281         sendMessage(hwnd, WM_CHAR, key, 0);
282     }
283 
284     /**
285      * .
286      * @param hwnd .
287      * @param key .
288      */
289     public final void keyUp(final long hwnd, final int key) {
290         sendMessage(hwnd, WM_KEYUP, key, 0);
291     }
292 
293     /**
294      * .
295      * @param hwnd .
296      */
297     public final void maximize(final long hwnd) {
298         HWND handle = longToHwnd(hwnd);
299         getUser32().ShowWindow(handle, SW_MAXIMIZE);
300     }
301 
302     /**
303      * .
304      * @param hwnd .
305      */
306     public final void minimize(final long hwnd) {
307         HWND handle = longToHwnd(hwnd);
308         getUser32().ShowWindow(handle, SW_MINIMIZE);
309     }
310 
311     /**
312      * .
313      * @param hwnd .
314      * @param button .
315      * @param x .
316      * @param y .
317      * @param isControl .
318      * @param isAlt .
319      * @param isShift .
320      */
321     public final void mouseDown(final long hwnd, final int button, final int x,
322             final int y, final boolean isControl, final boolean isAlt,
323             final boolean isShift) {
324         int message = 0;
325         int flags = 0;
326         switch (button) {
327             case 0:
328                 message = WM_LBUTTONDOWN;
329                 break;
330             case 1:
331                 message = WM_RBUTTONDOWN;
332                 break;
333             case 2:
334                 message = WM_MBUTTONDOWN;
335                 break;
336             default:
337                 message = WM_LBUTTONDOWN;
338                 break;
339         }
340 
341         if (isControl) {
342             flags |= MK_CONTROL;
343         }
344         if (isShift) {
345             flags |= MK_SHIFT;
346         }
347         sendMessage(hwnd, message, flags, makeLParam(x, y).intValue());
348     }
349 
350     /**
351      * .
352      * @param hwnd .
353      * @param button .
354      * @param x .
355      * @param y .
356      * @param isControl .
357      * @param isAlt .
358      * @param isShift .
359      */
360     public final void mouseUp(final long hwnd, final int button, final int x,
361             final int y, final boolean isControl, final boolean isAlt,
362             final boolean isShift) {
363         int message = 0;
364         int flags = 0;
365         switch (button) {
366             case 0:
367                 message = WM_LBUTTONUP;
368                 break;
369             case 1:
370                 message = WM_RBUTTONUP;
371                 break;
372             case 2:
373                 message = WM_MBUTTONUP;
374                 break;
375             default:
376                 message = WM_LBUTTONUP;
377                 break;
378         }
379 
380         if (isControl) {
381             flags |= MK_CONTROL;
382         }
383         if (isShift) {
384             flags |= MK_SHIFT;
385         }
386         sendMessage(hwnd, message, flags, makeLParam(x, y).intValue());
387     }
388 
389     /**
390      * .
391      * @param hwnd .
392      * @param x .
393      * @param y .
394      * @param width .
395      * @param height .
396      */
397     public final void move(final long hwnd, final int x, final int y,
398             final int width, final int height) {
399         moveTo(hwnd, x, y);
400         sizeTo(hwnd, width, height);
401     }
402 
403     /**
404      * .
405      * @param hwnd .
406      * @param x .
407      * @param y .
408      */
409     public final void moveTo(final long hwnd, final int x, final int y) {
410         HWND handle = longToHwnd(hwnd);
411         RECT rc = getRect(hwnd);
412         getUser32().MoveWindow(
413                 handle,
414                 x,
415                 y,
416                 rc.right - rc.left,
417                 rc.bottom - rc.top,
418                 true);
419     }
420 
421     /**
422      * .
423      * @param hwnd .
424      */
425     public final void restore(final long hwnd) {
426         HWND handle = longToHwnd(hwnd);
427         getUser32().ShowWindow(handle, SW_SHOWNORMAL);
428     }
429 
430     /**
431      * .
432      * @param hwnd .
433      * @param width .
434      * @param height .
435      */
436     public final void sizeTo(
437             final long hwnd,
438             final int width,
439             final int height) {
440         HWND handle = longToHwnd(hwnd);
441         RECT rc = getRect(hwnd);
442         getUser32().MoveWindow(handle, rc.left, rc.top, width, height, true);
443     }
444 
445     /**
446      * .
447      * @param hwnd .
448      * @param command .
449      * @param params .
450      * @param workingDir .
451      */
452     public final void start(
453             final long hwnd,
454             final String command,
455             final String params,
456             final String workingDir) {
457         HWND handle = longToHwnd(hwnd);
458         getShell32().ShellExecute(
459                 handle,
460                 null,
461                 command,
462                 params,
463                 workingDir,
464                 SW_SHOW);
465     }
466 }