C#学习笔记-icon托盘图标的简单知识

在做整个类似QQ的毕业设计中,有一个小图标把我给难了一阵子,就是托盘小图标aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAB8AAAAaCAIAAADAARDdAAACzElEQVRIiWNonLKKdohh1PRR02lkenpph1dAnLqqvrKippqanotXyP/YU/8Zj1HB9LD4YivrAF1dByUFbSV5TUV5dUV5tf+Mx/4zHtsatpUi0zPLO42MXR2dI75WnP+/6x4TE9P/Xfe+VpxXlFf9z3jsv/s1PD4gbLq7T7SmuoWdjfv///+ZmJjyG6cwMTH9//9fSV5VVVkD4gOcpsfn1uI3XUleS0le283V/+jBPRAL/v//f/TgHiV5VQ01HUsbF3wh4+wTjs/0Scsd5ZWV5LVfdHL8Py/x/5rZ//Ma//cKvl7O9r+FSUdRxcrADJ/psVlVeKRbuhfskErbwce/SIrnVzfb//1C/9cw/5/B8rODZaOKxkFV91m6RvhML++Yi0c6P6VolqzXLdZZq7nd6ty1Z6uLz9QRnK0lvkpX8iF723+FbXtkasMTinCa3tc6E5dcSmGLjrZFvYrjf939kNhDQRa7fzsv1pDTNDKyTC5owm76zIpOXKZ7ByUpy+u62Tt/3XkLYuKfrMf/Su7967n2b8WNP9ffvnr7UV5aTV/f0sUTe+Qx7PWLxGW6qpKhsrxueUbcz2fPIKZ/+/b9x4+fv3///vfv37efv1+8+xJhoykvo2Js4kSy6cryen6h6YubfP98/vKufM/nsP3/YeDv33/vP39/+fptT6ZnRmmHmaUbdtPnFrfgMr2mb3FN35KtXWaXbz/+/OXbr1+//////+/f/1+//3z88uPWk7ePHj463m5YP2k5ObHaOGXVko6sM5O07y/zPXHp7ot3n7/9+P3528/XH77effbu8NGjd2aZnenWwZci8cg1Tll1vFDh/53Af6clrnY6HppXtmXL9pMnzx7euOjZ+sBPOyT/3wm8VCFzNF+RTNMXVMSvL7DeU2iwI8dgfZb9guq4ht4FC8sjt6frb84zu1Cnu7/CdlOVB5mmU4iGsukATBRbRT+vyaEAAAAASUVORK5CYII=" alt="" />。

要完成的功能大约是,窗口选择关闭时变成最小化到托盘图标,双击托盘图标显示托盘图标。

还是以登录窗口为例子

