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

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

None.gif private   bool  centered  =   true ;
None.gif
ContractedBlock.gifExpandedBlockStart.gif    
Native Platform Invoke #region Native Platform Invoke
InBlock.gif
InBlock.gif    [DllImport(
"coredll.dll")]
InBlock.gif    
private static extern UInt32 SetWindowLong(IntPtr hWnd, int nIndex, UInt32 dwNewLong);
InBlock.gif
InBlock.gif    [DllImport(
"aygshell.dll")]
InBlock.gif    
private static extern int SHDoneButton(IntPtr hwndRequester, UInt32 dwState);
InBlock.gif
InBlock.gif    
private readonly int GWL_STYLE = (-16);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
private readonly UInt32 WS_CAPTION = 0x00C00000;     /**//* WS_BORDER | WS_DLGFRAME  */
InBlock.gif    
private readonly UInt32 WS_BORDER = 0x00800000;
InBlock.gif    
private readonly UInt32 WS_POPUP = 0x80000000
InBlock.gif
InBlock.gif    
private readonly UInt32 SHDB_SHOW = 0x0001;
InBlock.gif    
private readonly UInt32 SHDB_HIDE = 0x0002;
InBlock.gif
ExpandedBlockEnd.gif    
#endregion

None.gif
None.gif    
None.gif
None.gif    
public   bool  CenterFormOnScreen
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif      
get
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        
return centered;
ExpandedSubBlockEnd.gif      }

InBlock.gif      
set
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        centered 
= value;
InBlock.gif
InBlock.gif        
if (centered)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif          CenterWithinScreen();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif      }

ExpandedBlockEnd.gif    }

None.gif
None.gif    
protected   override   void  OnLoad(EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif      
// By default if you set a form's size within
InBlock.gif      
// the Visual Studio form designer it won't
InBlock.gif      
// take into account the additional height of
InBlock.gif      
// the caption, so we'll add that height heredot.gif
InBlock.gif
      this.Height += SystemInformation.MenuHeight;
InBlock.gif
InBlock.gif      
base.OnLoad(e);
InBlock.gif
InBlock.gif      
// Add the border and caption we removed from the form
InBlock.gif      
// when we set the Form's FormBorderStyle property to None.
InBlock.gif      
// We do this at the Win32 API level, which causes the .NET
InBlock.gif      
// Compact Framework wrapper to get out of sync.
InBlock.gif
      uint style = WS_BORDER | WS_CAPTION | WS_POPUP;
InBlock.gif      SetWindowLong(Handle, GWL_STYLE, style);
InBlock.gif
InBlock.gif      
// Add/Remove an [OK] button from the dialog's
InBlock.gif      
// caption bar as required
InBlock.gif
      SHDoneButton(Handle, ControlBox ? SHDB_SHOW : SHDB_HIDE);
InBlock.gif
InBlock.gif      
// Center the form if requested
InBlock.gif
      if (centered)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        CenterWithinScreen();
ExpandedSubBlockEnd.gif      }

ExpandedBlockEnd.gif    }

None.gif
None.gif    
protected   override   void  OnResize(EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif      
base.OnResize(e);
InBlock.gif
InBlock.gif      
// If the dialog changes size and we want to be
InBlock.gif      
// centered we may need to move the dialog to
InBlock.gif      
// keep it centered.
InBlock.gif
      if (centered)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        CenterWithinScreen();
ExpandedSubBlockEnd.gif      }

ExpandedBlockEnd.gif    }

None.gif
None.gif    
protected   override   void  OnKeyDown(KeyEventArgs e)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif      
base.OnKeyDown(e);
InBlock.gif
InBlock.gif      
// If we have an [OK] button in the caption pressing
InBlock.gif      
// Return or Escape should close the dialog
InBlock.gif
      if (this.ControlBox)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        
if (e.KeyCode == Keys.Return || e.KeyCode == Keys.Escape)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif          
this.DialogResult = DialogResult.OK;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif      }

ExpandedBlockEnd.gif    }

None.gif
None.gif    
private   void  displayRotationState_Changed( object  sender, ChangeEventArgs args)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif      
// If the orientation has changed and the CenterFormOnScreen
InBlock.gif      
// property is set re-center the form
InBlock.gif
      if (centered)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        CenterWithinScreen();
ExpandedSubBlockEnd.gif      }

ExpandedBlockEnd.gif    }

None.gif
None.gif    
private   void  CenterWithinScreen()
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif      
// Move the position of this form to center it within the
InBlock.gif      
// working area of the desktop
InBlock.gif
      int x = (Screen.PrimaryScreen.WorkingArea.Width - this.Width) / 2;
InBlock.gif      
int y = (Screen.PrimaryScreen.WorkingArea.Height - this.Height) / 2;
InBlock.gif
InBlock.gif      
this.Location = new Point(x, y);
ExpandedBlockEnd.gif    }

  可能用到的程序集:
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。

转载于:https://www.cnblogs.com/zzy0471/archive/2008/03/23/1118344.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值