Rubrik: Fenster | 22.03.05 |
SetWindowRgn-Funktion Diese Funktion setzt eine Region für ein Fenster. Nach erfolgreichem Aufruf der Funktion wird das Fenster nur noch innerhalb der zugewiesenen Region angezeigt. | ||
Betriebssystem: Win95, Win98, WinNT 4.0, Win2000, WinME | Views: 11.121 |
Deklaration:
Declare Function SetWindowRgn Lib "user32.dll" ( _ ByVal hWnd As Long, _ ByVal hRgn As Long, _ ByVal bRedraw As Boolean) As Long
Beschreibung:
Diese Funktion setzt eine Region für ein Fenster. Nach erfolgreichem Aufruf der Funktion wird das Fenster nur noch innerhalb der zugewiesenen Region angezeigt.
Parameter:
hWnd | Erwartet das Handle des Fensters, dem eine Region zugewiesen werden soll. |
hRgn | Erwartet das Handle der Region, die dem Fenster zugewiesen werden soll. Wird hier derWert "0" übergeben, so wird die aktuell zugewiesene Region von dem Fenster entfernt. |
bRedraw | Erwartet den Wert "True" wenn das Fenster nach dem Funktionsaufruf neu gezeichnet werden soll, ansonsten übergebenSie hier den Wert "False". |
Rückgabewert:
Ist die Funktion erfolgreich, so liefert sie einen Wert "ungleich 0", andernfalls wird derWert "0" zurückgegeben. Unter Windows NT, 2000 und XP können Sie die GetLastError-Funktion für erweiterte Fehlerinformationen aufrufen.
Beispiel:
Private Declare Function CreateRoundRectRgn Lib "gdi32.dll" ( _ ByVal X1 As Long, _ ByVal Y1 As Long, _ ByVal X2 As Long, _ ByVal Y2 As Long, _ ByVal X3 As Long, _ ByVal Y3 As Long) As Long Private Declare Function CreateEllipticRgnIndirect Lib "gdi32.dll" ( _ lpRect As RECT) As Long Private Declare Function GetWindowRgn Lib "user32.dll" ( _ ByVal hwnd As Long, _ ByVal hRgn As Long) As Long Private Declare Function SetWindowRgn Lib "user32.dll" ( _ ByVal hwnd As Long, _ ByVal hRgn As Long, _ ByVal bRedraw As Boolean) As Long Private Declare Function DeleteObject Lib "gdi32" ( _ ByVal hObject As Long) As Long Private Declare Function CreatePolygonRgn Lib "gdi32.dll" ( _ lpPoint As POINTAPI, _ ByVal nCount As Long, _ ByVal nPolyFillMode As Long) As Long Private Declare Function GetWindowRect Lib "user32" ( _ ByVal hwnd As Long, _ lpRect As RECT) As Long Private Declare Function CreateRectRgnIndirect Lib "gdi32.dll" ( _ lpRect As RECT) As Long Private Declare Function InflateRect Lib "user32.dll" ( _ lpRect As RECT, _ ByVal x As Long, _ ByVal y As Long) As Long Private Declare Function CopyRect Lib "user32.dll" ( _ lpDestRect As RECT, _ lpSourceRect As RECT) As Long Private Declare Function EqualRgn Lib "gdi32.dll" ( _ ByVal hSrcRgn1 As Long, _ ByVal hSrcRgn2 As Long) As Long Private Declare Function SetRect Lib "user32.dll" ( _ lpRect As RECT, _ ByVal X1 As Long, _ ByVal Y1 As Long, _ ByVal X2 As Long, _ ByVal Y2 As Long) As Long Private Type POINTAPI x As Long y As Long End Type Private Type RECT left As Long top As Long right As Long bottom As Long End Type ' CreatePolygonRgn nPolyFillMode-Konstanten Private Const ALTERNATE = 1 ' Alternative zwischen gefüllt und ungefüllt Private Const WINDING = 2 ' Das Dreieck normal gefüllt ' GetWindowRgn Rückgabe-Konstanten Private Const ERROR = 0 ' Die Funktion konnte nicht ausgeführt werden Private Const NULLREGION = 1 ' Es ist keine Region zugewiesen Private Const SIMPLEREGION = 2 ' Die Region ist eine Simple Region (RECT) Private Const COMPLEXREGION = 3 ' Die Region ist eine komplexe Region Dim RectRgn As Long, EllipticRgn As Long Dim PolygonRgn As Long, RoundRectRgn As Long Dim BufferRgn As Long
' Erstellt vier Regionen Private Function CreateRgns() Dim Retval As Long, WndRect As RECT Dim Polygon(2) As POINTAPI, TmpRect As RECT ' Fensterkoordinaten ermitteln und Breite und Höhe ausrechnen Retval = GetWindowRect(Me.hwnd, WndRect) Retval = SetRect(WndRect, 0, 0, WndRect.right - WndRect.left, _ WndRect.bottom - WndRect.top) ' Eine Region zum Empfangen von Regiondaten erstellen BufferRgn = CreateRectRgnIndirect(TmpRect) ' Kopie der Fensterkoordinaten anlegen und verkleinern Retval = CopyRect(TmpRect, WndRect) Retval = InflateRect(TmpRect, -50, -50) ' Rechteckige Region erstellen RectRgn = CreateRectRgnIndirect(TmpRect) ' Elliptische Region erstellen EllipticRgn = CreateEllipticRgnIndirect(WndRect) ' Region mit abgerundeten Ecken erstellen With WndRect RoundRectRgn = CreateRoundRectRgn(.left, .top, .right, .bottom, _ 50, 50) End With ' Dreieckige Region erstellen With Polygon(0) .x = 0 .y = 0 End With With Polygon(1) .x = WndRect.right .y = 0 End With With Polygon(2) .x = WndRect.right / 2 .y = WndRect.bottom End With PolygonRgn = CreatePolygonRgn(Polygon(0), 3, WINDING) End Function
' Regionen erstellen und Info ausgeben Private Sub Form_Load() ' Regionen erstellen Call CreateRgns ' Text auf die Form zeichnen für die Anweisungen Me.AutoRedraw = True Me.Print "Klicken sie mehrmals auf die Form" End Sub
Private Sub Form_Click() Static NextOp As Long Dim Retval As Long ' Aktuell gesetzte Region ermitteln und alte entfernen Retval = GetWindowRgn(Me.hwnd, BufferRgn) NextOp = NextOp + 1 If NextOp < 5 Then NextOp = 1 ' neue Region setzen Select Case NextOp Case 1 Retval = SetWindowRgn(Me.hwnd, EllipticRgn, True) Case 2 Retval = SetWindowRgn(Me.hwnd, PolygonRgn, True) Case 3 Retval = SetWindowRgn(Me.hwnd, RoundRectRgn, True) Case 4 Retval = SetWindowRgn(Me.hwnd, RectRgn, True) Case 5 Retval = SetWindowRgn(Me.hwnd, 0&, True) ' Der Form keine Region 'zuweisen Call CreateRgns ' Regionen erneut erstellen End Select End Sub
' Erstellte Regionen wieder entfernen Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer) DeleteObject EllipticRgn DeleteObject PolygonRgn DeleteObject RoundRectRgn DeleteObject StdRgn DeleteObject BufferRgn End Sub