• (转)windows mobile 开发中使用C#编写非充满屏幕的Form


          用vs开发windows mobile 时无法用其自身提供的控件来编写一个类似于MessageBox的小窗体,本人寻找多日,(参考原文地址:http://www.christec.co.nz/blog/archives/134)终于找到解决方法。步骤如下
          1,将Form属性中的 FormBorderStyle 设为none;
          2,将Size 设置为你想要的大小;
          3,在Form类中加入以下代码;

    private bool centered = true;

        
    Native Platform Invoke

        

        
    public bool CenterFormOnScreen
        
    {
          
    get
          
    {
            
    return centered;
          }

          
    set
          
    {
            centered 
    = value;

            
    if (centered)
            
    {
              CenterWithinScreen();
            }

          }

        }


        
    protected override void OnLoad(EventArgs e)
        
    {
          
    // By default if you set a form's size within
          
    // the Visual Studio form designer it won't
          
    // take into account the additional height of
          
    // the caption, so we'll add that height here
          this.Height += SystemInformation.MenuHeight;

          
    base.OnLoad(e);

          
    // Add the border and caption we removed from the form
          
    // when we set the Form's FormBorderStyle property to None.
          
    // We do this at the Win32 API level, which causes the .NET
          
    // Compact Framework wrapper to get out of sync.
          uint style = WS_BORDER | WS_CAPTION | WS_POPUP;
          SetWindowLong(Handle, GWL_STYLE, style);

          
    // Add/Remove an [OK] button from the dialog's
          
    // caption bar as required
          SHDoneButton(Handle, ControlBox ? SHDB_SHOW : SHDB_HIDE);

          
    // Center the form if requested
          if (centered)
          
    {
            CenterWithinScreen();
          }

        }


        
    protected override void OnResize(EventArgs e)
        
    {
          
    base.OnResize(e);

          
    // If the dialog changes size and we want to be
          
    // centered we may need to move the dialog to
          
    // keep it centered.
          if (centered)
          
    {
            CenterWithinScreen();
          }

        }


        
    protected override void OnKeyDown(KeyEventArgs e)
        
    {
          
    base.OnKeyDown(e);

          
    // If we have an [OK] button in the caption pressing
          
    // Return or Escape should close the dialog
          if (this.ControlBox)
          
    {
            
    if (e.KeyCode == Keys.Return || e.KeyCode == Keys.Escape)
            
    {
              
    this.DialogResult = DialogResult.OK;
            }

          }

        }


        
    private void displayRotationState_Changed(object sender, ChangeEventArgs args)
        
    {
          
    // If the orientation has changed and the CenterFormOnScreen
          
    // property is set re-center the form
          if (centered)
          
    {
            CenterWithinScreen();
          }

        }


        
    private void CenterWithinScreen()
        
    {
          
    // Move the position of this form to center it within the
          
    // working area of the desktop
          int x = (Screen.PrimaryScreen.WorkingArea.Width - this.Width) / 2;
          
    int y = (Screen.PrimaryScreen.WorkingArea.Height - this.Height) / 2;

          
    this.Location = new Point(x, y);
        }

      可能用到的程序集:
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    using Microsoft.WindowsMobile.Status;
    using Microsoft.WindowsCE.Forms;
    using Microsoft.WindowsMobile;

      由于是在网吧,没有环境,可能有写错的地方。如果还是看不明白,打开这个链接看看http://www.christec.co.nz/blog/archives/134。最好是用你的Form来继承此Form。

  • 相关阅读:
    升级Nginx1.14.1以上版本
    MaxScale中间件部署数据库读写分离
    php文件锁解决少量并发问题
    使用mysql悲观锁解决并发问题
    配置和查看composer镜像
    PHP常用的 五种设计模式及应用场景
    全球免费公共 DNS 解析服务器 IP 地址列表推荐 (解决无法上网/加速/防劫持)
    九种跨域方式实现原理
    Hadoop中RPC协议小例子报错java.lang.reflect.UndeclaredThrowableException解决方法
    DataNode启动不成功——java.net.BindException: Port in use: localhost:0 Caused by: java.net.BindException: Cannot assign requested address解决办法
  • 原文地址:https://www.cnblogs.com/zzy0471/p/1118344.html
Copyright © 2020-2023  润新知