要有托盘图标首先需要一个notifyIcon,这个在工具箱中找。然后就是notifyic的相关属性的设置了,首先需要一个icon图标,我是直接在网上随便翻的,不知道翻到了哪位大神的图标,不好意思,就让我暂时用一下啦,我保证我的水平还没办法用作商业用途T^T,aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAALEAAAAaCAIAAAB3kCq/AAAD8UlEQVRoge2aa0/TUBjH2Xuj32DgtqLIVSJERE0Ihs1PYWL0RasxgniJhAGTAKPtGMi2sjHHOhh2cOROuEgIKGAisUYxIcb4HfoNfHG69QZ0DHBKT/N/sXPSnTz9P7+e85yz5RQXl0FhWD6SVgZ0JgcxcbAM6AxiQkcGdAYxoSMDOiMxgYQEheYJHRnQGcSEjqAzOUe4sv4IhxViQkcpJmDzUlHpzu7P3V+/61u7ppZWJxZWQqNgdmU9wHLUwJCT9i28X+W/74C5JdFfxMTpk4oJi9Xm9oc9IZaJJZbXt+ITc/HJuaX1rQDLLa1v9kVGnLSvm4kMjgLRX8TE6ZOKCRt2wUn7vOFYIMYlZhcj3AQ3s5CYXQzEOGY44aR9TtrX4vG7ehnR31PDhJ3kBUEQBAHg2Q8xu5IzYbFYm7q8MPEd/YN+lmOGx/ws1xcZae0JwH4oKhgV/U2DCRwIigsQRw3bTvFHGGffeQIHAk86sp6SrEvORGn5lUZXt8lkOnP2nJyAlMoqq0wm050H9T3hmOivHhM4EAQB4HIyjs4EJmKRWQYREzpSMFFZfbfRmSKgmepvaO181k5KWFD9j5pcbV7m/os20d+DmVBkjgCCyIesKadE1qO6jafsSaZSWUvRdkJMSNEoH0AbH0+S6uD+a8mZqK6pa38ddPUyqpUiVUa0eZluJtIfHW1Lr55Qpk3OBKFYTwCh7lHdyfO8Nh2ZThXpMEEA9dDKHhwo4oNQ2yle4El79pN6jEzYbBhe/zQ0Cpjhsd7IiCfE0iGWCkbpEOsND/tjXDAOomCGBTMll8vTYCL5imuZgC+9fBGBPcmbxRbpSH4LWu0g+dRnZTqOmQkcyOLO36vHQfKwGiWAFJCD5E8bE1CFxSX3Hj4OcxPR8eno+DSEIApmImNTg2/f3ayts1iskr+6TEg5k5iANb68wLeL2XZomgqwcPAPMQHjMAQTUFYrtvLx08bnLxTzpsntfen2RhKTLrdH7W9G84S479POE8kesYWrB/lbTGAEkJglyCSbirVDjMlATBQUlWxuf93+9mNhbQPML7d4/ETDc4vFWlF13Wq1pceEop5Q7jsOW0/sxcTx1hPq8wm431WXk/vUmMZgAsPy51c/LK5tJGYXh8amWjz+J82uPfw9eN8hQqFlQu55evsODRN2khcyKunQOaaODmCiudMbioNgfJwOsq96B5xd9K262y0d7p5ASPJX53zCIVYOxx95hgsHhpjQlZaJ3Nw8sznXbDYXFBYHYomCopILFwtu1NRWXL1mTl55eedFf/XPMU8CC1WlcjghJnSEfitHUsuAzqD/3iGpheYJHRnQGcSEjgzozB/7SZrbd7NQ+AAAAABJRU5ErkJggg==" alt="" />也就是这个地方设置一下icon图标就好叻。

如果需要右键单击托盘图标并显示一些选项和操作的话,这里还需要在窗口中添加contextmenustrip控件(也就是右键快捷方式的控件)了,这个也可以在工具箱中找到。aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAJwAAABcCAIAAAAUMkuuAAAK6UlEQVR4nO2d7XPTyB3H/feoXJ7ATmzryXmyHAIcWJRCSOKQHEx50xm4K0nA9q/0zV37sq+unb66IT43dxCH67WG45ibSxEBckmaUfuqc+2403au7cyZowFiS+oLWdJKlpyHi2Sf2O9oGGmlfdB+9ru7Wm+GwNd1Ku+fnrakvtknPds//W9fFcBQfQgVsLyVF1CrWB4KQ/WhMFQfyiOoFSwPhaG2pNYzRIDIrO8xdotAzfEBTXxuj6+yd61niEDAphJzvG3wHmW84/av+J2HmuNN75njd/826xlit40BjbKeIQIEQVjyVVHvE9QcH0CzU0/3UOwdqelQ0bfds/YBKp+xpLGeIYhMht8fqOsZa5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GG4vp6h2J0gqEHWYpj6EGG4hh6kKUTLBNX/40xQ72xRIwZYpk4FR0kIwORSL96RMP90XB/NNJLRvuoSB8V6WOpfiYS66X6++gBJsKy0Vg0RFLdUTpM0WHq/6SN/uUb6JqGAAAAAElFTkSuQmCC" alt="" />再设置需要拥有的功能就好叻~

上面的都完成了不代表就搞定了,他们两个属于两个陌生人,他们彼此都是独立的个体,要想点击图标出现右键还需要硬拉关系~大家多走动走动就熟悉了嘛~

