在網上淘了文章後,我想出了這種設計的基於WinForms的觸摸屏應用程序,需要智能手機如滾動。該應用程序本身將運行在平板電腦或觸摸屏桌面上。如何讓智能手機像滾動的WinForms觸摸屏應用程序(滾動面板)
- 我把我想要在面板上滾動的所有東西。
- 設置自動滾屏爲true(這將顯示滾動條)
- 現在把這個整個面板組框
- 內收縮組框,直到滾動條隱藏的(視覺上隱藏的,不可見= FALSE)
現在,我陷入了一個有趣的部分。我想我必須處理mousedown,mouseup & mousemove在面板上設置自動滾動位置,這樣當有人觸摸面板並拖動它時,它就是滾動魔術。請幫助填寫以下方法存根的幾行代碼。自動滾動位置上的msdn doc非常令人困惑,因爲它返回負數,但需要用abs設置爲正,而不是。
Point mouseDownPoint;
Point mouseUpPoint;
Point mouseDragPoint;
private void myPanel_MouseDown(object sender, MouseEventArgs e)
{
this.mouseDownPoint = e.Location;
Console.WriteLine("Mouse down at {0}", e.location);
}
private void myPanel_MouseUp(object sender, MouseEventArgs e)
{
this.mouseUpPoint = e.Location;
this.mouseDownPoint = new Point(); //will set for IsEmpty check
Console.WriteLine("Mouse Up at {0}", e.location);
}
private void myPanel_MouseMove(object sender, MouseEventArgs e)
{
Console.WriteLine("Mouse at {0}", e.location);
if (mouseDownPoint.IsEmpty()) //finger is off the touchscreen
return;
myPanel.Autocrollposition = ??
}
謝謝
//更新 - 下面我有試錯的工作&測試代碼。 (不重構)。如果有人有更優雅的解決方案,請發佈。
Point mouseDownPoint;
private void innerpanel_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
this.mouseDownPoint = e.Location;
}
private void innerpanel_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left)
return;
if ((mouseDownPoint.X == e.Location.X) && (mouseDownPoint.Y == e.Location.Y))
return;
Point currAutoS = innerpanel.AutoScrollPosition;
if (mouseDownPoint.Y > e.Location.Y)
{
//finger slide UP
if (currAutoS.Y != 0)
currAutoS.Y = Math.Abs(currAutoS.Y) - 5;
}
else if (mouseDownPoint.Y < e.Location.Y)
{
//finger slide down
currAutoS.Y = Math.Abs(currAutoS.Y) + 5;
}
else
{
currAutoS.Y = Math.Abs(currAutoS.Y);
}
if (mouseDownPoint.X > e.Location.X)
{
//finger slide left
if (currAutoS.X != 0)
currAutoS.X = Math.Abs(currAutoS.X) - 5;
}
else if (mouseDownPoint.X < e.Location.X)
{
//finger slide right
currAutoS.X = Math.Abs(currAutoS.X) + 5;
}
else
{
currAutoS.X = Math.Abs(currAutoS.X);
}
innerpanel.AutoScrollPosition = currAutoS;
mouseDownPoint = e.Location; //IMPORTANT
}
對於訪問該網站的任何人如何解決代碼問題,發佈答案不應該提出一個新問題。如果您在使用代碼段時遇到問題,請嘗試提問。 –