(转)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。

posted @ 2008-03-23 11:01  会长  阅读(1501)  评论(10编辑  收藏  举报