C#中调用一些API函数

来源:互联网 发布:中国银行mac企业网银 编辑:程序博客网 时间:2024/05/01 19:54

在类中写入:

            [DllImport("user32.dll")]
            private static extern int SetWindowLong(IntPtr hwnd, int nindex, int newlong);
            [DllImport("user32.dll")]
            private static extern int GetWindowLong(IntPtr hwnd, int newlong);
            [DllImportAttribute("gdi32.dll")]
            public static extern IntPtr CreateRoundRectRgn(int nLeftRect, int nTopRect, int         

             nRightRect, int nBottomRect, int nWidthEllipse, int nHeightEllipse);
            [DllImportAttribute("user32.dll")]
            public static extern int SetWindowRgn(IntPtr hWnd, IntPtr hRgn, bool bRedraw);

            [DllImport("user32.dll")]
            public static extern bool ReleaseCapture();
            [DllImport("user32.dll")]
            public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int

            lParam);

 

        [DllImport("user32")]
        public static extern int GetKeyState(int nVirtKey);   //得到键盘上的键状态

 

 

一些消息参数数值:

            const int WM_SYSCOMMAND = 0x0112;
            const int SC_MOVE = 0xF010;
            const int HTCAPTION = 0x0002;
            const int WS_CAPTION = 0x00C00000;
            const int GWL_EXSTYLE = -20;
            const int GWL_STYLE = -16;
            const int WS_EX_TOOLWINDOW = 0x0080000;
            const int WS_EX_APPWINDOW = 0x0040000;

 

           const int WM_LBUTTONDOWN = 0x0201;
           const int WM_RBUTTONDOWN = 0x0204;

API调用:

               SetWindowLong((IntPtr)f.Handle, GWL_STYLE,GetWindowLong((IntPtr)

                  f.Handle,GWL_STYLE) & ~WS_CAPTION );
                // 去掉窗体标题栏
               IntPtr rgnHwnd = CreateRoundRectRgn(0,0, f.Width, f.Height, 5, 5);
               SetWindowRgn((IntPtr)f.Handle, rgnHwnd, true);
                //给窗体切圆角 

          

           SendMessage((IntPtr)control.Handle,WM_LBUTTONDOWN,0,0); 

           SendMessage((IntPtr)control.Handle,WM_LBUTTONUP,0,0);

           //模拟一次鼠标单击

           label1.Text = (GetKeyState(20) & -1) > 0 ? "CapsLock:开" : "CapsLock:关";

            //取得CapsLock灯的状态赋值LABEL的TEXT属性

 

原创粉丝点击