Click here to Skip to main content
15,067,539 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
I'm trying to move a window, with the MoveWindow Function of API, but the problem is, it doesnt work on every window (but on the most), i found out that the windows that wont move are such windows forms that arent allowed to be maximized but when i made a form on my VS which isnt allowed to be maximized either, then it worked, so im confused and if you have any idea please reply as soon as possible, thanks in advance Begi.

PS: I used the SetWindowPos too, the same thing...here's the code below;


VB
Imports System.Runtime.InteropServices
Imports System.Threading

Public Class Form1
    Friend Structure RECT
        Public left As Integer
        Public top As Integer
        Public right As Integer
        Public bottom As Integer
    End Structure

    Friend Declare Auto Function GetForegroundWindow Lib "user32.dll" () As IntPtr

    <DllImport("user32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall, ExactSpelling:=True, SetLastError:=True)> _
    Friend Shared Function GetWindowRect(hWnd As IntPtr, ByRef rect As RECT) As Boolean
    End Function

    <DllImport("user32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall, ExactSpelling:=True, SetLastError:=True)> _
    Friend Shared Sub MoveWindow(hwnd As IntPtr, X As Integer, Y As Integer, nWidth As Integer, nHeight As Integer, bRepaint As Boolean)
    End Sub

    Private Shared Sub Main()
        Dim id As IntPtr
        Dim Rect As New RECT()
        Thread.Sleep(2000)
        id = GetForegroundWindow()
        GetWindowRect(id, Rect)
        MoveWindow(id, 450, 100, Rect.right - Rect.left, Rect.bottom - Rect.top, True)
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Main()
    End Sub
End Class
Posted

Instead of MoveWindow, use SetWindowPos function[^]
   
Comments
[no name] 23-May-12 17:04pm
   
Yes i used SetWindowPos at the first time and it didnt work on all windows then i used MoveWindow, again i tried but without success. Im using Win7 what do u think this could be the problem or something else?
Maciej Los 23-May-12 17:35pm
   
Maybe, i have no idea :(
Check this site: http://allapi.mentalis.org/apilist/apilist.php. Maybe there you'll find something interesting about both functions.
It's going to be difficult, if not impossible, to determine what's wrong without verifying that the Windows functions have succeeded. They don't throw exceptions and explicit checks are required.

Check MSDN windows and messages reference[^] for each function to find out how an error is reported and use System.Runtime.InteropServices.Marshal.GetLastWin32Error() to get the error codes.

Alan.
   
Run your program (or your IDE) as administrator.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900