선언:

C#

[DllImport("user32")]
public static extern Int32 SetWindowLong(IntPtr hWnd, Int32 nIndex, Int32 dwNewLong);


VB.NET

<DllImport("user32")> _
Public Shared Function SetWindowLong(hWnd As IntPtr, nIndex As Int32, dwNewLong As Int32) As Int32
End Function




사용 예제:

C#

void Button1Click(object sender, EventArgs e) {
    Int32 tb1Lng = GetWindowLong(textBox1.Handle, -16);
    Int32 tb2Lng = GetWindowLong(textBox2.Handle, -16);
    
    if ( (tb1Lng & WS_VISIBLE) != 0 )
        SetWindowLong(textBox1.Handle, -16, tb1Lng & ~WS_VISIBLE);

    if ( (tb2Lng & ~WS_VISIBLE) != 0 )
        SetWindowLong(textBox2.Handle, -16, tb1Lng & WS_VISIBLE);
    
    this.Invalidate(true);
}


VB.NET

Private Sub Button1Click(sender As Object, e As EventArgs)
    Dim tb1Lng As Int32 = GetWindowLong(textBox1.Handle, -16)
    Dim tb2Lng As Int32 = GetWindowLong(textBox2.Handle, -16)

    If (tb1Lng And WS_VISIBLE) <> 0 Then
        SetWindowLong(textBox1.Handle, -16, tb1Lng And Not WS_VISIBLE)
    End If

    If (tb2Lng And Not WS_VISIBLE) <> 0 Then
        SetWindowLong(textBox2.Handle, -16, tb1Lng And WS_VISIBLE)
    End If

    Me.Invalidate(True)
End Sub




예제 실행 결과:




매개 변수 설명:

hWnd - 정보를 바꿀 컨트롤의 핸들을 입력합니다.

nIndex - 바꿀 정보의 번호를 입력합니다.

dwNewLong - 새로운 정보(바뀔 정보)를 입력합니다.




API 설명:

컨트롤의 정보를 바꿉니다.




참고:

SetWindowLong (MSDN)




비고:

기본 스타일(-16) 의 경우, 대부분이 컨트롤을 생성할 때(=초기화)에 입력된 값들이기에 SetWindowLong API를 이용하여 정보를 바꾸는 경우, 제대로 적용이 안될 수 있습니다. 그렇기에 활성화 여부를 바꿀 땐 EnableWindow() API를, 표시 여부를 설정할 땐 ShowWindow() API를 사용하는 것을 권장합니다.

기본 스타일 참고 페이지입니다.

기본 스타일 (MSDN)




프로젝트 파일:

 C#.zip

 VB.NET.zip


'API Reference' 카테고리의 다른 글

33. GetSystemDirectory  (0) 2014.10.23
32. SendMessage  (1) 2014.10.11
30. GetWindowLong  (0) 2014.10.10
29. SetWindowTheme  (0) 2014.10.09
28. GetForegroundWindow  (0) 2014.10.09

+ Recent posts