Tewl
xTeam
C# & VB Developer
Posts: 495
|
Post by Tewl on Dec 7, 2003 1:28:58 GMT -5
I am kind of in a hurry about getting this working so I'm posting it on a few forums I don't really expect to get the answer from here because not many ppl use .Net but you never know I might luck out. Anyways this example is writting in C# if you know the answer to this question in Visual Basic .Net I can use that as well. I am tryin to set the text of the selected tab to bold but theres no property to do this so I tried to override the drawing of the tabs. private void tab_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) { tabArea = tab.GetTabRect(e.Index); tabTextArea = (RectangleF)tab.GetTabRect(e.Index); StringFormat drawFormat = new StringFormat(); drawFormat.Alignment = StringAlignment.Center; SolidBrush b = new SolidBrush(Color.Black); Graphics g = e.Graphics; Pen p = new Pen(Color.White); System.Drawing.Font font; if (e.Index == tab.SelectedIndex) { font = new Font(tab.Font.FontFamily,tab.Font.Size , FontStyle.Bold, GraphicsUnit.Point); } else { font = tab.Font; g.DrawRectangle(p, tabArea); } g.DrawString(tab.TabPages[e.Index].Text, font, b, tabTextArea, drawFormat); }
I have a few problems with this. One is it puts the text at the top of the tab instead of the middle I'm not sure how to fix that. Second it's writting the text as multiline. Third i draw the line but with pen but don't know how to keep it from appearing under the selected tab. But the bold works ;D Does anyone kno how i can fix these problems or another way of doing this?
|
|
|
Post by Webagent on Dec 7, 2003 4:58:57 GMT -5
well the only thing I do know is how to change the tab color, maybe you can get some info out of this how you have to redraw the whole thing and use bald text instead of selected color, btw this is normal TabStrip, not SSTab
Option Explicit
Private Const WM_DRAWITEM = &H2B Private Const WM_PRINTCLIENT = &H318 Private Const GWL_WNDPROC = (-4) Private Const GWL_STYLE = (-16) Private Const GWL_EXSTYLE = (-20)
' A rectangle! Imagine that! Private Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type
' Ownerdrawn TabStrip style. Private Const TCS_OWNERDRAWFIXED = &H2000
' ************************************************************************** ' GDI32 API Functions used for changing the background color of the tabstrip ' ************************************************************************** Private Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long Private Declare Function FillRect Lib "user32" (ByVal hdc As Long, lpRect As RECT, ByVal hBrush As Long) As Long Private Declare Function GetClientRect Lib "user32" (ByVal hWnd As Long, lpRect As RECT) As Long ' ************************************************************************** ' Used to subclass the tabstrip and its parent ' ************************************************************************** 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 GetParent Lib "user32" (ByVal hWnd 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 RemoveProp Lib "user32" Alias "RemovePropA" (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 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 ' ************************************************************************** ' Used for our slimy pointer hack for the "safe" subclassing method ' ************************************************************************** Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)
Public Function subclassTabStrip(aTabstrip As TabStrip)
Dim origProc As Long ' Original process address Dim aStyle As Long ' Original style for the tabstrip ' Set the OwnerDrawn style ' get the original style aStyle = GetWindowLong(aTabstrip.hWnd, GWL_STYLE) ' add in the ownerdrawn style aStyle = aStyle Or TCS_OWNERDRAWFIXED ' replace the style with our "ownerdrawn" one SetWindowLong aTabstrip.hWnd, GWL_STYLE, aStyle ' subclass the tabstrip - used to change the background color of the tabstrip ' Redirect our messages to the function "TabStripProc" origProc = SetWindowLong(aTabstrip.hWnd, GWL_WNDPROC, AddressOf TabStripProc) ' Store the original process address against the tabstrip's handle SetProp aTabstrip.hWnd, "OrigTabStripProc", origProc ' Store a "tabheight" value so we can make the tab background a different color ' than the rest of the tabstrip control (want the tab backgrounds, i.e. the ' parts that aren't really tabs to be the same color as the form) SetProp aTabstrip.hWnd, "TabHeight", aTabstrip.TabFixedHeight / Screen.TwipsPerPixelY
' subclass the parent - used to capture the WM_DRAWITEM message ' Redirect our messages for the tabstrip's parent to "TabStripOwnerProc" origProc = SetWindowLong(GetParent(aTabstrip.hWnd), GWL_WNDPROC, AddressOf TabStripOwnerProc) ' Store the original window process's address against its handle (we're assuming ' only in name that its owner is a form - it could be a picturebox or something) SetProp GetParent(aTabstrip.hWnd), "OrigWindowProc", origProc ' Store a pointer to the tabstrip's form so we can implement our slimy hack ' "safe" subclassing method SetProp GetParent(aTabstrip.hWnd), "ODFormHwnd", ObjPtr(aTabstrip.Parent)
End Function Public Function unsubclassTabstrip(aTabstrip As TabStrip) Dim origProc As Long 'Unsubclass the parent
' Get the original process address for the tabstrip's parent origProc = GetProp(GetParent(aTabstrip.hWnd), "OrigWindowProc") ' Redirect all messages back to this parent SetWindowLong GetParent(aTabstrip.hWnd), GWL_WNDPROC, origProc ' Remove the entries from windows' internal database RemoveProp GetParent(aTabstrip.hWnd), "OrigWindowProc" RemoveProp GetParent(aTabstrip.hWnd), "ODFormHwnd"
'unsubclass the tabstrip ' Get the original process for the tabstrip origProc = GetProp(aTabstrip.hWnd, "OrigTabStripProc") ' Redirect all messages back to its original process SetWindowLong aTabstrip.hWnd, GWL_WNDPROC, origProc ' Remove the entries from windows' internal database RemoveProp aTabstrip.hWnd, "OrigTabStripProc" RemoveProp aTabstrip.hWnd, "TabHeight" End Function
Private Function TabStripProc(ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long Dim oldWndProc As Long ' Original process address for the tabstrip Dim aRect As RECT ' Rectangle structure for drawing and filling with colors Dim abrush As Long ' brush object for filling with colors Dim aTabHeight As Long ' height of the tab items ' Get the original tabstrip process address oldWndProc = GetProp(hWnd, "OrigTabStripProc") ' If its going to "print" the tabstrip shape on the client area, we ' need to paint the background colors first If wMsg = WM_PRINTCLIENT Then ' Retrieve the height of the tabs as stored when the tabstrip was ' first subclassed aTabHeight = GetProp(hWnd, "TabHeight") ' Get the dimensions for the tabstrip's area GetClientRect hWnd, aRect ' Create a brush with our "mainTabstrip" background color. this is the ' color that we're filling the main body of the tabstrip with abrush = CreateSolidBrush(getAppColor("mainTabstrip")) ' Adjust our fill area to down to the top of the tabs aRect.Bottom = aRect.Bottom - aTabHeight + 1 ' Fill the main area of the tabstrip FillRect wParam, aRect, abrush ' clean up the brush object DeleteObject abrush ' Set the fill area to only the tab area. This doesn't really fill the ' tabs as much as it makes the area around the tabs the right color. ' that way we can match it to the backcolor of our form. aRect.Bottom = aRect.Bottom + aTabHeight aRect.Top = aRect.Bottom - aTabHeight + 1 ' Create the brush the same color as the backcolor of our form abrush = CreateSolidBrush(getAppColor("formbackground")) ' Fill our selected area FillRect wParam, aRect, abrush ' clean up the brush object DeleteObject abrush End If ' Invoke whatever the default process for our tabstrip, including the ' WM_PRINTCLIENT message that will draw the tabstrip's border TabStripProc = CallWindowProc(oldWndProc, hWnd, wMsg, wParam, lParam) End Function
Private Function TabStripOwnerProc(ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long Dim aform As fMain ' a dummy instance of fMain for our slimy "safe" subclassing method Dim oldWndProc As Long ' original windows process for the form Dim lPtr As Long ' pointer to the instance of our fMain object ' Get the original oldWndProc = GetProp(hWnd, "OrigWindowProc") If wMsg = WM_DRAWITEM Then ' Get our form's pointer address lPtr = GetProp(hWnd, "ODFormHwnd") ' check to make sure the pointer address is stored and valid If lPtr <> 0 Then ' splice in our address to the dummy instance CopyMemory aform, lPtr, 4 ' Draw the tabstrip caption item aform.DrawTabItem hWnd, lParam ' erase the address from our dummy instance CopyMemory aform, 0&, 4 End If Else ' Invoke the default process for the tabstrip's ownerform. TabStripOwnerProc = CallWindowProc(oldWndProc, hWnd, wMsg, wParam, lParam) End If
End Function
|
|
|
Post by Webagent on Dec 7, 2003 5:09:24 GMT -5
^^ that was in a module named TabStrip , below goes in the form that has the tabstrip br] ' ' Owner draw state. ' Private Const ODS_SELECTED = &H1 Private Const ODS_GRAYED = &H2 Private Const ODS_DISABLED = &H4 Private Const ODS_CHECKED = &H8 Private Const ODS_FOCUS = &H10 Private Const ODS_DEFAULT = &H20 Private Const ODS_COMBOBOXEDIT = &H1000 Private Const ODS_HOTLIGHT = &H40 Private Const ODS_INACTIVE = &H80
' A rectangle. DUH! Private Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type
' A point type Private Type POINTAPI X As Long Y As Long End Type
' A draw item structure type for OD Controls 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
' A Tabstrip control item structure Private Type TCITEM mask As Long dwState As Long dwStateMask As Long pszText As Long cchTextMax As Long iImage As Long lParam As Long End Type
' A tabstrip control information constant Private Const TCIF_TEXT = &H1
' Messages to send to a Tabstrip control to retrieve information Private Const TCM_FIRST = &H1300 Private Const TCM_GETCURSEL = (TCM_FIRST + 11) Private Const TCM_GETITEMA = (TCM_FIRST + 5) Private Const TCM_GETITEM = TCM_GETITEMA
' A null/hollow brush... lets us create a hollow rectangle Private Const NULL_BRUSH As Long = 5
' CreatePen() API constants Private Const PS_SOLID As Long = 0 Private Const PS_DASH As Long = 1 Private Const PS_DASHDOT As Long = 3 Private Const PS_DASHDOTDOT As Long = 4 Private Const PS_DOT As Long = 2
' DrawText() API constants Private Const DT_LEFT As Long = &H0 Private Const DT_CENTER As Long = &H1 Private Const DT_RIGHT As Long = &H2 Private Const DT_TOP As Long = &H0 Private Const DT_BOTTOM As Long = &H8 Private Const DT_VCENTER As Long = &H4 Private Const DT_WORDBREAK = &H10 Private Const DT_MULTILINE As Long = (&H1) Private Const DT_SINGLELINE As Long = &H20 Private Const DT_CALCRECT As Long = &H400
' ******************************************************************************** ' GDI32 stuff ' ******************************************************************************** Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long Private Declare Function CreatePen Lib "gdi32" (ByVal nPenStyle As Long, ByVal nWidth As Long, ByVal crColor As Long) As Long Private Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long Private Declare Function DrawFocusRect Lib "user32" (ByVal hdc As Long, lpRect As RECT) 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 FillRect Lib "user32" (ByVal hdc As Long, lpRect As RECT, ByVal hBrush As Long) As Long Private Declare Function GetStockObject Lib "gdi32" (ByVal nIndex As Long) As Long Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long Private Declare Function SetTextColor Lib "gdi32" (ByVal hdc As Long, ByVal crColor As Long) As Long Private Declare Function SetBkColor 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 Declare Function Rectangle Lib "gdi32" (ByVal hdc As Long, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
' Used to store the original procedure addresses by individual window handle 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
' Miscellaneous API functions for manipulating the types and retrieving information Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long) Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hWnd As Long, ByVal msg As Long, ByVal wParam As Long, lParam As Any) As Long
Friend Sub DrawTabItem(ByVal hWnd As Long, ByVal drawInfoPtr As Long)
Dim aDIS As DRAWITEMSTRUCT ' A OD draw item structure Dim aTCItem As TCITEM ' a Tabstrip control item structure Dim abrush As Long ' a temporary brush object for filling areas with color Dim aStr As String ' temporary variable for converting caption text from unicode Dim aStrByte() As Byte ' a byte buffer for storing the caption string (unicode) Dim aPen As Long ' a temporary pen item for drawing the border color Dim origPen As Long ' original pen for the DIS.hDC property ' Make sure that the hwnd and drawInfo pointers are valid If hWnd = 0 Or drawInfoPtr = 0 Then Exit Sub ' Copy information from our pointer address into the DrawItem structure CopyMemory aDIS, ByVal drawInfoPtr, Len(aDIS) With aDIS ' If the item is the "selected" tab, we're gonna do a bunch of ' stuff to it. If ((aDIS.itemState And ODS_SELECTED) = ODS_SELECTED) Then ' Create a brush the color of "mainTabstrip" abrush = CreateSolidBrush(getAppColor("mainTabstrip")) ' Fill in the whole tab caption area with "mainTabstrip" color first FillRect .hdc, .rcItem, abrush ' clean up our brush resource DeleteObject abrush ' adjust the "top" of our painting area down 2 pixels .rcItem.Top = .rcItem.Top + 2 ' create another brush the "selected" color abrush = CreateSolidBrush(getAppColor("Selected")) ' fill in the area FillRect .hdc, .rcItem, abrush ' clean up our brush resource DeleteObject abrush ' create a new pen of color "BorderColor" and select it into our device context origPen = SelectObject(.hdc, CreatePen(0, 1, getAppColor("BorderColor"))) ' create a hollow brush and select it into the device context abrush = SelectObject(.hdc, GetStockObject(NULL_BRUSH)) ' draw our rectange of color "BorderColor" Rectangle .hdc, .rcItem.Left + 3, .rcItem.Top + 1, .rcItem.Right - 3, .rcItem.Bottom - 3 ' replace the original pen object and delete our pen of color "BorderColor" DeleteObject SelectObject(.hdc, origPen) ' replace the original brush SelectObject .hdc, abrush ' adjust the "top" of the item down a little more (2 more pixels) .rcItem.Top = .rcItem.Top + 2 Else ' We're just going to fill in everything with the appropriate color abrush = CreateSolidBrush(getAppColor("mainTabstrip")) .rcItem.Top = .rcItem.Top - 2 FillRect .hdc, .rcItem, abrush DeleteObject abrush End If ' Set which information we want to retrieve aTCItem.mask = TCIF_TEXT ' Create a buffer for our caption ReDim aStrByte(0 To 127) ' set a pointer to our string buffer aTCItem.pszText = VarPtr(aStrByte(0)) ' set the size of our buffer aTCItem.cchTextMax = UBound(aStrByte) ' get the tab caption information SendMessage tbsPage.hWnd, TCM_GETITEM, aDIS.itemID, aTCItem ' Convert the caption from unicode to a regular string aStr = StrConv(aStrByte(), vbUnicode) ' trim out the null characters aStr = VBA.Left$(aStr, InStr(aStr, Chr$(0)) - 1) ' Set our text color to the right color (either "SelectedText" or "GeneralText") SetTextColor .hdc, IIf(aDIS.itemState And ODS_SELECTED, getAppColor("SelectedText"), getAppColor("GeneralText")) ' Make the text print transparently SetBkMode .hdc, 1 ' Draw the caption text DrawText .hdc, aStr, Len(aStr), .rcItem, DT_VCENTER Or DT_CENTER End With
End Sub
name of the tabstrip = tbsPage and to draw in form load = modTabStrip.subclassTabStrip tbsPage
|
|
Tewl
xTeam
C# & VB Developer
Posts: 495
|
Post by Tewl on Dec 7, 2003 7:49:54 GMT -5
eh ill think ill keep looking lol
|
|
Tewl
xTeam
C# & VB Developer
Posts: 495
|
Post by Tewl on Dec 7, 2003 19:52:32 GMT -5
Until I figure this out I have decided to use a grey to black
|
|