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

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

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

屏幕抓图:(jpeg 或 png)