作者 Anonymous [cpp] 2008-02-03 21:27 (点击下载)

  1. /*
  2. * =====================================================================================
  3. *
  4. * Filename: ee_tip.cpp
  5. *
  6. * Description: 延时5秒提醒
  7. *
  8. * Version: 1.0
  9. * Created: 2007年12月21日 11时11分53秒 CST
  10. * Revision: none
  11. * Compiler: gcc
  12. *
  13. * Author: wind (xihe), xihels@gmail.com
  14. * Company: cyclone
  15. *
  16. * =====================================================================================
  17. */
  18.  
  19. #include <gtkmm.h>
  20. #include <iostream>
  21. #define SPACING 4
  22.  
  23. using namespace std;
  24.  
  25. class MainWnd : public Gtk::Window {
  26. public:
  27. MainWnd(Glib::RefPtr<Gtk::StatusIcon> stat_,
  28. int timeout,
  29. Glib::ustring show_text_,
  30. Glib::ustring image_file_path_) ;
  31. bool on_stat_size_channged(int size);
  32. bool on_button_press_event (GdkEventButton* event);
  33. bool on_timeout();
  34. private:
  35. Glib::RefPtr<Gdk::Screen> screen;
  36. Glib::RefPtr<Gtk::StatusIcon> stat;
  37. Glib::ustring show_text;
  38. Glib::ustring image_file_path;
  39. };
  40.  
  41. MainWnd::MainWnd(Glib::RefPtr<Gtk::StatusIcon> stat_,
  42. int timeout,
  43. Glib::ustring show_text_,
  44. Glib::ustring image_file_path_)
  45. : Gtk::Window(Gtk::WINDOW_POPUP),
  46. show_text(show_text_),
  47. image_file_path(image_file_path_)
  48. {
  49. stat = stat_;
  50. stat->signal_size_changed().connect(sigc::mem_fun(this, &MainWnd::on_stat_size_channged));
  51. Gtk::HBox* hbox= Gtk::manage(new Gtk::HBox);
  52. this->add(*hbox);
  53.  
  54. Gtk::Image* image;
  55. if (image_file_path.empty())
  56. image = Gtk::manage(
  57. new Gtk::Image(Gtk::Stock::DIALOG_INFO,
  58. Gtk::ICON_SIZE_DIALOG));
  59. else
  60. image = Gtk::manage(
  61. new Gtk::Image(
  62. Gdk::Pixbuf::create_from_file(
  63. image_file_path)));
  64.  
  65. hbox->pack_start(*image, true, true);
  66.  
  67. //Glib::ustring text_str(" <span size='large' color='red' weight='bold'> " + show_text + " </span>");
  68. Gtk::Label* text = Gtk::manage( new Gtk::Label(show_text));
  69. text->set_use_markup(true);
  70. hbox->pack_end(*text, true,true);
  71. this->show_all();
  72.  
  73. Glib::signal_timeout().connect(sigc::mem_fun(*this, &MainWnd::on_timeout), timeout*1000);
  74. this->add_events(Gdk::BUTTON_PRESS_MASK);
  75. }
  76.  
  77.  
  78. bool MainWnd::on_button_press_event(GdkEventButton*)
  79. {
  80. Gtk::Main::quit();
  81. return true;
  82. }
  83.  
  84. bool MainWnd::on_stat_size_channged(int size)
  85. {
  86. //return false;
  87. Gdk::Rectangle rect;
  88. Gtk::Orientation orie;
  89. int wx = 0;
  90. int wy = 0;
  91. int ww = 0;
  92. int wh = 0;
  93. this->get_size(ww, wh);
  94. if (stat->get_geometry(screen, rect, orie)) {
  95. if (orie == Gtk::ORIENTATION_HORIZONTAL) { // 面板是水平的
  96. if (rect.get_y() < screen->get_height()/2) // 面板在上面
  97. wy = rect.get_y() + size + SPACING;
  98. else
  99. wy = rect.get_y() - wh - SPACING;
  100. wx = rect.get_x();
  101. } else {
  102. if (rect.get_x() < screen->get_width()/2) // 面板在左边
  103. wx = rect.get_x() + size + SPACING;
  104. else
  105. wx = rect.get_x() - ww - SPACING;
  106. wy = rect.get_y();
  107. }
  108. // 如果右面超出屏幕, 右对齐
  109. if ((wx + ww) > screen->get_width())
  110. wx = screen->get_width() - ww - SPACING ;
  111. // 右对齐后左面超出.. 还是左对齐好了 -_-!..
  112. if (wx < SPACING)
  113. wx = SPACING;
  114.  
  115. this->move(wx, wy);
  116. }
  117.  
  118. return false;
  119. }
  120.  
  121. bool MainWnd::on_timeout()
  122. {
  123. Gtk::Main::quit();
  124. return true;
  125. }
  126.  
  127. void on_active()
  128. {
  129. Gtk::Main::quit();
  130. }
  131.  
  132. int main(int argc, char* argv[])
  133. {
  134. //-title -text -img
  135. Gtk::Main kit(argc, argv);
  136.  
  137. string text("写点什么?");
  138. string image;
  139. char* pstr;
  140. int timeout = 5;
  141. if(argc<=1){
  142. cout << "tip 调用方法:" << endl;
  143. cout << "-text 提示内容" << endl;
  144. cout << "-image 图标全路径" << endl;
  145. cout << "-timeout 延时的秒数" << endl;
  146. exit(127);
  147. };
  148.  
  149. for(int i = 1; i < argc; i++) {
  150. if (!strcmp("-text", argv[i])) {
  151. pstr = argv[++i];
  152. if (!pstr)
  153. break;
  154. text.assign(pstr);
  155. } else if (!strcmp("-image", argv[i])) {
  156. pstr = argv[++i];
  157. if (!pstr)
  158. break;
  159. image.assign(pstr);
  160. } else if (!strcmp("-timeout", argv[i])) {
  161. pstr = argv[++i];
  162. if (!pstr)
  163. break;
  164. timeout = atoi(pstr);
  165. timeout = timeout > 0 ? timeout : 5;
  166. } else {
  167. cout << argv[0] << " " << endl;
  168. cout << "-text 提示内容" << endl;
  169. cout << "-image 图标全路径" << endl;
  170. cout << "-timeout 延时的毫秒数" << endl;
  171. exit(127);
  172. }
  173. }
  174.  
  175. try {
  176. Glib::RefPtr<Gtk::StatusIcon> stat;
  177. if (image.empty())
  178. stat =
  179. Gtk::StatusIcon::create(Gtk::Stock::DIALOG_INFO);
  180. else
  181. stat =
  182. Gtk::StatusIcon::create(
  183. Gdk::Pixbuf::create_from_file(image));
  184. stat->set_blinking();
  185. stat->signal_activate().connect(sigc::ptr_fun(on_active));
  186.  
  187. MainWnd * main_wnd = new MainWnd(stat, timeout, text, image);
  188. kit.run(*main_wnd);
  189. } catch (const Glib::Exception& e) {
  190. cout << e.what() << endl;
  191. exit(127);
  192. }
  193.  
  194. return 0;
  195. }

提交下面的校正或者修改. (点击这里开始一个新的帖子)
姓名: 在 cookie 中记住我的名字

屏幕抓图:(jpeg 或 png)