实例介绍
【实例简介】
【实例截图】
【核心代码】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 | using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using AlphaForm; #region notes // This example shows how to use two instances of AlphaForm with one being the main // window and the second being an animated drawer that slides in and out underneath // the main form. The code here could be adapted to N slide out drawers as the logic // for managing the animation and z-order issues is the same. It addresses the // following behaviours: // // 1. The main form can be top-most or not as in either case the drawer window's // ownership is set to the main form which keeps their z-order in sync. // 2. A close event to either the main form or drawer window closes both. // 3. Location changed event handlers enable dragging of both the main from and // drawer window in tandem. #endregion namespace SlidingPanes { public partial class Form1 : Form { public SlideOut m_drawer; Point m_drawerLocOld; Point m_mainLocOld; bool m_drawerOpen; bool m_lockLoc; public Form1() { InitializeComponent(); m_drawer = new SlideOut(); m_drawer.ShowInTaskbar = false ; m_drawerOpen = false ; m_lockLoc = false ; LocationChanged = new EventHandler(MainFormLocationChanged); m_drawer.LocationChanged = new EventHandler(DrawerLocationChanged); } protected override void OnShown(EventArgs e) { alphaFormTransformer1.Fade(FadeType.FadeIn, false , false , 500); base .OnShown(e); } protected override void OnClosing(CancelEventArgs e) { // We want to close both the drawer window and the main form if either // receives a close event. So we use the Owner property as a flag to // manage this. Also see OnClosing in SlideOut.cs. if (m_drawer.Owner != null ) { m_drawer.Owner = null ; m_drawer.Close(); } alphaFormTransformer1.Fade(FadeType.FadeOut, true , false , 300); base .OnClosing(e); } private void Form1_Load( object sender, EventArgs e) { // It's going to fade in, so we must set opacity to 0. alphaFormTransformer1.TransformForm(0); } public void DrawerButton_Click( object sender, EventArgs e) { int ledge; // See docs in AlphaForm on this class Impulse imp = new Impulse(7.0); int frames; int duration; bool SaveTop = TopMost; // Suppress location changed event handlers for drawer and main form m_lockLoc = true ; if (m_drawerOpen) { duration = 400; // owner must be null to guarantee drawer window will appear under // the main form's layered window. m_drawer.Owner = null ; // Force main form to topmost so that drawer is underneath TopMost = true ; ledge = m_drawer.Left; int op = 0; int uop = 0; // 30-40 fps is a reasonable median starting value (it will // be adjusted, on-the-fly, up or down to give // the greatest frame rate for the requested duration) // The only risk to setting an initially high fps is on slower // graphics displays where this routine will make a larger // reduction in frame rate to meet the desired duration. Such // on-the-fly frame rate adjustments may be perceived as jitteryness. // The inverse is also true - setting it initially too low will force // a fast graphics system to make large increases to the frame rate. // (Convergence in either case is very fast though :-)) frames = Math.Max((40 * duration) / 1000, 1); int inc = Math.Max(m_drawer.Width / frames, 1); int frameDur = duration / frames; // 0.92 figured by trial & error - because the frame of the drawer // window image includes some empty space, and I want it to slide in/out // right to the edge. If I wasn't so lazy, I'd crop the image in // Photoshop instead of doing this :-) However this illustrates an // important point. If you need to rely on some fraction of the drawer // window width or height as an animation parameter, don't use an // absolute pixel value as that won't scale for different // DPI screens. int slideW = ( int )(0.92 * m_drawer.Width); while (uop != slideW) { uop = ( int )Math.Round(imp.Evaluate(op / ( double )slideW) * ( double )slideW); if (uop > slideW) uop = slideW; DateTime sf = DateTime.Now; m_drawer.SetDesktopLocation(ledge - uop, m_drawer.Location.Y); TimeSpan ts = DateTime.Now - sf; int wait = frameDur - ts.Milliseconds; m_drawer.Update(); // force contents to update // We either wait or speed up to maintain requested duration if (wait > 0) { System.Threading.Thread.Sleep(wait); if (wait <= frameDur/2 && inc > 1) { inc = Math.Max(1, inc / 2); frameDur /= 2; } } else if (Math.Abs(wait) >= frameDur) { inc *= 2; frameDur *= 2; } op = inc; } m_drawerOpen = false ; // Hide the drawer window (it's closed - behind the main form now). // Note: We need to hide the composite window which includes the layered window. // The Visible property hides the base class property in SlideOut, and a utility function // is called to set the property for both the form and layered window (see // SlideOut.cs). m_drawer.Visible = false ; DrawerButton.Text = "Open Drawer ->" ; } else { // Setting TopMost on this form won't guarantee the drawer window will be shown // underneath, and we want it to appear behind the main form when made visible. // An easy way to achieve this is to just show the drawer window off-screen first, display, then // set TopMost on the main form to ensure the drawer is behind. m_drawer.Location = new Point(-1000000, -1000000); // Note: We need to show the composite window which includes the layered window. // The Visible property hides the base class property in SlideOut, and a utility function // is called to set the property for both the form and layered window (see // SlideOut.cs). m_drawer.Visible = true ; TopMost = true ; // The drawer window is behind (z-order) and off-screen now, so move it into position // for animating. m_drawer.Location = new Point(Location.X Width - m_drawer.Width, Location.Y (Height - m_drawer.Height) / 2); ledge = m_drawer.Left; duration = 600; // arbitrary int op = 0; int uop = 0; // 30-40 fps is a reasonable median starting value (it will // be adjusted, on-the-fly, up or down to give // the greatest frame rate for the requested duration) // The only risk to setting an initially high fps is on slower // graphics displays where this routine will make a larger // reduction in frame rate to meet the desired duration. Such // on-the-fly frame rate adjustments may be perceived as jitteryness. // The inverse is also true - setting it initially too low will force // a fast graphics system to make large increases to the frame rate. // (Convergence in either case is very fast though :-)) frames = Math.Max((40 * duration) / 1000, 1); int inc = Math.Max(m_drawer.Width / frames, 1); int frameDur = duration / frames; // See note above on where this 0.92 comes from. int slideW = ( int ) (0.92 * m_drawer.Width); while (uop != slideW) { uop = ( int )Math.Round(imp.Evaluate(op / ( double )slideW) * ( double )slideW); if (uop > slideW) uop = slideW; DateTime sf = DateTime.Now; m_drawer.SetDesktopLocation(ledge uop, m_drawer.Location.Y); TimeSpan ts = DateTime.Now - sf; int wait = frameDur - ts.Milliseconds; m_drawer.Update(); // force contents to update // We either wait or speed up to maintain requested duration if (wait > 0) { System.Threading.Thread.Sleep(wait); if (wait <= frameDur / 2 && inc > 1) { inc = Math.Max(1, inc / 2); frameDur /= 2; } } else if (Math.Abs(wait) >= frameDur) { inc *= 2; frameDur *= 2; } op = inc; } // Once drawer is displayed, we set ownership to main form so that // the two are in sync with respect to activation and close events. m_drawer.Owner = this ; m_drawerLocOld = m_drawer.Location; m_drawerOpen = true ; DrawerButton.Text = "<-- Close Drawer" ; } TopMost = SaveTop; m_lockLoc = false ; } // These location change event handlers enable synchroneous dragging of // the main form and the drawer window void DrawerLocationChanged(Object sender, EventArgs e) { bool saveLock = m_lockLoc; if (!m_lockLoc) { m_lockLoc = true ; SetDesktopLocation(Location.X m_drawer.Location.X - m_drawerLocOld.X, Location.Y m_drawer.Location.Y - m_drawerLocOld.Y); m_mainLocOld = Location; } m_drawerLocOld = m_drawer.Location; m_lockLoc = saveLock; } void MainFormLocationChanged(Object sender, EventArgs e) { bool saveLock = m_lockLoc; if (!m_lockLoc) { m_lockLoc = true ; m_drawer.SetDesktopLocation(m_drawer.Location.X Location.X - m_mainLocOld.X, m_drawer.Location.Y Location.Y - m_mainLocOld.Y); m_drawerLocOld = m_drawer.Location; } m_mainLocOld = Location; m_lockLoc = saveLock; } private void button1_Click( object sender, EventArgs e) { Close(); } } } |
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论