一接觸到c#就被它的魅力所吸引,vb一樣快速開發(fā),c++一樣的間接語法,簡直就是完美的統(tǒng)一.呵呵.
所以,寫了個小程序,可以在桌面上飛來飛去,如果這個程序要是用vb6來做,一定會很麻煩,而且需要調(diào)用到api函數(shù).可是利用vs,僅僅需要動動鼠標,改改屬性即可,簡直太方便了!
首先,準備好小鳥的素材,最好gif動畫的,省卻代碼來實現(xiàn)動畫(但無法控制動畫速度了).
然后就是添加控件,2個timer用來控制方向和動畫,一個contextmenu實現(xiàn)右鍵菜單.(當然,你也可以不添加而是手寫代碼,但這樣就無法體現(xiàn)出快速開發(fā)的特性了.盡管ide自己生成的代碼很啰唆)
這里要用到三個比較重要的特性:
1.窗體的topmost實現(xiàn)動畫的最前端顯示
2.窗體的透明屬性(transparencykey)來實現(xiàn)非矩形外觀
3.窗體的背景圖片(backgroundimage)屬性來顯示小鳥
在方法的實現(xiàn)上要注意兩點:
1.小鳥飛到邊緣的轉向
2.鼠標移動無邊框窗體
好了,來看看具體代碼的實現(xiàn)吧:
//實現(xiàn)飛行轉向
private void timmove_tick(object sender, system.eventargs e)
  {
   if(this.left<=0)
   {
    this.movetoleft=false;
    this.backgroundimage=this.imagelist1.images[4];
    this.movex=5;
   }
   if(this.left>=system.windows.forms.screen.primaryscreen.workingarea.width-this.width)
   {
    this.movetoleft=true;
    this.backgroundimage=this.imagelist1.images[0]; 
    this.movex=-5;
   }
   if(this.top<=0)
   {
    this.movey=5;
   }
   if(this.top>=system.windows.forms.screen.primaryscreen.workingarea.height-this.height)
   {
    this.movey=-5;
   }
   this.left+=movex;
   this.top+=movey;
  }
//實現(xiàn)鼠標移動無邊框窗體
private void form1_mousedown(object sender, system.windows.forms.mouseeventargs e)
  {
   if(e.button==mousebuttons.left)
   {
    canmove=true;
    curx=e.x;
    cury=e.y;
   }
  }
  private void form1_mouseup(object sender, system.windows.forms.mouseeventargs e)
  {
   canmove=false;
  }
  private void form1_mousemove(object sender, system.windows.forms.mouseeventargs e)
  {
   if(canmove)
   {
    this.left = this.left + e.x - curx;
    this.top = this.top + e.y - cury;
   }
  }
新聞熱點
疑難解答
圖片精選