------ <a href="" target="blank">Windows Phone 7手机开发</a>、<a href="" target="blank">.Net培训</a>、期待与您交流! -------

1. C#窗体加边框阴影效果

const int CS_DropSHADOW = 0x20000;

const int GCL_STYLE = (-26);

[DllImport("user32.dll", CharSet = CharSet.Auto)]

public static extern int SetClassLong(IntPtr hwnd, int nIndex, int dwNewLong);

[DllImport("user32.dll", CharSet = CharSet.Auto)]

public static extern int GetClassLong_r(IntPtr hwnd, int nIndex);

SetClassLong(this.Handle,GCL_STYLE,GetClassLong_r(this.Handle,GCL_STYLE)|CS_DropSHADOW);

2. 开启屏幕保护功能

[DllImport("user32.dll",EntryPoint= SystemParametersInfo)]

public static extern int SystemParametersInfo(int uAction,bool uParam,StringBuilder lpvParam,int fuWinIni);

public const int SPI_SETDRAGFULLWINDOWS=37; 

SystemParametersInfo(SPI_SETDRAGFULLWINDOWS,true,null,0);

3. Public class ModeConvertHelper<T> where T:new(),泛型约束,约束类型T必须具有无参的构造函数。

4. 可以通过FormDialogResult属性将它改造成MessageBox

5. [Obsolete(该方法不再使用!)],标记不再使用的程序元素。

6. 事件的addremove

Public event Delegate EventName

{

add{base.Events.AddHandler(key,value);}

remove{base.Events.RemoveHandler(key,value);}

}

C#编译器为事件的addremove方法增加[MethodIMpl(MethodImplOpetions.Synchronized)]属性。这个属性的目的是为确保在操作实例的事件成员时,对于任何一个对象,在同一时刻只能有一个add方法或者remove方法可以执行。该属性同样确保在操作静态事件成员时,同一时刻也只能有一个add方法remove方法可以执行。这里需要线程同步,以免委托对象的链表被破坏。

7. 显示/隐藏任务栏窗口

[DllImport(user32.dll,EntryPoint=FindWindowEx,SetLastError=true)]

Static extern InPtr FindWindowEx(IntPtr hwndParent,IntPtr hwndChildAfter,string lpszClass,string lpszWindow);

[DllImport(user32.dll,EntryPoint=ShowWindow,SetLastError=true)]

Static extern bool ShowWindow(IntPtr hWnd,unit nCmdShow);

//IntPtr trayHwnd=FindWindowEx(IntPtr.Zero,IntPtr.Zero,Shell_TrayWnd,null);

IntPtr trayHwnd=FindWindow(Shell_Tray_Wnd,null); 

If(trayHwnd!=IntPtr.Zero)

{

ShowWindow(trayHwnd,0);

}

8. 获取指定键的状态,按下时返回键值,不按下时返回10

[DllImport(user32.dll)]

public static extern short GetKeyState(int nVirtKey);

9. 获取光标的位置

[DllImport(user32.dll)]

[return:MarshalAs(UnmanagedType.Bool)]

public static extern bool GetCursorPos(ref Point lpPoint);

10. 获取指定窗口的边框矩形的尺寸,该尺寸以相对于屏幕坐标坐上角的屏幕坐标给出。

[DllImport(user32.dll)]

[return:MarshalAs(UnmanagedType.Bool)]

public static extern bool GetWindowRect(IntPtr hWnd,ref Rect lpRect);

11. 获取窗口客户区的坐标。

[DllImport(user32.dll)]

[return:MarshalAs(UnmanagedType.Bool)]

public static extern bool GetClientRect(IntPtr hWnd,ref Rect lpRect);

12. 判断指定的点是否位于矩形内部 

[DllImport(user32.dll)]

[return:MarshalAs(UnmanagedType.Bool)]

public static extern bool PtInRect([In] ref Rect lprc,Point pt);

[In]指示应将数据从调用方封送到被调用房,而不返回到调用方。

13. System.Diagnostics.Stopwatch

提供一组方法和属性,可用于准确地测量运行时间。通过StartStop方法可以获取这两个方法的调用时间间隔。

posted on 2013-08-08 20:29  瑞雪的情  阅读(252)  评论(0)    收藏  举报