有Api的C#例子吗

解决方案 »

  1.   

    以发声Beep()为例:在MSDN上查找Beep():ms-help://MS.MSDNQTR.v90.chs/debug/base/beep.htm发现函数在Kernel32.dll内部,
    而且Beep的声明:
    BOOL WINAPI Beep(
      __in          DWORD dwFreq,
      __in          DWORD dwDuration
    );那么用C#调用时
    第一步:声明函数DllImport["Kernel32.dll"]
    bool Beep(unsigned int dwFreq, unsigned int dwDuration);具体使用就可以按照上面声明的来做了
      

  2.   

    类似QQ窗体靠边隐藏原理:class Win32API
            {
                [DllImport("user32.dll")]
                public static extern bool PtInRect(ref Rectangle r, Point p);
            }        private void timer3_Tick(object sender, EventArgs e)
            {
                System.Drawing.Point pp = new Point(Cursor.Position.X, Cursor.Position.Y);//获取鼠标在屏幕的坐标点
                Rectangle Rects = new Rectangle(this.Left, this.Top, this.Left + this.Width, this.Top + this.Height);//存储当前窗体在屏幕的所在区域
                Rectangle Rects2 = new Rectangle(this.Right, this.Top, this.Right + this.Width, this.Top + this.Height);
                if ((this.Top < 0) && Win32API.PtInRect(ref Rects, pp))//当鼠标在当前窗体内,并且窗体的Top属性小于0
                    this.Top = 0;              //设置窗体的Top属性为0,就是将窗口上边沿紧靠顶部          
                else if (this.Top > -5 && this.Top < 5 && !(Win32API.PtInRect(ref Rects, pp)))//当窗体的上边框与屏幕的顶端的距离小于5时
                    this.Top = 5 - this.Height;//将QQ窗体隐藏到屏幕的顶端            else if ((this.Left < -150) && Win32API.PtInRect(ref Rects, pp))//当鼠标在当前窗体内,并且窗体的Left属性小于0
                    this.Left = 0;
                else if (this.Left > -5 && this.Left < 5 && !(Win32API.PtInRect(ref Rects, pp)))//将QQ窗体隐藏到屏幕的左端
                    this.Left = 5 - this.Width;            else if (this.Bottom > Screen.AllScreens[0].Bounds.Height + this.Height - 30 && (Win32API.PtInRect(ref Rects, pp)))//当鼠标在当前窗体内,并且窗体底边到屏幕顶部的距离大于屏幕高度+窗体高度—30像素
                    this.Top = Screen.AllScreens[0].Bounds.Height - this.Height;//窗体紧贴屏幕下方
                else if (this.Top == Screen.AllScreens[0].Bounds.Height - this.Height && !(Win32API.PtInRect(ref Rects, pp)))
                    this.Top = Screen.AllScreens[0].Bounds.Height - 5; //窗体隐藏            else if (this.Right > Screen.AllScreens[0].Bounds.Width + 100 && (Win32API.PtInRect(ref Rects, pp)))
                    this.Left = Screen.AllScreens[0].Bounds.Width - this.Width;
                else if (this.Left == Screen.AllScreens[0].Bounds.Width - this.Width && !(Win32API.PtInRect(ref Rects, pp)))
                    this.Left = Screen.AllScreens[0].Bounds.Width - 5;
            }