i presently learning c# , have chosen project write simple colour picker control ; things have changed substantially since last looked @ writing code , have struck problem.
i using mousedown event in control mouse coordinates point - working fine , returns expect - mouse coordinates relative control; when try , check against control location returned value point showing position of control relative form in cases mouse coordinates outside bounds of because values less relative start position of control i.e click @ pixel 1,1 in control - mouse position 1,1 because control located @ 9,9 relative form location of mouse less bounds of control - have absolutely no idea how fix this, have been attempting sort out pointtoclient , pointtoscreen no avail seem come outlandish values can please me out it's driving me insane.
i've managed sort out myself thought i'd post answer , can else. having problems getting point values relative same pixel origin : sorted it.
private void colourpicker_mousedown(object sender, mouseeventargs e) { // being paranoid worried scaling issues, this.location // return same result mess may not handle // scaling <i haven't checked> point controlcoord = this.pointtoclient(this.pointtoscreen(this.location)); int controlxstartposition = controlcoord.x; int controlystartposition = controlcoord.y; int controlxcalculatedwidth = ((rectanglecolumnscount + 1) * widthperblock ) + controlxstartposition; int controlycalculatedheight = ((rectanglerowscount + 1) * heightperblock) + controlystartposition; // ensure mouse coordinates comparible control coordinates boundry checks. point controlrelitivemousecoord = this.parentform.pointtoclient(this.pointtoscreen(e.location)); int controlrelitivemousexcoord = controlrelitivemousecoord.x; int controlrelitivemouseycoord = controlrelitivemousecoord.y; // 0 relitive coordinates used caluculating selected block location int zerorelitivexmousecoord = e.x; int zerorelitiveymousecoord = e.y; // ensure in calculated boundries of control control maybe bigger painted area on // design time form , don't want use unpaited area in our calculations. if((controlrelitivemousexcoord > controlxstartposition) && (controlrelitivemousexcoord < controlxcalculatedwidth)) { if((controlrelitivemouseycoord > controlystartposition) && (controlrelitivemouseycoord < controlycalculatedheight)) { setevaluatedcolourfromposition(zerorelitivexmousecoord, zerorelitiveymousecoord); } } }
Comments
Post a Comment