winform 通过消息实现非客户端区域点击事件

protected override void WndProc(ref Message m)
{
      base.WndProc(ref m);

      switch (m.Msg)
       {
            case 0xA1://WM_NCLBUTTONDOWN
            case 0x84://WM_NCHITTEST
                 if (MouseButtons == MouseButtons.Left)//左键按下
                 {
                     var mousePoint = new Point((int)m.LParam);//鼠标位置(屏幕)
                     //NClientClick(mousePoint); //触发响应的事件即可
                 }
                 break;
       }
}

 private Rectangle ScreenRectangle //控件在屏幕的区域
{
     get
     {
                var r = new RECT();
                GetWindowRect(new HandleRef(this, Handle), ref r);
                var clientRect = new Rectangle(r.Left, r.Top, r.Right - r.Left, r.Bottom - r.Top);
                return clientRect;
      }
}

[DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
public static extern bool GetWindowRect(HandleRef hWnd, [In, Out] ref RECT rect);

public struct RECT
        {
            /// <exclude/>
            public int Left;
            /// <exclude/>
            public int Top;
            /// <exclude/>
            public int Right;
            /// <exclude/>
            public int Bottom;
        }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
是的,WinForm 可以通过 OWIN 框架实现 WebAPI 的开发。OWIN 是 Open Web Interface for .NET 的缩写,是一个开放式标准,允许 .NET 应用程序通过中间件来处理 HTTP 请求和响应。 具体实现步骤如下: 1. 安装 Microsoft.AspNet.WebApi.OwinSelfHost NuGet 包。 2. 创建 WebAPI 控制器。 3. 在 Program.cs 文件中,编写以下代码以启动 WebAPI: ``` using Microsoft.Owin.Hosting; using System; namespace MyWebAPI { static class Program { static void Main(string[] args) { string baseAddress = "http://localhost:9000/"; // Start OWIN host using (WebApp.Start<Startup>(url: baseAddress)) { Console.WriteLine("WebAPI started at " + baseAddress); Console.ReadLine(); } } } } ``` 4. 创建 Startup.cs 文件,并编写以下代码: ``` using System.Web.Http; using Microsoft.Owin; using Owin; [assembly: OwinStartup(typeof(MyWebAPI.Startup))] namespace MyWebAPI { public class Startup { public void Configuration(IAppBuilder appBuilder) { // Configure Web API for self-host. HttpConfiguration config = new HttpConfiguration(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); appBuilder.UseWebApi(config); } } } ``` 以上代码会将 WebAPI 的路由配置到 /api/{controller}/{id} 上。 5. 最后,在 WinForm 窗体中创建 HttpClient 对象,并使用它来调用 WebAPI: ``` using (var client = new HttpClient()) { client.BaseAddress = new Uri("http://localhost:9000/"); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); // HTTP GET HttpResponseMessage response = await client.GetAsync("api/products/1"); if (response.IsSuccessStatusCode) { Product product = await response.Content.ReadAsAsync<Product>(); Console.WriteLine("{0}\t${1}\t{2}", product.Name, product.Price, product.Category); } } ``` 以上就是通过 OWIN 实现 WinForm 中 WebAPI 的简单过程。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值