Autor Tema: Pintar un Texto sobre un Button.  (Leído 3267 veces)

0 Usuarios y 1 Visitante están viendo este tema.

E N T E R

  • Petabyte
  • ******
  • Mensajes: 1062
  • Reputación: +57/-13
  • www.enterpy.com
    • Ver Perfil
    • www.enterpy.com
Pintar un Texto sobre un Button.
« en: Abril 03, 2014, 12:22:23 pm »
Hola, esto una ves ya había posteado pero nunca lo pude resolver, en vb.net se que puede hacer pero en vb6 es lo que quiero seria muy útil para evitar poner en el caption el texto asi se ahorra un poco de espacio en los botones.

SCRENN


http://snag.gy/m0IJF.jpg

Lo que quiero es pintar un texto sobre el Button por ejemplo: [F1] o [ctrl F1].
« última modificación: Abril 03, 2014, 12:29:07 pm por E N T E R »
CIBER GOOGLE - CONCEPCIÓN PARAGUAY
www.enterpy.com
Primera regla de la programacion, para que vas a hacerlo complicado si lo puedes hacer sencillo

YAcosta

  • Moderador Global
  • Exabyte
  • *****
  • Mensajes: 2853
  • Reputación: +160/-38
  • Daddy de Qüentas y QüeryFull
    • Ver Perfil
    • Personal
Re:Pintar un Texto sobre un Button.
« Respuesta #1 en: Abril 03, 2014, 03:24:26 pm »
Crear un control de usuario, no se como seria de otra forma.

Te escribo un MP por otro tema
Me encuentras en YAcosta.com

Waldo

  • Gigabyte
  • ****
  • Mensajes: 264
  • Reputación: +22/-0
    • Ver Perfil
Re:Pintar un Texto sobre un Button.
« Respuesta #2 en: Abril 03, 2014, 05:54:22 pm »
Hola amigo Enter, a primera vista se me habia ocurrido tratar de poner el texto con alguna api, pero hasta donde veo las funciones: DrawText y TextOut necesitan como parametro un hDC (Device Context) para poder "dibujar" el texto, pero esto lo tienen por ej los forms o los pictures, no lo tiene un boton command.

LeandroA

  • Administrador
  • Petabyte
  • *****
  • Mensajes: 1128
  • Reputación: +151/-8
    • Ver Perfil
Re:Pintar un Texto sobre un Button.
« Respuesta #3 en: Abril 04, 2014, 08:26:02 am »
Hola, mira hice un modulo bas, para subclasificar el botón y pintarlo por ensima de este:

Código: (Vb) [Seleccionar]
Option Explicit
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function GetDC Lib "user32.dll" (ByVal hwnd As Long) As Long
Private Declare Function ReleaseDC Lib "user32.dll" (ByVal hwnd As Long, ByVal hdc As Long) As Long
Private Declare Function DrawText Lib "user32.dll" Alias "DrawTextA" (ByVal hdc As Long, ByVal lpStr As String, ByVal nCount As Long, ByRef lpRect As RECT, ByVal wFormat As Long) As Long
Private Declare Function GetClientRect Lib "user32.dll" (ByVal hwnd As Long, ByRef lpRect As RECT) As Long
Private Declare Function SetTextColor Lib "gdi32.dll" (ByVal hdc As Long, ByVal crColor As Long) As Long
Private Declare Function SetBkMode Lib "gdi32.dll" (ByVal hdc As Long, ByVal nBkMode As Long) As Long
Private Declare Function InflateRect Lib "user32" (lpRect As RECT, ByVal x As Long, ByVal y As Long) As Long
Private Declare Function CreateFont Lib "gdi32" Alias "CreateFontA" (ByVal nHeight As Long, ByVal nWidth As Long, ByVal nEscapement As Long, ByVal nOrientation As Long, ByVal fnWeight As Long, ByVal fdwItalic As Boolean, ByVal fdwUnderline As Boolean, ByVal fdwStrikeOut As Boolean, ByVal fdwCharSet As Long, ByVal fdwOutputPrecision As Long, ByVal fdwClipPrecision As Long, ByVal fdwQuality As Long, ByVal fdwPitchAndFamily As Long, ByVal lpszFace As String) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Private Declare Function SetProp Lib "user32.dll" Alias "SetPropA" (ByVal hwnd As Long, ByVal lpString As String, ByVal hData As Long) As Long
Private Declare Function RemoveProp Lib "user32.dll" Alias "RemovePropA" (ByVal hwnd As Long, ByVal lpString As String) As Long
Private Declare Function GetProp Lib "user32.dll" Alias "GetPropA" (ByVal hwnd As Long, ByVal lpString As String) As Long


Private Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type


Public Enum EnuCaptionAlign
    DT_TOP = &H0
    DT_LEFT = &H0
    DT_CENTER = &H1
    DT_RIGHT = &H2
    DT_BOTTOM = &H8
    DT_VCENTER = &H4
End Enum

