2009-12-13 3 views

답변

3

어떤 종류의 Win32 기반 런타임에서 생성 된 모든 응용 프로그램에서이 창을 볼 수 있다고 가정하면 창 작업을 위해 핵심 Win32 API의 p/invoke를 사용해야 할 것처럼 보입니다.

예를 들어 user32.dll에서 가져올 수있는 SetWindowPos을 사용할 수 있습니다. 서명은 다음과 같습니다.

BOOL SetWindowPos(HWND hWnd, 
    HWND hWndInsertAfter, 
    int X, 
    int Y, 
    int cx, 
    int cy, 
    UINT uFlags 
); 

이전에 p/invoke 가져 오기를 수행 한 것으로 가정하지 않으므로 맨 위부터 살펴 보겠습니다. 이다는 Win32 창 방식의 호출/P와

/* hWndInsertAfter constants. Lifted from WinUser.h, 
* lines 4189 onwards depending on Platform SDK version */ 
public static IntPtr HWND_TOP = (IntPtr)0; 
public static IntPtr HWND_BOTTOM = (IntPtr)1; 
public static IntPtr HWND_TOPMOST = (IntPtr)(-1); 
public static IntPtr HWND_NOTOPMOST = (IntPtr)(-2); 

/* uFlags constants. Lifted again from WinUser.h, 
* lines 4168 onwards depending on Platform SDK version */ 
/* these can be |'d together to combine behaviours */ 
public const int SWP_NOSIZE   = 0x0001; 
public const int SWP_NOMOVE   = 0x0002; 
public const int SWP_NOZORDER  = 0x0004; 
public const int SWP_NOREDRAW  = 0x0008; 
public const int SWP_NOACTIVATE  = 0x0010; 
public const int SWP_FRAMECHANGED = 0x0020; 
public const int SWP_SHOWWINDOW  = 0x0040; 
public const int SWP_HIDEWINDOW  = 0x0080; 
public const int SWP_NOCOPYBITS  = 0x0100; 
public const int SWP_NOOWNERZORDER = 0x0200; /* Don't do owner Z ordering */ 
public const int SWP_NOSENDCHANGING = 0x0400; /* Don't send WM_WINDOWPOSCHANGING */ 
public const int SWP_DRAWFRAME  = SWP_FRAMECHANGED; 
public const int SWP_NOREPOSITION = SWP_NOOWNERZORDER; 
public const int SWP_DEFERERASE  = 0x2000; 
public const int SWP_ASYNCWINDOWPOS = 0x4000; 

[DllImport("user32.dll", CharSet = CharSet.Auto)] 
public static extern int SetWindowsPos(IntPtr hWnd, 
    IntPtr hWndInsertAfter, 
    int x, 
    int y, 
    int cx, 
    int cy, 
    UInt32 uFlags); 

성가신 일 :

1) Form1 클래스에 다음 선언을 추가 한 후 응용 프로그램을 형성하는 창을 만들고 : 그냥 윈도우 폼 응용 프로그램을 비난하자 Win32에서 사용하는 다양한 숫자 상수 등을 가져 오기 시작해야합니다. 따라서 사전에 모든 껌을 미리 입력해야합니다.

해당 기능에 대한 설명은 SetWindowPos 메서드의 MSDN 링크를 참조하십시오.

2) cmdMakeHidden라는 폼에 단추를 추가하고 다음과 같이 핸들러를 쓰기 :

private void cmdMakeHidden_Click(object sender, EventArgs e) 
{ 
    //also causes the icon in the start bar to disappear 
    //SWP_HIDEWINDOW is the 'kill -9' of the windows world without actually killing! 
    SetWindowPos(this.Handle, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_HIDEWINDOW); 
} 

는 창을 숨기도록 선택의 창 핸들로 'this.Handle'을 교체합니다.

이 방법은 실제로 여러 변경 사항을 적용하는 데 사용되므로 SWP_NO* 옵션 중 일부를 사용해야 할 필요가 있습니다. 예를 들어, SWP_NOSIZE를 지정해야합니다. 그렇지 않으면 0을 cx에 전달하고 cy는 창을 0 너비 및 높이로 동시에 축소시킵니다.

는 창을 이동 보여 양식에 cmdMove라는 또 다른 버튼을 추가하고 다음과 같이 클릭 핸들러를 작성하려면 버튼을 누르 때마다

private void cmdMove_Click(object sender, EventArgs e) 
{ 
    SetWindowPos(this.Handle, HWND_TOP, 100, 100, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOREPOSITION); 
} 

이 코드는 100100로 양식을 이동합니다.

다시 this.Handle을 바꿉니다. 여기에서 HWND_TOP은 SWP_NOZORDER 및 SWP_NOREPOSITION 플래그로 재정렬이 비활성화되었으므로 여기서는 완전히 선택 사항입니다.

희망 사항은 올바른 방향으로 이동하는 데 도움이됩니다.

+0

같은 방법으로 다른 Win32 메서드를 사용하여 창의 실제 창 스타일을 수정할 수 있습니다. SetWindowLong 메서드 (http://msdn.microsoft.com/en-us/library/ms633591(VS.85).aspx)는 하나 이상의 창 스타일을 수정하는 데 사용됩니다. 스타일을 변경 한 후에 SetWindowPos를 호출해야하는 경우가 종종 있습니다. 스타일 변경으로 인해 창 프레임에 변경이 발생하는 경우가 대부분입니다. 추한 것처럼 보일 수도 있지만 작동 할 것입니다! –

+0

SetWindowsPos ==> SetWindowPos. 감사합니다. – Behrooz

관련 문제