gr8vyguy@Blogjava

          如何在標(biāo)題欄上加一個問號按鈕

          Windows系統(tǒng)中用以激活上下文幫助的有兩種方式,
              1. F1,
              2. 標(biāo)題欄上的問號按鈕.
          helpbutton.gif

          SWT支持第一種方式。只要對一個控件加上SWT.Help事件的Listener就可以了。

          我卻沒有找到怎樣在標(biāo)題欄上加上一個問號的方法。只好借助JNI了,先查需要哪個Win32 API的函數(shù)。那是
          SetWindowLong(hWnd,  GWL_EXSTYLE,  WS_EX_CONTEXTHELP)函數(shù),其中hWnd是對話框的句柄,WS_EX_CONTEXTHELP激活了上下文問號按鈕.  然后了我們可以定義一個Java  Native的方法以及相應(yīng)的C函數(shù), 并把C函數(shù)做成一個DLL的庫, 從Java中調(diào)用SetWindowLong以激活問號按鈕。

          幸運(yùn)的是SWT已經(jīng)為我們作了大部分工作了,org.eclipse.swt.internal.win32.OS類中已經(jīng)定義了SetWindowLong函數(shù), 也就是說我們不需要自己寫JNI的接口.  當(dāng)然OS類位于一個internal的包了, internal包里的類要謹(jǐn)慎使用. 另外即使在SWT的CVS庫里也沒有定義WS_EX_CONTEXTHELP這個常數(shù), 這說明SWT目前還不支持標(biāo)題欄上的問號按鈕, 可能是因?yàn)槠渌拇翱谙到y(tǒng)下不支持的緣故.在這篇里,我只考慮Windows的情況.

          在我們寫代碼之前,需要知道WS_EX_CONTEXTHELP具體的值, 查MSDN或者用VC++里的打開定義功能, 查到WS_EX_CONTEXTHELP = 0x400.

          public class HelpButton {

              
          public static final String HELP_TEXT = "HELP_TEXT";
              
          public static final String HELP_IMAGE = "HELP_IMAGE";

              
          private Shell parent;
              
          private Shell helpShell;
              
          private Label helpImageLabel;
              
          private Label helpTextLabel;

              
          private static final int WS_EX_CONTEXTHELP = 0x400;

              
          public HelpButton(Shell shell) {
                  
          int style = OS.GetWindowLong(shell.handle, OS.GWL_EXSTYLE);
                  OS.SetWindowLong(shell.handle, OS.GWL_EXSTYLE, style
                          
          | WS_EX_CONTEXTHELP);

                  
          this.parent = shell;
                  helpShell 
          = new Shell(parent, SWT.ON_TOP | SWT.TOOL);
                  GridLayout gridLayout 
          = new GridLayout();
                  gridLayout.numColumns 
          = 2;
                  gridLayout.marginWidth 
          = 2;
                  gridLayout.marginHeight 
          = 2;
                  helpShell.setLayout(gridLayout);
                  Display display 
          = shell.getDisplay();
                  helpShell.setBackground(display
                          .getSystemColor(SWT.COLOR_INFO_BACKGROUND));

                  helpImageLabel 
          = new Label(helpShell, SWT.NONE);
                  helpImageLabel.setForeground(display
                          .getSystemColor(SWT.COLOR_INFO_FOREGROUND));
                  helpImageLabel.setBackground(display
                          .getSystemColor(SWT.COLOR_INFO_BACKGROUND));
                  helpImageLabel.setLayoutData(
          new GridData(GridData.FILL_HORIZONTAL
                          
          | GridData.VERTICAL_ALIGN_CENTER));

                  helpTextLabel 
          = new Label(helpShell, SWT.NONE);
                  helpTextLabel.setForeground(display
                          .getSystemColor(SWT.COLOR_INFO_FOREGROUND));
                  helpTextLabel.setBackground(display
                          .getSystemColor(SWT.COLOR_INFO_BACKGROUND));
                  helpTextLabel.setLayoutData(
          new GridData(GridData.FILL_HORIZONTAL
                          
          | GridData.VERTICAL_ALIGN_CENTER));

                  Listener listener 
          = new Listener() {
                      
          public void handleEvent(Event event) {
                          
          if (helpShell.isVisible())
                              helpShell.setVisible(
          false);
                      }
                  };
                  helpTextLabel.addListener(SWT.MouseDown, listener);
                  helpImageLabel.addListener(SWT.MouseDown, listener);
                  parent.addListener(SWT.MouseDown, listener);
              }

              
          public void activeHelpListener(final Control control) {
                  control.addListener(SWT.Help, 
          new Listener() {
                      
          public void handleEvent(Event event) {
                          Control control 
          = (Control) event.widget;
                          String text 
          = control.getData(HELP_TEXT).toString();
                          Object img 
          = control.getData(HELP_IMAGE);
                          
          if (img instanceof Image) {
                              helpImageLabel.setImage((Image) img);
                          }
                          helpTextLabel.setText(text);
                          Rectangle controlRect 
          = control.getBounds();
                          Display display 
          = parent.getDisplay();
                          Point pt 
          = display.map(parent, null, control.getLocation());
                          helpShell.pack();
                          helpShell.setLocation(pt.x 
          + controlRect.width / 2, pt.y
                                  
          + controlRect.height / 2);
                          helpShell.setVisible(
          true);
                      }
                  });
              }
          }

          測試程序
          public class HelpButtonTest {

              
          public static void main(String[] args) {
                  
          final Display display = new Display();
                  
          final Shell shell = new Shell(display, SWT.DIALOG_TRIM);
                  shell.setLayout(
          new RowLayout());
                  HelpButton helpButton 
          = new HelpButton(shell);
                  
                  Button button 
          = new Button(shell, SWT.PUSH);
                  button.setText(
          "Button");
                  helpButton.activeHelpListener(button);
                  button.setData(HelpButton.HELP_TEXT, 
          "Click me to get 1 million EURO.");
                  Image image 
          = new Image(display, HelpButtonTest.class
                          .getResourceAsStream(
          "information.gif"));
                  button.setData(HelpButton.HELP_IMAGE, image);
                  
                  button 
          = new Button(shell, SWT.PUSH);
                  button.setText(
          "Eclipse SWT");
                  helpButton.activeHelpListener(button);
                  button.setData(HelpButton.HELP_TEXT, 
          "Click me to get 100 million EURO.");
                  image 
          = new Image(display, HelpButtonTest.class
                          .getResourceAsStream(
          "warning.gif"));
                  button.setData(HelpButton.HELP_IMAGE, image);
                  
                  shell.setSize(
          200100);
                  shell.open();
                  
          while (!shell.isDisposed()) {
                      
          if (!display.readAndDispatch())
                          display.sleep();
                  }
                  
          if (image != null)
                      image.dispose();
                  display.dispose();
              }
          }

          helpbutton1.gif


          轉(zhuǎn)載請保留http://www.aygfsteel.com/xilaile/archive/2007/02/23/100397.html

          posted on 2007-02-23 03:09 gr8vyguy 閱讀(5363) 評論(1)  編輯  收藏 所屬分類: Java

          評論

          # re: 如何在標(biāo)題欄上加一個問號按鈕 2007-02-24 02:33 BeanSoft

          很實(shí)用的功能, 多謝!  回復(fù)  更多評論   

          <2007年2月>
          28293031123
          45678910
          11121314151617
          18192021222324
          25262728123
          45678910

          導(dǎo)航

          統(tǒng)計

          公告

        1. 轉(zhuǎn)載請注明出處.
        2. msn: gr8vyguy at live.com
        3. 常用鏈接

          留言簿(9)

          隨筆分類(68)

          隨筆檔案(80)

          文章分類(1)

          My Open Source Projects

          搜索

          積分與排名

          最新評論

          主站蜘蛛池模板: 临桂县| 滨海县| 页游| 措勤县| 梁平县| 克东县| 汨罗市| 治多县| 游戏| 景泰县| 惠安县| 隆林| 铜川市| 揭西县| 平度市| 张家口市| 房山区| 始兴县| 临汾市| 黄陵县| 芮城县| 全州县| 高雄县| 荥阳市| 原平市| 龙口市| 海南省| 深水埗区| 昭通市| 大港区| 嘉荫县| 天门市| 阿坝县| 堆龙德庆县| 颍上县| 嘉善县| 乌兰察布市| 美姑县| 庆元县| 南康市| 阿荣旗|