Hmm... jeg tror at både niceday og caturn er gået forkert - der står i hvert fald Visual Basic på min titellinje :) Men det er faktisk utroligt simpelt, du skal bare bruge [Objekt].Forecolor = [Farve i hex el. 'long'] f.eks. Label1.Forecolor = vbRed ... Tja, jeg fik lige øje på at der faktisk står "knap" i dit spørgsmål, og dette er en smule mere kompliceret da der ikke er "indbygget" en funktion der gør dette ved knapper...
Ok, det kan godt lade sig gøre, men det er ikke ligefrem enkelt. Men følgende kodestump sørger for det meste for dig:
'*** I et modul *** Option Explicit
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long) Private Declare Function GetParent Lib "user32" (ByVal hWnd As Long) As Long Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long Private Declare Function GetProp Lib "user32" Alias "GetPropA" (ByVal hWnd As Long, ByVal lpString As String) As Long Private Declare Function SetProp Lib "user32" Alias "SetPropA" (ByVal hWnd As Long, ByVal lpString As String, ByVal hData As Long) As Long Private Declare Function RemoveProp Lib "user32" Alias "RemovePropA" (ByVal hWnd As Long, ByVal lpString As String) As Long Private 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 GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long Private Declare Function DrawText Lib "user32" Alias "DrawTextA" (ByVal hDC As Long, ByVal lpStr As String, ByVal nCount As Long, lpRect As RECT, ByVal wFormat As Long) As Long Private Declare Function SetTextColor Lib "gdi32" (ByVal hDC As Long, ByVal crColor As Long) As Long Private Declare Function SetBkMode Lib "gdi32" (ByVal hDC As Long, ByVal nBkMode As Long) As Long
Private Const TRANSPARENT As Long = 1 Private Const GWL_WNDPROC As Long = -4 Private Const ODT_BUTTON As Long = 4 Private Const ODS_SELECTED As Long = &H1 Private Const WM_DESTROY As Long = &H2 Private Const WM_DRAWITEM As Long = &H2B Private Const DT_HCENTER As Long = &H1 Private Const DT_TOP As Long = &H0 Private Const DT_VCENTER As Long = &H4 Private Const DT_BOTTOM As Long = &H8 Private Const DT_SINGLELINE As Long = &H20 'chris added Private Const DT_WORDBREAK As Long = &H10 Public Const DT_CHARSTREAM = 4 ' Character-stream, PLP Public Const DT_EXPANDTABS = &H40 Public Const DT_EXTERNALLEADING = &H200 Public Const DT_LEFT = &H0 Public Const DT_NOCLIP = &H100 Public Const DT_CENTER As Long = &H1 Public Const DT_CALCRECT = &H400 Public Const DT_INTERNAL = &H1000
Public Const TA_CENTER = 6 Public Const TA_UPDATECP = 1 Public Const TA_BASELINE = 24 Public Const DT_METAFILE = 5 ' Metafile, VDM Public Const DT_PLOTTER = 0 ' Vector plotter Public Const DUPLICATE = &H6
Public Const WM_GETTEXT = &HD Public Const WM_GETMINMAXINFO = &H24 Public Const WM_GETFONT = &H31 Public Const WM_COPY = &H301 Public Const WM_GETTEXTLENGTH = &HE Public Const WM_COPYDATA = &H4A Public Const WM_PASTE = &H302
Private Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type
Private Type DRAWITEMSTRUCT CtlType As Long CtlID As Long ItemID As Long ItemAction As Long ItemState As Long hWndItem As Long hDC As Long rcItem As RECT ItemData As Long End Type
Public Enum AlignText AlignTop = DT_TOP AlignCenter = DT_VCENTER AlignBottom = DT_BOTTOM ThreeD = DT_VCENTER Or DT_BOTTOM End Enum
Public Sub SetForeColor(Button As CommandButton, ByVal ForeColor As OLE_COLOR, Optional ByVal Alignment As AlignText = AlignCenter)
Dim hWndPnt As Long
With Button hWndPnt = GetParent(.hWnd) If GetProp(hWndPnt, PropSubclass) = 0 Then 'not yet subclassed SetProp hWndPnt, PropSubclass, GetWindowLong(hWndPnt, GWL_WNDPROC) SetWindowLong hWndPnt, GWL_WNDPROC, AddressOf DrawButtonProc End If SetProp .hWnd, PropCustom, True SetProp .hWnd, PropForeColor, ForeColor SetProp .hWnd, PropAlign, Alignment .Refresh End With
End Sub
Public Sub UnsetForeColor(Button As CommandButton)
With Button RemoveProp .hWnd, PropCustom RemoveProp .hWnd, PropForeColor RemoveProp .hWnd, PropAlign .Refresh End With
End Sub
Private Function DrawButtonProc(ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim lOldProc As Long Dim di As DRAWITEMSTRUCT Dim s As String Dim VA As AlignText
lOldProc = GetProp(hWnd, PropSubclass) DrawButtonProc = CallWindowProc(lOldProc, hWnd, wMsg, wParam, lParam) Select Case wMsg Case WM_DRAWITEM CopyMemory di, ByVal lParam, Len(di) With di If .CtlType = ODT_BUTTON Then If GetProp(.hWndItem, PropCustom) Then VA = GetProp(.hWndItem, PropAlign) With .rcItem Select Case VA Case DT_TOP .Top = .Top + 4 Case DT_BOTTOM .Bottom = .Bottom - 4 Case ThreeD .Left = .Left - 1 .Top = .Top - 1 .Right = .Right - 1 .Bottom = .Bottom - 1 VA = AlignCenter End Select If (di.ItemState And ODS_SELECTED) = ODS_SELECTED Then 'Button is in down state - offset the text .Left = .Left + 1 .Top = .Top + 1 .Right = .Right + 1 .Bottom = .Bottom + 1 End If End With SetBkMode .hDC, TRANSPARENT s = String$(255, 0) GetWindowText .hWndItem, s, Len(s) s = Left$(s, InStr(s, Chr$(0)) - 1) SetTextColor .hDC, GetProp(.hWndItem, PropForeColor) 'Command4 was chosen as the 'multi line button, let's do 'all the others first '(Command4's ID# is 2) If di.CtlID <> 2 Then DrawText .hDC, s, Len(s), .rcItem, DT_SINGLELINE Or DT_HCENTER Or VA Else 'we have to fiddle with the 'RECT to move the text down 'vertically depending upon the 'size of the button. This method 'could use some help. With .rcItem .Top = .Top + 46 End With 'draw the multi line text. 'it is centered horizontally but 'at a Y coordinate of 0 unless 'we do the above DrawText .hDC, s, Len(s), .rcItem, DT_WORDBREAK Or TA_CENTER Or DT_HCENTER End If End If End If End With Case WM_DESTROY If lOldProc Then 'is subclassed SetWindowLong hWnd, GWL_WNDPROC, lOldProc RemoveProp hWnd, PropSubclass End If End Select
End Function
'*** I din form *** Private Sub Form_Load() SetForeColor Command1, vbRed End Sub
Private Sub Form_Unload(Cancel As Integer) UnsetForeColor Command1 End Sub
Du kan selvfølgelig ændre flere knappers tekstfarve ved at kopier koden i form_load & form_unload. Du kan også ændre farven i "runtime" altså mens programmet kører... -Sion
Ude i din menu i højreside skal du lave de formateringer for din knap.....der er noget color eller bgcolor (så ved du hvad jeg snakker om, hedder måske noget andet, og det kan endda være du skal sætte tekstfarve ved at trykke font)......
Hvis du kun lige er startet på VB, så accepter, at du ikke umiddelbart kan ændre farven. Det er ikke en property der er tilgængelig umiddelbart, og det kræver en masse(!) fix-fakserier at skifte farven, som jeg tvivler på at du er interesseret i. Når tubber siger, at du kan kikke i dit property-vindue, så er det fordi tubber vist ikke selv har prøvet at kikke - for du kan nemlig ikke finde et sted at ændre farven på teksten...
PS: Du kan kode dine egne kontroller i VB - så er du ikke glad for den knap der er med som default, kan du lave en ny, een gang for alle, som du kan bruge i alle dine programmer. Bare til orientering :o)
En knap som jeg kan give properties jeg kan ændre på lige som den standart knap?
Men som sagt jeg er n00bi VBer... så det er nok ikke lige noget jeg gør
Hvis man laver programmer i VB.NET kræver det så at computeren hvor programmer køre har installeret .NET Framework? Eller er det noget man kan vælge i VB.NET? Om det skal være almindelig program eller et .NET program?
Jeg er næsten 100% på at du skal have .NET framework for at kunne kompilere dine programmer - men det er jo også gratis, så det er vel til at leve med. Jeg vil tro, at du sagtens kan kompilere til en .exe, der kan køre uden frameworket - men har 0 erfaring med VB.NET. Skal dog snart i gang med ASP.NET, og har i den forbindelse haft god glæde af www.gotdotnet.com/ og www.snakegully.nu/tech/dotnet.html til at fatte principperne bag .NET frameworket
Husk at lukke dine spørgsmål, når du har fået svar nok - sion har vel fortjent sine 5 points :o)
Det kræver, at man laver alle knapper som grafik, med den rigtige baggrundsfarve osv - ret klodset, og ikke særlig dynamisk. Man kan ikke skifte caption, font osv (jo, men så skal man loade et nyt billede). Du har ret - det er en løsning - men en ret uelegant en, synes jeg, og det er da noget snavs i forhold til at kunne bestemme fontcolor ved at sætte en property :o)
Der skal overhovedet ikke herske nogen tvivl om at det er klodset :O) Men nu lider jeg selv af API-fobi så 80.000 linier API kald for at lave en tekstfarve om... Hvorfor kunne de ikke også bare ahve lavet en forecolor på den?
Jeg skal nok holde mine dårlige idéer for mig selv ;O)
Kan huske den gang jeg havde... VAR det en 12" grøn/sort og jeg blev helt vild misundelig da en af mine bekendte fik en ORANGE / sort !!! COMMODORE VIC20 RULEZ :O)
Nej - du tager ikke fejl - min egen fejl at gå i krig med et spørgsmål, hvor der er afsat 3 gange færre points end anbefalet minimum ;o)
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.