Private Const DT_SINGLELINE As Long = &H20
Private Const GWL_WNDPROC = (-4)
Private Const WM_PAINT As Long = &HF&
Private Const BM_SETSTATE As Long = &HF3&
Private Const TRANSPARENT  As Long = &H1
Private Const OUT_DEFAULT_PRECIS As Long = 0
Private Const CLIP_DEFAULT_PRECIS As Long = 0
Private Const PROOF_QUALITY As Long = 2
Private Const DEFAULT_PITCH As Long = 0
Private Const DEFAULT_CHARSET As Long = 1
Private Const FW_NORMAL As Long = 400


Dim PrevProc As Long
Dim cCaptions As Collection


Public Function AddButtonLabel(ByVal hwnd As Long, ByVal Caption As String, ByVal ForeColor As Long, Optional Align As EnuCaptionAlign = DT_TOP Or DT_RIGHT)
    On Error GoTo ErrHandler
    Dim lngCaption As Long

    If cCaptions Is Nothing Then Set cCaptions = New Collection
    cCaptions.Add Caption, Str(hwnd)
    SetProp hwnd, "ForeColor", ForeColor
    SetProp hwnd, "Align", Align
    PrevProc = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf WindowProc)
    AddButtonLabel = PrevProc <> 0
ErrHandler:
End Function

Public Function RemoveButtonLabel(ByVal hwnd As Long) As Boolean
    SetWindowLong hwnd, GWL_WNDPROC, PrevProc
    cCaptions.Remove Str(hwnd)
    RemoveProp hwnd, "ForeColor"
    RemoveProp hwnd, "Align"
    RemoveButtonLabel = CBool(Err.Number = 0)
End Function


Private Function WindowProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    On Error Resume Next
    Dim hdc As Long
    Dim tRECT As RECT
    Dim hFont As Long
    Dim OldFont As Long
   
    WindowProc = CallWindowProc(PrevProc, hwnd, uMsg, wParam, lParam)
   
    If uMsg = WM_PAINT Or uMsg = BM_SETSTATE Then
   
        hdc = GetDC(hwnd)
       
        GetClientRect hwnd, tRECT
       
        InflateRect tRECT, -5, -5
       
        SetBkMode hdc, TRANSPARENT
       
        SetTextColor hdc, GetProp(hwnd, "ForeColor")

        hFont = CreateFont(-9, 0, 0, 0, FW_NORMAL, False, True, False, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, PROOF_QUALITY, DEFAULT_PITCH, "Times New Roman")
       
        OldFont = SelectObject(hdc, hFont)
       
        DrawText hdc, cCaptions(Str(hwnd)), -1, tRECT, DT_SINGLELINE Or GetProp(hwnd, "Align")

        DeleteObject SelectObject(hdc, OldFont)
       
        ReleaseDC hwnd, hdc
       
    End If
   
End Function

y la forma de llamarlo seria asi
Código: (Vb) [Seleccionar]
Option Explicit

Private Sub Form_Load()
    AddButtonLabel Command1.hwnd, "F1", vbBlue
    AddButtonLabel Command2.hwnd, "F2", vbRed, DT_RIGHT Or DT_BOTTOM
End Sub

Private Sub Form_Unload(Cancel As Integer)
    RemoveButtonLabel Command1.hwnd
    RemoveButtonLabel Command2.hwnd
End Sub

fijate que la funcion AddButtonLabel  tiene cuatro parametros el 1ero el hwnd del boton, el 2do el texto, el 3ero el color y el 4to es la alineacion tanto horizontal como vertical, solo tenes que combinar las constante de enumeración (mira en el command2 tenes un ejemplo).

E N T E R

  • Petabyte
  • ******
  • Mensajes: 1062
  • Reputación: +57/-13
  • www.enterpy.com
    • Ver Perfil
    • www.enterpy.com
Re:Pintar un Texto sobre un Button.
« Respuesta #4 en: Abril 04, 2014, 10:16:50 am »
Que grandee Leandro una maquina en el VB6  ;D ;D

Gracias gracias amigo funciona perfecto.
CIBER GOOGLE - CONCEPCIÓN PARAGUAY
www.enterpy.com
Primera regla de la programacion, para que vas a hacerlo complicado si lo puedes hacer sencillo

Waldo

  • Gigabyte
  • ****
  • Mensajes: 264
  • Reputación: +22/-0
    • Ver Perfil
Re:Pintar un Texto sobre un Button.
« Respuesta #5 en: Abril 04, 2014, 10:23:06 am »
Un Genio Leandro!!!

YAcosta

  • Moderador Global
  • Exabyte
  • *****
  • Mensajes: 2853
  • Reputación: +160/-38
  • Daddy de Qüentas y QüeryFull
    • Ver Perfil
    • Personal
Re:Pintar un Texto sobre un Button.
« Respuesta #6 en: Abril 04, 2014, 12:35:26 pm »
Me voy a ir a comprar un sombrero para ponermelo y poder quitarmelo.

Excelente Lea
Me encuentras en YAcosta.com

ssccaann43

  • Terabyte
  • *****
  • Mensajes: 970
  • Reputación: +97/-58
    • Ver Perfil
    • Sistemas Nuñez, Consultores y Soporte, C.A.
Re:Pintar un Texto sobre un Button.
« Respuesta #7 en: Abril 04, 2014, 01:01:26 pm »
Q Crack leandro...! Excelente...!
Miguel Núñez.