我构建了一个简单的程序(到目前为止),它有一个大面板作为程序的“WorkArea”。我在它上面绘制一个网格,有一些功能可以将我的光标捕捉到网格上的最近点。我在窗口底部有一个状态栏,显示我在面板上的当前位置。但是,无论我滚动到哪里(假设垂直条相对于顶部是10%,水平是25%),它会显示关于实际窗口的光标位置。
我有一个处理此事件的OnMouseMove事件:
private void WorkArea_MouseMove(object sender, MouseEventArgs e)
{
GridCursor = grid.GetSnapToPosition(new Point(e.X, e.Y));
toolStripStatusLabel1.Text = grid.GetSnapToPosition(new Point(e.X, e.Y)).ToString();
Refresh();
}
它可以正常工作,我希望给出光标的点,将它绘制到正确的位置,依此类推。但是,如果我滚动,我仍然会得到相同的读数。我可以在垂直和水平滚动条中间向外滚动,将光标放在左上角,然后读取0,0,它应该是更像5000,5000(在面板上10k乘10k)
如何在一个面板中获得关于其滚动条的绝对位置?
答案 0 :(得分:1)
您需要通过滚动位置偏移位置:
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
Point scrolledPoint = new Point( e.X - panel1.AutoScrollPosition.X,
e.Y - panel1.AutoScrollPosition.Y);
..
}
请注意,AutoScrollPosition值为负值..:
如果控件,检索到的X和Y坐标值为负 已经滚动离开它的起始位置(0,0)。当你设置它 属性,您必须始终分配正X和Y值来设置 滚动位置相对于起始位置。例如,如果你 有一个水平滚动条,你将x和y设置为200,你移动 向右滚动200像素;如果你然后将x和y设置为100,那么 滚动似乎向左跳过100像素,因为您正在设置 距起始位置100个像素。在第一种情况下, AutoScrollPosition返回{-200,0};在第二种情况下,它返回 {-100,0}。
答案 1 :(得分:0)
<强>的WinForms:强>
方法Control.PointToClient允许您相对于控件本身转换鼠标位置。
<div id="table-wrapper">
<div id="table-scroll">
<table id="results" class="hidden" cellspacing=10px>
<thead>
<tr class = "spacing">
<th>SAM ID</th>
<th>Item Description</th>
<th>Issued QTY</th>
<th>Opening QTY</th>
<th>Closing QTY</th>
<th>Corrupted QTY</th>
<th>Remarks</th>
<th>NTA SAM Reference No.</th>
</tr>
</thead>
<tbody id="bResults">
</tbody>
</table>
<div id="noResults"></div>
</div>
<强> WPF:强>
使用Mouse.GetPosition,您可以获得相对于特定控件的鼠标位置:
$(".navbar-search").one('click', function(){
$.ajax({
url: "http://localhost:3000/api/queryAllRecord", // server url
type: "POST", //POST or GET
contentType: "application/json",
// data to send in ajax format or querystring format
dataType : "JSON", //dataType is you telling jQuery what kind of
response to expect
success: function(response) {
alert('success');
if(response){
var len = response.length;
var txt = "";
if(len > 0){
for(var i=0;i<len;i++){
if(response[i].samID && response[i].itemDescription){
txt += "<tr class='rowdata'>
<td>"+response[i].samID+"</td>"
+"<td>"+response[i].itemDescription+"</td>"
+"<td>"+response[i].issuedQTY + "</td>"
+"<td>"+response[i].openingQTY + "</td>"
+"<td>"+response[i].closingQTY+"</td>"
+"<td>"+response[i].corruptedQTY+"</td>"
+"<td>"+response[i].Remarks+"</td>"+"
<td>"+response[i].ntaRequestRef+"</td>"
+"<td><input class='input button-edit'
type='submit' id='edit' value='Edit' onclick
= 'edit(this)' /></td>"
+"<td><input class='button-edit'
type='button' id='delete' value='Delete'
onclick='deleteResult(this)' /></td>"+"
</tr>";
}
}
$("#bResults").empty();
if(txt != ""){
$("#results").removeClass("hidden");
$("#bResults").append(txt);
}
}
}
},
error: function(response) {
alert('error');
}
});
event.preventDefault();
});
function edit(el){
var current_id = $(el).closest('tr');
var currentTD = $(el).closest('tr').find('td'); // tds except the td which
closest to the edit button
var samId = currentTD[0].textContent;
var itemDescrip= currentTD[1].textContent;
var issueQty = currentTD[2].textContent;
var openingQty = currentTD[3].textContent;
var closingQty = currentTD[4].textContent;
var corruptedQty = currentTD[5].textContent;
var Remarks = currentTD[6].textContent;
var ntaSamRef = currentTD[7].textcontent;
var postData = { "samId": samId, "itemDescrip": itemDescrip, "issueQty" :
issueQty,"openQty" : openingQty, "closeQty" :closingQty,
"corrupQty": corruptedQty, "remarks": Remarks, "ntaReqRef":
ntaSamRef};
var postJSON = JSON.stringify(postData);
if ($(el).val() == 'Edit') {
$.each(currentTD, function () {
$(this).prop('contenteditable', true);
});
} else {
$.each(currentTD, function () {
$(this).prop('contenteditable', false);
});
}
$(el).val($(el).val() == 'Edit' ? 'Save' : 'Edit');
if($(el).val() == 'Edit' ){
$.ajax({
url: "http://localhost:3000/api/updateRecord", // server url
type: "POST", //POST or GET
contentType: "application/json", // data to send in ajax format or
querystring format
data: postJSON,
dataType : "JSON", //dataType is you telling jQuery what kind of
response to expect
success: function(response) {
alert('success');
$("#deleteresult").html("Updated Successfully");
},
error: function(response) {
alert('error');
}
});
}
}