?
Solved

500 POINTS - SetCursorPos on Panel Center

Posted on 2007-08-02
23
Medium Priority
?
314 Views
Last Modified: 2010-04-05
I need to Set Cursor Position to center of a panel.

I've tried this but it's not coming up at the center.

code:   SetCursorPos((Panel1.width div 2)+Panel1.Left, (Panel1.Height div 2)+Panel1.Top);

The whole code:

procedure TfMain.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var i : integer;

begin

  if Button = mbRight then
  begin
      SetCursorPos((Panel1.width div 2)+Panel1.Left, (Panel1.Height div 2)+Panel1.Top);
  end;

Any ideas why it's not working please?

0
Comment
Question by:ST3VO
  • 9
  • 8
  • 6
23 Comments
 
LVL 10

Expert Comment

by:dinilud
ID: 19615843
procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
  var pt:TPoint;
begin
  if Button = mbRight then
  begin
      pt:=Point(Panel1.Width div 2 ,Panel1.Height div 2);
      pt:=ClientToScreen(pt);
      SetCursorPos(Panel1.Left+pt.x, Panel1.Top+pt.Y);
  end;
end;
0
 
LVL 21

Expert Comment

by:ziolko
ID: 19615845
procedure TForm1.Button1Click(Sender: TObject);
var p: TPoint;
begin
  p.X := (Panel1.width div 2)+Panel1.Left;
  p.Y := (Panel1.Height div 2)+Panel1.Top;
  p := ClientToScreen(p);
  SetCursorPos(p.X, p.Y);
end;


ziolko.
0
 

Author Comment

by:ST3VO
ID: 19615976
THis is really strange....None of them work :o(

The mouse appears 1/3 to the left.

Do I need to put the same code on FormMouseUP ???



0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:dinilud
ID: 19615992
Try this

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
  var pt:TPoint;
begin
  if Button = mbRight then
  begin
      pt:=Point(Panel1.Width div 2 ,Panel1.Height div 2);
      Windows.ClientToScreen( Panel1.Handle,pt);
      SetCursorPos(pt.x, pt.Y);
  end;
end;
0
 

Author Comment

by:ST3VO
ID: 19616077
Guys, I really don't understand why but it still appears 1/3 left :o(
0
 
LVL 10

Expert Comment

by:dinilud
ID: 19616109
Start a new project and check, is this code is working or not
0
 

Author Comment

by:ST3VO
ID: 19616151
I think I know what it is.

I changed my Screen Resolution down to 1024 x 768 and it was right on the center.

But when I put to resolution higher is goes to the left....Strange or normal?

0
 
LVL 10

Expert Comment

by:dinilud
ID: 19616476
procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
  var WRec:TRect;
begin
  if Button = mbRight then
  begin
      GetWindowRect(Panel1.Handle,WRec);
      SetCursorPos(WRec.Left+(WRec.Right-WRec.Left) div 2 ,
                                      WRec.Top+(WRec.Bottom-WRec.Top) div 2);
  end;
end;
0
 
LVL 21

Expert Comment

by:ziolko
ID: 19616495
strange

try this one:

procedure TForm1.Button1Click(Sender: TObject);
var p: TPoint;
begin
  p.X := 0;
  p.Y := 0;
  Windows.ClientToScreen(Handle, p);
  p.X := p.X + ((Panel1.width div 2)+Panel1.Left);
  p.Y := p.Y + ((Panel1.Height div 2)+Panel1.Top);
  Windows.SetCursorPos(p.X, p.Y);
end;

ziolko.
0
 
LVL 21

Expert Comment

by:ziolko
ID: 19616506
...or even better this one:
procedure TForm1.Button1Click(Sender: TObject);
var p: TPoint;
begin
  p.X := 0;
  p.Y := 0;
  if not Windows.ClientToScreen(Handle, p) then
    ShowMessage(SysErrorMessage(GetLastError));
  p.X := p.X + ((Panel1.width div 2)+Panel1.Left);
  p.Y := p.Y + ((Panel1.Height div 2)+Panel1.Top);
  if not Windows.SetCursorPos(p.X, p.Y) then
    ShowMessage(SysErrorMessage(GetLastError));
end;

ziolko.
0
 

Author Comment

by:ST3VO
ID: 19616782
Very strange....

I know that it's not in the center but I tried ziolko's code with the Messages and I got none!

So, I bet it things it's on the center of the Panel although it only IS when the screen resolution is 1024 x 768.

My current resolution is: 1680 x 1050.

I really don't have a clue of what to try anymore :o(
0
 
LVL 10

Accepted Solution

by:
dinilud earned 1500 total points
ID: 19616799
did you try my code


{
procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
  var WRec:TRect;
begin
  if Button = mbRight then
  begin
      GetWindowRect(Panel1.Handle,WRec);
      SetCursorPos(WRec.Left+(WRec.Right-WRec.Left) div 2 ,
                                      WRec.Top+(WRec.Bottom-WRec.Top) div 2);
  end;
end;
}
0
 

Author Comment

by:ST3VO
ID: 19616803
Here's the code of what I tried:

procedure TfMain.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
const cnCursorID1 = 1;
    //for the listview
var i : integer;
    InsertItem : TListItem;
    p: TPoint;

begin
  Screen.Cursors[ cnCursorID1 ] :=
    LoadCursorFromFile(
      'SmoothHand.cur' );
  Cursor := cnCursorID1;

  if Button = mbright then
  begin
 
      Cursor := cnCursorID1;
      panel1.Cursor:=cnCursorID1;
      p.X := 0;
  p.Y := 0;
  if not Windows.ClientToScreen(Handle, p) then
    ShowMessage(SysErrorMessage(GetLastError));
  p.X := p.X + ((Panel1.width div 2)+Panel1.Left);
  p.Y := p.Y + ((Panel1.Height div 2)+Panel1.Top);
  if not Windows.SetCursorPos(p.X, p.Y) then
    ShowMessage(SysErrorMessage(GetLastError));

...
0
 
LVL 10

Expert Comment

by:dinilud
ID: 19616914
did you try example

using GetWindowRect
0
 
LVL 21

Expert Comment

by:ziolko
ID: 19616936
>>I tried ziolko's code with the Messages and I got none!

that's good that there was no message it means that API calls executed ok, but why cursor is in wrong position... no clue:/

ziolko.
0
 

Author Comment

by:ST3VO
ID: 19616957
Yes dinilud,

I did try it...but no luck :o(

0
 
LVL 21

Expert Comment

by:ziolko
ID: 19617010
in which resolution you designed and compiled your exe?
this might related to scaled and pixelperinch properties

ziolko.
0
 

Author Comment

by:ST3VO
ID: 19617033
I designed it on: 1680 x 1050 Res....but strangly it's only centered when the Res is: 1024 x 768
0
 
LVL 21

Expert Comment

by:ziolko
ID: 19617094
it's late and my head is about to blow in a second, i'll get back to you tomorrow

ziolko.
0
 
LVL 10

Expert Comment

by:dinilud
ID: 19617171
procedure TForm1.Button1Click(Sender: TObject);
begin
  //check this function workig perfecty or not. This will set middle of screen
  SetCursorPos(Screen.Width div 2,Screen.Height div 2);
end;
0
 

Author Comment

by:ST3VO
ID: 19617282
Nope....That was the first thing I tried :o(

When you're mixing Windows GUI and 2 different 3D Engines on one application, nothing seems to work right ever :o(   Grrrrrr

0
 
LVL 10

Expert Comment

by:dinilud
ID: 19617725
OK Good Luck
0
 

Author Comment

by:ST3VO
ID: 19624090
Thanks :o)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question