使用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#region Native Platform Invoke

    [DllImport("coredll.dll")]
    private static extern UInt32 SetWindowLong(IntPtr hWnd, int nIndex, UInt32 dwNewLong);

    [DllImport("aygshell.dll")]
    private static extern int SHDoneButton(IntPtr hwndRequester, UInt32 dwState);

    private readonly int GWL_STYLE = (-16);

    private readonly UInt32 WS_CAPTION = 0x00C00000;     /**//* WS_BORDER | WS_DLGFRAME  */
    private readonly UInt32 WS_BORDER = 0x00800000;
    private readonly UInt32 WS_POPUP = 0x80000000;

    private readonly UInt32 SHDB_SHOW = 0x0001;
    private readonly UInt32 SHDB_HIDE = 0x0002;

    #endregion

   

    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。
转自http://www.sf.org.cn/windowsmobile/WindowsMobileDev/20923.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值