Results 1 to 4 of 4

Thread: Windowless Buttons

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2008
    Posts
    1,181

    Windowless Buttons

    Typically, buttons (or any other control for that matter) in VB6 are windows, who's window function is designed to handle clicks. Each window has its own handle called an hWnd. However, this has some downsides, particularly when it comes to security. This is because windows can be "hooked" by malware which can then check to see when you click on a button. If the malware is advanced enough it can hook all the events in all the windows, allowing the hacker who's receiving the info from the malware to tell exactly what windows are being clicked, typed in, etc. This allows them to literally map out every moment of your computer usage. For example, it allows them to tell not only what is being typed at any given moment, but also that the fact that what you typed is in a password box (if the text in the titlebar of the window is "password"), so as to clue them in on the importance of what is being typed (in this example, the fact that it is a password).

    This is where my sample program comes into play. It prevents malware from determining every single minutia of what you are doing on your computer, by making all of the activity on the window (as far as malware that hooks hWnds is concerned) to all be happening on the same window, preventing a significant amount of information from being leaked from your computer to a hacker. It does this by implementing windowless controls (buttons in this sample program, but I could also extend it to windowless text boxes, check boxes, etc, if given enough time to code all of that). It works by allowing each button to have its own instance of a class called NonWndButton. The only hWnd in the entire program is the one for Form1. This significantly obfuscates the activity that the user performs on the form.


    Here's a description of each of the code files in this program.
    Windowless Button.frm: This is the form for the program, and has all the code to handle user interactions, as well as an implementation of the NonWndButtonCallback interface.

    NonWndButton.cls: This is the class that has code for just drawing the button and handling events.

    NonWndButtonCallback.cls: This is actually an interface, that must be implemented in the form (Form1 in my program). This allows for callbacks from the button class so that code for handling what each button does can be directly coded in the form, rather than in the separate class file for the buttons.

    modNonWndButton.bas: This is the module file that handles the framework for the buttons, including a method for adding buttons (button class instances) to the Buttons collection which is also contained in this module, as well as one for redrawing the buttons, which is necessary after a screen clearing (as happens when the Cls method). If you wanted to clear text or other graphics on the screen via Cls, but keep the buttons (as these are the main controls now), you absolutely need to be able to redraw them. Also in this module is a method for scanning the buttons when a click occurs on the form, to determine which button actually got clicked, so as to trigger ButtonUp or ButtonDown event of the correct instance of the button class.

    GdiModule.bas: This contains declarations and methods needed to perform the basics of drawing text and rectangles. This is necessary for actually rendering the buttons on the screen.



    Attached to this post is a zip file containing the complete source code, including the vbp project file and vbw project layout file.
    Attached Files Attached Files
    Last edited by Ben321; May 25th, 2016 at 05:07 AM.

  2. #2
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,253

    Re: Windowless Buttons

    Hmm - isn't that a bit overkill, when VB6 already supports Windowless-Controls?

    Here's, what's needed (in a Project-Private UserControl with its Windowless Property at True),
    to do exactly what your current buttons do (with significantly less code and complexity + no API-usage).

    Code:
    Option Explicit
    
    Event ButtonDown()
    Event ButtonUp()
    
    Public Caption As String
    
    Private mMouseDown As Boolean
    
    Private Sub UserControl_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
      mMouseDown = True: Refresh
      RaiseEvent ButtonDown
    End Sub
    
    Private Sub UserControl_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
      mMouseDown = False: Refresh
      RaiseEvent ButtonUp
    End Sub
    
    Private Sub UserControl_Paint()
      BackColor = IIf(mMouseDown, vbRed, vbGreen)
      Cls
      CurrentX = (ScaleWidth - TextWidth(Caption)) / 2
      CurrentY = (ScaleHeight - TextHeight(Caption)) / 2
      Print Caption
    End Sub
    Olaf

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2008
    Posts
    1,181

    Re: Windowless Buttons

    Quote Originally Posted by Schmidt View Post
    Hmm - isn't that a bit overkill, when VB6 already supports Windowless-Controls?

    Here's, what's needed (in a Project-Private UserControl with its Windowless Property at True),
    to do exactly what your current buttons do (with significantly less code and complexity + no API-usage).

    Code:
    Option Explicit
    
    Event ButtonDown()
    Event ButtonUp()
    
    Public Caption As String
    
    Private mMouseDown As Boolean
    
    Private Sub UserControl_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
      mMouseDown = True: Refresh
      RaiseEvent ButtonDown
    End Sub
    
    Private Sub UserControl_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
      mMouseDown = False: Refresh
      RaiseEvent ButtonUp
    End Sub
    
    Private Sub UserControl_Paint()
      BackColor = IIf(mMouseDown, vbRed, vbGreen)
      Cls
      CurrentX = (ScaleWidth - TextWidth(Caption)) / 2
      CurrentY = (ScaleHeight - TextHeight(Caption)) / 2
      Print Caption
    End Sub
    Olaf


    Wow, I never knew it could be so easy to create a windowless button.

  4. #4
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Windowless Buttons

    We also have mswless.ocx that comes with VB6.

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