Using C # WinForm to do Windows system taskbar

Source: Internet
Author: User

Recently the company to do an examination system, need an answer bar, requirements: Occupy the upper part of the screen, and always pinned, when the other window maximized when the "answer bar" always placed before and do not block any part of the maximized window! Just like the Windows taskbar.

The end result is as follows:

First, we must declare the required structures and constants.

We declare the RECT WINAPI structure as follows:

[StructLayout (layoutkind.sequential)] struct rect{    publicint left ;      Public int top;      Public int Right ;      Public int Bottom;}

We declare the APPBARDATA SHELLAPI structure as follows:

[StructLayout (layoutkind.sequential)] struct appbardata{    publicint  cbsize;      Public IntPtr hWnd;      Public int ucallbackmessage;      Public int Uedge;      Public RECT RC;      Public IntPtr LParam;}

Then, we declare that the constants used by AppBar are as follows:

 enum  abmsg: int  {abm_new  =0  , Abm_remove, Abm_quer     YPOS, Abm_setpos, Abm_getstate, Abm_gettaskbarpos, Abm_activate, Abm_getautohidebar, Abm_setautohidebar, Abm_windowposchanged, abm_setstate}  enum  abnotify: int  {abn_statechange  =0  Span style= "COLOR: #000000", abn_poschanged, Abn_fullscreenapp, abn_windowarrange}  enum  abedge: int  {abe_left  =< Span style= "COLOR: #800080" >0   

Next, we declare the WIN32 and Shell API interfaces that need to be used:

[DllImport ("SHELL32", CallingConvention =Callingconvention.stdcall)]Static extern UINTSHAppBarMessage (intDwmessage,refappbardata pData); [DllImport ("USER32")]Static extern intGetSystemMetrics (intIndex); [DllImport ("User32.dll", exactspelling =true, CharSet =System.Runtime.InteropServices.CharSet.Auto)]Static extern BOOLMoveWindow (IntPtr hWnd,intXintYintCxintCyBOOLrepaint); [DllImport ("User32.dll", CharSet =CharSet.Auto)]Static extern intRegisterWindowMessage (stringmsg);

Next, we create a method to register the AppBar.

Private voidRegisterbar () {Appbardata abd=NewAppbardata (); Abd.cbsize=marshal.sizeof (ABD); Abd.hwnd= This.    Handle; if(!fbarregistered) {Ucallback= RegisterWindowMessage ("Appbarmessage"); Abd.ucallbackmessage=Ucallback; UINTret = SHAppBarMessage ((int) Abmsg.abm_new,refAbd); Fbarregistered=true;    Absetpos (); }    Else{shappbarmessage (int) Abmsg.abm_remove,refAbd); Fbarregistered=false; }}

Finally, we create a way to set the AppBar location.

Private voidAbsetpos () {Appbardata abd=NewAppbardata (); Abd.cbsize=marshal.sizeof (ABD); Abd.hwnd= This.    Handle; Abd.uedge= (int) Abedge.abe_top; if(Abd.uedge = = (int) Abedge.abe_left | | Abd.uedge = = (int) {abedge.abe_right) {abd.rc.top=0; Abd.rc.bottom=SystemInformation.PrimaryMonitorSize.Height; if(Abd.uedge = = (int) {abedge.abe_left) {Abd.rc.left=0; Abd.rc.right=Size.width; }        Else{abd.rc.right=SystemInformation.PrimaryMonitorSize.Width; Abd.rc.left= Abd.rc.right-Size.width; }    }    Else{abd.rc.left=0; Abd.rc.right=SystemInformation.PrimaryMonitorSize.Width; if(Abd.uedge = = (int) {abedge.abe_top) {abd.rc.top=0; Abd.rc.bottom=Size.Height; }        Else{Abd.rc.bottom=SystemInformation.PrimaryMonitorSize.Height; Abd.rc.top= Abd.rc.bottom-Size.Height; }    }    //Query The system for a approved size and position.SHAppBarMessage ((int) Abmsg.abm_querypos,refAbd); //Adjust the rectangle, depending on the edge to which the//AppBar is anchored.    Switch(Abd.uedge) { Case(int) ABEdge.ABE_LEFT:abd.rc.right= Abd.rc.left +Size.width;  Break;  Case(int) ABEdge.ABE_RIGHT:abd.rc.left= Abd.rc.right-Size.width;  Break;  Case(int) ABEdge.ABE_TOP:abd.rc.bottom= Abd.rc.top +Size.Height;  Break;  Case(int) ABEdge.ABE_BOTTOM:abd.rc.top= Abd.rc.bottom-Size.Height;  Break; }    //Pass The final bounding rectangle to the system.SHAppBarMessage ((int) Abmsg.abm_setpos,refAbd); //Move and size the AppBar so, it conforms to the//bounding rectangle passed to the system.MoveWindow (Abd.hwnd, Abd.rc.left, Abd.rc.top, Abd.rc.right-Abd.rc.left, Abd.rc.bottom-abd.rc.top,true);}

Reference Address: Http://www.codeproject.com/Articles/6741/AppBar-using-C

: http://pan.baidu.com/s/1qY4HX2c B7NV

Using C # WinForm to do Windows system taskbar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.