这里的重要一步就是,notifyicon中的aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAANMAAAAaCAIAAACigv7yAAADxklEQVR4nO2aS5LjIAyG+zI9O1flBr0bfBV8FXwVzqKzcINZ8NADAXHijGdcdGnhkCB+pA8BSX99f/+aRm1ZHpdruKUty+Pn5/eyPKJ9ladp0z5tjLzLl8K/ZsuseR8L7CRvEKDLNdzSJnnjAF2u4ZY2yRsH6HINt7RJ3jhAl2v432zzIYTg7SiwHfKiixBCCH776xNYHYQQwBltYrCbM8MUQ6UH6Nx5GQfapI52LLJpgmPExlk/ZmaHgkEW0J3Fm+RZz5JhPbj1oOjVgZ7O57qsDgIAiHFNjO455G0+0OFisJjs08mzXl1ORztqCwYROY88hh0KeHkWxVrk0ZS8bCeQ590OclmDc/4c8swOip8+efU+QFpQai7M1ueMrTlhze7sM/Hd+AJ2JztG/wBkO4p1KDbZSm1ewLqwRFhyFWunt0UADuGdqWaR+6ZiRWtefAt2m6txqSMN8mwnta1Ag3N0MuRjyZWMgnFQPFgfgve8SyLAepSbhDF5anCFmBiCrBaf6wXGZFfk0bkTAtgfek4YkPaKPNnd2zIKOIMp1DuC98jZ6kC0tJ1Xwgbk8fKmk5d8auSxv+TqKHmbD5wDOsmo2+yQvNPisXlUj+2JKhyuqnmWjkJakObarSZGJ69EVp6W9JrHCwCtSYakhCw8jIO+T1GHZocSW5LbokTZbXMt8TZ197ZRrtB5Q1iLPDw7tsUkvMRJgJJH6kue40HyZDtNNiWgIs+K5ceLvwYleV4dcEDpg+JWE9MiD/cpFneVPFPSzFvKUiQv2TWI5ok+G1dVBJZ7dpxVyTN5meUNBKFpOG8Ia5Ini1wpGZI8XJDKbmuqcHXOedr5USEvfuwJ8tpFdERe6u48KQkCQenzGHlFSQG9Q55S83JLeoWb2lPkKVc3kuryboO81A4QZUjyKudd8ti5KBVOesssDo+Rx8tndNi828prc7rbVrutvuupuy2t1Vh+wa3GQW+3FZOR49ZuW+TRfOe4sHjVst8857XIa3Wn5zN+YFI78sJflk3jnNcX1phItauQmpfFjMmrZfS/z2NFdtOiz28Ykjz+DQgRgeoJcHEI0oURYBygBlrqKrcDMekUnpTTLYmNFdQbBg/lM3fbulwVD+QERrqnwkAuYVUA5aZJAeUbpXTeFYYEEyccHeUbnGfIw7s51rL5G8bA5m8Ybxk/54nATvJ6Nsl7yyZ5L9sk7y2b5L1sk7zPBXb+N/y0a2zWvMHSvFzDLW2SNw7Q5RpuaZO8cYAu13BLm+SNA3S5hlvaJE+Y/J/CSd6HTJD3B0hyOv58xn+6AAAAAElFTkSuQmCC" alt="" />需要关联刚刚建立的右键快捷方式。

然后我就是想将关闭窗口变成最小化到托盘,这个就需要窗体的formclosing事件了。

将notifyicon的visible属性设置为true(也就是可见),那正常情况下都不会关闭的,所以这个时候将关闭变成最小化就只是需要1、取消关闭;2、窗口最小化。

1         #region 关闭用户界面即最小化到托盘图标界面
2 private void Panel_FormClosing(object sender, FormClosingEventArgs e)
3 {
4 // 取消关闭窗体
5 e.Cancel = true;
6 // 将窗体变为最小化
7 this.WindowState = FormWindowState.Minimized;
8 }
9 #endregion

最后一点就是双击托盘图标显示用户界面

这个很明显就是icon的属性了,上面的功能基本是窗口的属性,所以他的formclosing事件在窗口中找,现在是icon的事情,那只有在notifyicon的属性里面找是否有相关的功能实现的事件了,也就是aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAALAAAAAcCAIAAABOC6KcAAADdklEQVRoge2bzY3zIBCGt5nsLVK6cE6W27APKH04EkohSLgScCnTw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7EKxI6A+PKhZFIViP0B4f6/oQJRtbXqS7ZFOo5nKhBFOo5nKhBFOo5n/gGJvnqxMITkngAAAABJRU5ErkJggg==" alt="" />mousedouble咯~

 1         #region 双击托盘图标显示用户界面
2 private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
3 {
4 if (WindowState == FormWindowState.Minimized)
5 {
6 //还原窗体显示
7 WindowState = FormWindowState.Normal;
8 //激活窗体并给予它焦点
9 this.Activate();
10 }
11 }
12 #endregion

(2016-04-07)

上一篇:【Java】Java原生的序列化和反序列化


下一篇:java中的序列化与反序列化,还包括将多个对象序列化到一个文件中