Robert Melton wrote:
I am trying to reposition a window using Win32::API.  I believe my
struct in this case is malformed but I don't know what is malformed
about it -- and the WinAPI just gives me a genertic "The parameter is
incorrect.".  Any advise would be great -- thanks.

--- CODE BELOW ---
use warnings;
use strict;

use Win32;
use Win32::API;
use Win32::API::Struct;

my $FindWindow = new Win32::API('user32', 'FindWindow', 'PP', 'N');
my $SetWindowPlacement = new Win32::API('user32',
'SetWindowPlacement', 'NP', 'N');

Win32::API::Struct->typedef( 'WINDOWPLACEMENT', qw(
     UINT length;
     UINT flags;
     UINT showCmd;
     POINT ptMinPosition;
     POINT ptMaxPosition;
     RECT rcNormalPosition;
));

Win32::API::Struct->typedef( 'POINT', qw(
     LONG x;
     LONG y;
));

Win32::API::Struct->typedef( 'RECT', qw(
     LONG left;
     LONG top;
     LONG right;
     LONG bottom;
));

my $window = $FindWindow->Call("Notepad", 'Untitled - Notepad');


Every time you create a new Struct below you've written 'new' twice. Looking at the docs, you could omit the first 'new' ... I don't know if that matters. I was unable to detect any effect.


my $UpperLeftPoint = new Win32::API::Struct->new( 'POINT' );
$UpperLeftPoint->{'x'} = 1;
$UpperLeftPoint->{'y'} = 1;

my $WindowSize = new Win32::API::Struct->new( 'RECT' );
$WindowSize->{'left'} = 1;
$WindowSize->{'top'} = 1;
$WindowSize->{'right'} = 1000;
$WindowSize->{'bottom'} = 1000;

my $WindowPlacement = new Win32::API::Struct->new( 'WINDOWPLACEMENT' );
$WindowPlacement->{'flags'} = 0;
$WindowPlacement->{'showCmd'} = 0;

That hides the window and activates another window. Is that what you want ? I thought maybe you want SW_SHOWNORMAL which is:
$WindowPlacement->{'showCmd'} = 1;


$WindowPlacement->{'ptMinPosition'} = $UpperLeftPoint;
$WindowPlacement->{'ptMaxPosition'} = $UpperLeftPoint;
$WindowPlacement->{'rcNormalPosition'} = $WindowSize;

I wonder whether it's permissible to assign a typedef to another typedef like that. I'm not altogether sure what "typedef" does. The docs mention that it defines "an LPNAME type, which holds a pointer to your strucure", so it might be that passing a typedef to another typedef is not doing what you think it is. Instead, I thought I'd try:


$WindowPlacement->{'ptMinPosition'} = pack('LL', 1, 1);
$WindowPlacement->{'ptMaxPosition'} = pack('LL', 1, 1);
$WindowPlacement->{'rcNormalPosition'}=pack('LLLL',1,1,1000,1000);

But that made absolutely no difference, so I'm none the wiser. I'm not all that familiar with Win32::API, but I *think* that what I tried there is a viable alternative.

$WindowPlacement->{'length'} = $WindowPlacement->{'sizeof'};

I don't think this is right. I find that $WindowPlacement->{'sizeof'} returns an empty value (which would equate to zero). For me, the value is 44. That would be 4 bytes for 'flags', 4 bytes for 'showCmd', 8 bytes for 'ptMinPosition', 8 bytes for 'ptMaxPosition', 16 bytes for 'rcNormalPosition' and 4 bytes for 'length' - which (I hope) sums to 44. I would therefore have it as:


$WindowPlacement->{'length'} = 44;

In C, you just have it as sizeof(WINDOWPLACEMENT), but I don't know how you're supposed to emulate that with Win32::API. The docs suggest to me that you're expected to use Win32::API::Struct->sizeof('WINDOWPLACEMENT'), but for me that returns 0.


print "Window: $window\n"; print "SetWindowPlacement: ".$SetWindowPlacement->Call($window, $WindowPlacement)."\n\n";

print "Errors (if any): ".Win32::FormatMessage(Win32::GetLastError());

That will always show an error message, even if the current script has not produced an error - but the error I keep getting ("The parameter is incorrect") is certainly being produced by the preceding line of code.


It looks to me that with those changes (or some sort of combination thereof) the script should work - but I'm damned if I can get it to do the job.

I do have a perl script (using Inline::C) that wraps 'FindWindow' and 'SetWindowPlacement'. It works fine for repositioning a window - but it's not much use to you if you don't have a compiler.

Hope there's something there that helps - or, better still, that someone else can have more success than either you or I have managed :-)

Cheers,
Rob

_______________________________________________
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to