Results 1 to 5 of 5

Thread: Registering and using a new window class

Threaded View

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2012
    Posts
    4

    Lightbulb Registering and using a new window class

    vb Code:
    1. Public Type WNDCLASS
    2.     style As Long
    3.     lpfnwndproc As Long
    4.     cbClsextra As Long
    5.     cbWndExtra As Long
    6.     hInstance As Long
    7.     hIcon As Long
    8.     hCursor As Long
    9.     hbrBackground As Long
    10.     lpszMenuName As Long
    11.     lpszClassName As Long
    12. End Type
    13.  
    14. Public Const COLOR_WINDOW = 5
    15.  
    16. Public Declare Function UnregisterClass Lib "user32" Alias "UnregisterClassA" _
    17. (ByVal lpClassName As String, ByVal hInstance As Long) As Long
    18. Public Declare Function RegisterClass Lib "user32" Alias "RegisterClassA" _
    19. (Class As WNDCLASS) As Long
    20. Public Declare Function CreateWindowEx Lib "user32" Alias "CreateWindowExA" _
    21. (ByVal dwExStyle As Long, ByVal lpClassName As String, ByVal lpWindowName As String, _
    22. ByVal dwStyle As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, _
    23. ByVal nHeight As Long, ByVal hWndParent As Long, ByVal hMenu As Long, _
    24. ByVal hInstance As Long, lpParam As Any) As Long
    25. Public Declare Function DefWindowProc Lib "user32" Alias "DefWindowProcA" _
    26. (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    27.  
    28. Public Const WS_CHILD = &H40000000
    29. Public Const WS_BORDER = &H800000
    30. Public Const WS_VISIBLE = &H10000000
    31.  
    32. Public Const TEST_CLASS         As String = "TestClass"
    33.  
    34. Public Const WM_PAINT = &HF
    35.  
    36. Public Type RECT
    37.         Left As Long
    38.         Top As Long
    39.         Right As Long
    40.         Bottom As Long
    41. End Type
    42.  
    43. Public Declare Function GetClientRect Lib "user32" _
    44. (ByVal hwnd As Long, lpRect As RECT) As Long
    45. Public Declare Function Rectangle Lib "gdi32" _
    46. (ByVal hdc As Long, ByVal X1 As Long, ByVal Y1 As Long, _
    47. ByVal X2 As Long, ByVal Y2 As Long) As Long
    48. Public Declare Function SelectObject Lib "gdi32" _
    49. (ByVal hdc As Long, ByVal hObject As Long) As Long
    50. Public Declare Function DeleteObject Lib "gdi32" _
    51. (ByVal hObject As Long) As Long
    52. Public Declare Function CreateSolidBrush Lib "gdi32" _
    53. (ByVal crColor As Long) As Long
    54. Public Declare Function CreatePen Lib "gdi32" _
    55. (ByVal nPenStyle As Long, ByVal nWidth As Long, ByVal crColor As Long) As Long
    56.  
    57. Public Const PS_SOLID = 0
    58.  
    59.  
    60. Public Type PAINTSTRUCT
    61.         hdc As Long
    62.         fErase As Long
    63.         rcPaint As RECT
    64.         fRestore As Long
    65.         fIncUpdate As Long
    66.         rgbReserved(32) As Byte
    67. End Type
    68.  
    69. Public Declare Function GetDC Lib "user32" _
    70. (ByVal hwnd As Long) As Long
    71. Public Declare Function ReleaseDC Lib "user32" _
    72. (ByVal hwnd As Long, ByVal hdc As Long) As Long
    73.  
    74. Public Declare Function DrawTextEx Lib "user32" Alias "DrawTextExA" _
    75. (ByVal hdc As Long, ByVal lpsz As String, ByVal n As Long, _
    76. lpRect As RECT, ByVal un As Long, lpDrawTextParams As Any) As Long
    77. Public Declare Function SetTextColor Lib "gdi32" _
    78. (ByVal hdc As Long, ByVal crColor As Long) As Long
    79. Public Declare Function SetBkMode Lib "gdi32" _
    80. (ByVal hdc As Long, ByVal nBkMode As Long) As Long
    81. Public Const TRANSPARENT = 1
    82. Public Const DT_CENTER = &H1
    83. Public Const DT_VCENTER = &H4
    84. Public Const DT_SINGLELINE = &H20
    85.  
    86. Public Const WM_LBUTTONDOWN = &H201
    87. Public Const WM_LBUTTONUP = &H202
    88.  
    89. Private Function GetAddressOf(A As Long) As Long
    90.     GetAddressOf = A
    91. End Function
    92.  
    93. Public Sub UnregisterWindowClass()
    94.     UnregisterClass TEST_CLASS, App.hInstance
    95. End Sub
    96.  
    97. Public Sub RegisterWindowClass()
    98.     Dim wc              As WNDCLASS
    99.     Dim hAtom           As Long
    100.     Dim BytesArray()    As Byte
    101.      
    102.     'The following code fills the WNDCLASS structure:
    103.     wc.style = 0
    104.     wc.lpfnwndproc = GetAddressOf(AddressOf MyWndProc)
    105.     wc.cbClsextra = 0
    106.     wc.cbWndExtra = 0
    107.     wc.hInstance = App.hInstance
    108.     wc.hIcon = 0
    109.     wc.hCursor = 0
    110.     wc.hbrBackground = COLOR_WINDOW
    111.     wc.lpszMenuName = 0
    112.      
    113.     'Convert the class name from Unicode to array of bytes.
    114.     BytesArray = StrConv(TEST_CLASS & Chr$(0), vbFromUnicode)
    115.     wc.lpszClassName = VarPtr(BytesArray(0))
    116.     'Register the new class
    117.     hAtom = RegisterClass(wc)
    118.      
    119. End Sub
    120.  
    121. 'The following function draws a rectangle with 'ABC' letters in it.
    122. Private Sub DrawWin(hdc As Long, hwnd As Long)
    123.     Dim rcWin           As RECT
    124.     Dim hBrush          As Long
    125.     Dim hPen            As Long
    126.     Dim hOldBrush       As Long
    127.     Dim hOldPen         As Long
    128.      
    129.     GetClientRect hwnd, rcWin
    130.      
    131.     'Create and select new brush and new pen.
    132.     hBrush = CreateSolidBrush(RGB(240, 240, 255))
    133.     hPen = CreatePen(PS_SOLID, 2, RGB(255, 0, 255))
    134.     hOldBrush = SelectObject(hdc, hBrush)
    135.     hOldPen = SelectObject(hdc, hPen)
    136.      
    137.     'Draw the rectangle
    138.     Rectangle hdc, rcWin.Left + 1, rcWin.Top + 1, rcWin.Right, rcWin.Bottom
    139.      
    140.     'Deselect and delete the pen and brush objects.
    141.     SelectObject hdc, hOldBrush
    142.     DeleteObject hPen
    143.     DeleteObject hBrush
    144.     SelectObject hdc, hOldPen
    145.      
    146.     'Set the text mode to transparent.
    147.     SetBkMode hdc, TRANSPARENT
    148.     'Select the color of the text
    149.     SetTextColor hdc, RGB(0, 128, 255)
    150.     'Draw the "ABC" letters
    151.     DrawTextEx hdc, "ABC", 3, rcWin, DT_CENTER Or DT_VCENTER Or DT_SINGLELINE, ByVal 0
    152. End Sub
    153.  
    154. 'MyWndProc is the window procedure of the new window class that we create.
    155. 'This procedure receives all messages of the windows we create from the new window class.
    156. Public Function MyWndProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    157.     Dim hdc         As Long
    158.     Dim ps          As PAINTSTRUCT
    159.      
    160.     'Call the default window procedure.
    161.     MyWndProc = DefWindowProc(hwnd, uMsg, wParam, lParam)
    162.     If uMsg = WM_PAINT Then
    163.         'When we receive the WM_PAINT message, repaint the entire window.
    164.         hdc = GetDC(hwnd)
    165.         DrawWin hdc, hwnd
    166.         ReleaseDC hwnd, hdc
    167.     End If
    168.      
    169.     If uMsg = WM_LBUTTONUP Then
    170.         When you click the left mouse button, the "click !" message will be shown.
    171.         MsgBox "click !", vbOKOnly
    172.     End If
    173. End Function
    174.  
    175. Public Function CreateMyWindow(x As Long, y As Long, Width As Long, Height As Long, hParent As Long) As Long
    176.     CreateMyWindow = CreateWindowEx(0, TEST_CLASS, "", WS_CHILD Or WS_BORDER Or WS_VISIBLE, _
    177.     x, y, Width, Height, hParent, 0, App.hInstance, ByVal 0)
    178. End Function

    Last edited by Hack; Apr 2nd, 2012 at 06:01 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width