Winapi SetWindowPos上的错误

Winapi SetWindowPos上的错误,winapi,c++-cli,Winapi,C++ Cli,我正在尝试编写更改应用程序记事本windows位置的简单代码。记事本只能在后台运行1个实例 我正在使用Process::GetProcessByName记事本获取进程ID, 和myProcess->MainWindowHandle获取记事本窗口句柄 现在我试图通过将HWND句柄传递给SetWindowPos来更改窗口大小 我不知道如何正确地做它搜索网页没有找到一个明确的新手答案 首先,我尝试: HWND hwnd = myProcess->MainWindowHandle;

我正在尝试编写更改应用程序记事本windows位置的简单代码。记事本只能在后台运行1个实例

我正在使用Process::GetProcessByName记事本获取进程ID, 和myProcess->MainWindowHandle获取记事本窗口句柄

现在我试图通过将HWND句柄传递给SetWindowPos来更改窗口大小

我不知道如何正确地做它搜索网页没有找到一个明确的新手答案

首先,我尝试:

HWND hwnd =  myProcess->MainWindowHandle;
             SetWindowPos(hwnd , 
                 HWND_TOP, 
                 100, 
                 100, 
                 0, 0,          // Ignores size arguments. 
                 SWP_NOSIZE); 
这让我抓狂

Compile error:  error C2440: 'initializing' : cannot convert from 'System::IntPtr' to 'HWND'
然后我试着投球和传球作为参考:

HWND hwnd =  (HWND)&myProcess->MainWindowHandle;
SetWindowPos(&hwnd , 
         HWND_TOP, 
         100, 
         100, 
         0, 0,          // Ignores size arguments. 
         SWP_NOSIZE); 

       }
错误:

Compile error:  error C2664: 'SetWindowPos' : cannot convert parameter 1 from 'HWND *' to 'HWND'
完整代码见下文:

#include "StdAfx.h"
#include <windows.h>
#include <iostream>
#include <string>  

#using <System.dll>
using namespace System;
using namespace System::Diagnostics;
int main()
{

      array<Process^>^myProcesses = Process::GetProcessesByName( "notepad");
      if ( myProcesses->Length == 0 )
            Console::WriteLine( "Could not find notepad processes on local computer." );
      Collections::IEnumerator^ myEnum = myProcesses->GetEnumerator();
      while ( myEnum->MoveNext() )
      {
         Process^ myProcess = safe_cast<Process^>(myEnum->Current);
         Console::Write( "Process Name : {0}  Process ID : {1}  HandleCount : {2}\n", myProcess->ProcessName, myProcess->Id, myProcess->HandleCount );
         Console::Write( "Main window Title : {0}  MainWindowHandle : {1}", myProcess->MainWindowTitle, myProcess->MainWindowHandle );

//HWND hwnd =  myProcess->MainWindowHandle; //1st try
     //          SetWindowPos(hwnd , 
  //               HWND_TOP, 
  //               100, 
  //               100, 
  //               0, 0,          // Ignores size arguments. 
  //               SWP_NOSIZE); 

        HWND hwnd =  (HWND)&myProcess->MainWindowHandle;//2nd try
        SetWindowPos(&hwnd , 
                 HWND_TOP, 
                 100, 
                 100, 
                 0, 0,          // Ignores size arguments. 
                 SWP_NOSIZE); 

               }
      }   
}

向SetWindowsPos函数的第一个参数发送地址,但此函数需要HWND值。你在C++中知道:

&变量=存储值的变量的返回地址 变量=变量的返回值

VisualC++智能工具或工具提示给你原型。

BOOL SetWindowsPos(HWND, HWND, int, int, int, int, UINT);
编辑:如注释中所述,此行:

HWND hwnd = (HWND)&myProcess->MainWindowHandle;
如果错误,您必须执行以下操作:

HWND hwnd = (HWND)myProcess->MainWindowHandle.ToPointer();

ToPointer方法文档:

HWND HWND=HWNDmyProcess->MainWindowHandle.ToPointer;当然,在SetWindowPos调用中使用hwnd和not&hwnd,如@πντα所述ῥεῖSetWindowPoshwnd,删除&编译器错误非常明显