文件拖拽:
效果:將一個文件拖拽到窗體的某個控件時,將該控件的路徑顯示在該控件上,只要拿到了路徑自然可以讀取文件中的內容了。
將一個控件的屬性AllowDrop設置為true,然后添加DragDrop、DragEnter時間處理函數,如下:
private void txtAppPath_DragEnter(object sender, System.Windows.Forms.DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop)) { e.Effect = DragDropEffects.Link; } else { e.Effect = DragDropEffects.None; } } private void txtAppPath_DragDrop(object sender, System.Windows.Forms.DragEventArgs e) { txtLocalFileName.Text = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString(); }
圖片的縮放和拖拽:
一、實現鼠標滾輪控制圖片縮放;
1、設置PixtureBox屬性:
Dock:none
SizeMode:StretchImage
2、添加事件:
(1)設置綁定圖片路徑
private void ScrewInfoForm_Shown(object sender, EventArgs e) { //加載裝配圖紙 string drawingPath = Path.Combine(@"//192.168.2.136/PCS", productCode + ".png"); try { pbxDrawing.Load(drawingPath); } catch (Exception ex) { MessageBox.Show("加載裝配圖紙失敗,詳細:" + ex.Message, "測量", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } }
(2)添加事件1
pbxDrawing.MouseWheel += new MouseEventHandler(pbxDrawing_MouseWheel);//實現滾輪縮放 private void pbxDrawing_MouseWheel(object sender, System.Windows.Forms.MouseEventArgs e) { if (e.Delta < 0) { this.pbxDrawing.Width = this.pbxDrawing.Width * 9 / 10; this.pbxDrawing.Height = this.pbxDrawing.Height * 9 / 10; } else { this.pbxDrawing.Width = this.pbxDrawing.Width * 11 / 10; this.pbxDrawing.Height = this.pbxDrawing.Height * 11 / 10; } }
(3)添加事件2
//實現移動圖片 int xPos; int yPos; bool MoveFlag; private void pbxDrawing_MouseDown(object sender, MouseEventArgs e) { this.pbxDrawing.Focus(); MoveFlag = true;//已經按下. xPos = e.X;//當前x坐標. yPos = e.Y;//當前y坐標. } //在picturebox的鼠標按下事件里. private void pbxDrawing_MouseUp(object sender, MouseEventArgs e) { MoveFlag = false; } //在picturebox鼠標移動 private void pbxDrawing_MouseMove(object sender, MouseEventArgs e) { if (MoveFlag) { pbxDrawing.Left += Convert.ToInt16(e.X - xPos);//設置x坐標. pbxDrawing.Top += Convert.ToInt16(e.Y - yPos);//設置y坐標. } }
總結
以上所述是小編給大家介紹的c# 文件拖拽和pixturBox縮放與拖拽,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VEVB武林網網站的支持!
新聞熱點
疑難解答