hi Micha,
I have started writing some wrappers for the grid control. I have come up with a feature request:
you have set it up so that you can change the color of every column in a row by setting the column to -1. This is quite nice. Can you also change SetGridCtrlRowColCountID to only modify the number of rows if the colum is set to -1?
Also, some function to get properties back from the control? (number of rows, cols, etc)
Thanks,
--engunneer
Here is a very early version of the wrapper code:
GridControl.ahk:
Code:
;GridControl.ahk
;Functions to wrap around Micha's excellent grid control
CtrlInit(GUItitle, CtrlDllPath="")
{
WinGet, HWND, ID, %GUItitle%
If (CtrlDllPath ="")
CtrlDllPath = AHKCtrlSupport.dll
; Load the dll to speed up the following calls
hModule := DllCall("LoadLibrary", "str", CtrlDllPath)
nRC = 0
DetectHiddenWindows, On
MainHWND := WinExist(A_ScriptFullPath . " ahk_class AutoHotkey")
nRC := DllCall("AHKCtrlSupport\Init", int, MainHWND, int, HWND, int, hModule, "Cdecl Int")
If (Errorlevel <> 0) || (nRC = 0)
{
MsgBox, Error while calling Init Errorlevel: %Errorlevel% - RC: %nRC%
Return
}
Return HWND
}
CtrlGridCreate(GUItitle, ByRef GridCtrlHwnd, ByRef nID, options = "")
{
;Needed for Copy/Paste of grid-control
DllCall("ole32\OleInitialize", "Uint", 0)
; --------------- default options ---------------------------------------
; -----------------------------------------------------------------------
width = 300
height = 300
x = 5
y = 5
; --------------- parse options -----------------------------------------
; -----------------------------------------------------------------------
Stringsplit option, options, %A_space%`,
loop %option0% {
Stringleft, option, option%A_index%, 1
Stringtrimleft, value, option%A_index%, 1
if (option = "w")
width := value
if (option = "h")
height := value
if (option = "x")
x := value
if (option = "y")
y := value
}
; --------------- Create a Grid Ctrl ------------------------------------
; -----------------------------------------------------------------------
dwexStyle = 0
nID = 2
WinGet, HWND, ID, %GUItitle%
GridCtrlHwnd := DllCall("AHKCtrlSupport\CreateGridCtrl"
, int, dwexStyle
, int, x
, int, y
, int, width
, int, height
, int, HWND
, int, nID
, "Cdecl Int")
If (Errorlevel <> 0) || (GridCtrlHwnd = 0)
{
MsgBox, Error while calling CreateGridCtrl Errorlevel: %Errorlevel% - RC: %GridCtrl%
Return
}
Return nID
}
CtrlGridSetRowColCount(nID, options = "")
{
;default to not changing, then the options can change one or both
GCrows = -1
GCcols = -1
; --------------- parse options -----------------------------------------
; -----------------------------------------------------------------------
Stringsplit option, options, %A_space%`,
loop %option0% {
Stringleft, option, option%A_index%, 1
Stringtrimleft, value, option%A_index%, 1
if (option = "r")
GCrows := value
if (option = "c")
GCcols := value
}
; --------------- Set Row and Columncount. rows, cols -------------------
; -----------------------------------------------------------------------
;
nRC := DllCall("AHKCtrlSupport\SetGridCtrlRowColCountID"
, int, nID
, int, GCrows
, int, GCcols
, "Cdecl Int")
If (Errorlevel <> 0) || (nRC = 0)
{
MsgBox, Error while calling SetGridCtrlRowColCountID Errorlevel: %Errorlevel% - RC: %nRC%
Return
}
}
CtrlGridSetFixedRowColCount(nID, options = "")
{
global GCfrows, GCfcols ;global so they can be static
; --------------- parse options -----------------------------------------
; -----------------------------------------------------------------------
Stringsplit option, options, %A_space%`,
loop %option0% {
Stringleft, option, option%A_index%, 1
Stringtrimleft, value, option%A_index%, 1
if (option = "r")
GCfrows := value
if (option = "c")
GCfcols := value
}
; --------------- Set fixed Rows and Columns. ---------------------------
; --------- 1. parameter = rows, second parameter cols-------------------
; -------------- row and column are 1 based !!
;
nRC := DllCall("AHKCtrlSupport\SetGridCtrlFixedRowColCountID"
, int, nID
, int, GCfrows
, int, GCfcols
, "Cdecl Int")
If (Errorlevel <> 0) || (nRC = 0)
{
MsgBox, Error while calling SetGridCtrlFixedRowColCountID Errorlevel: %Errorlevel% - RC: %nRC%
Return
}
}
CtrlGridSetCellText(GridCtrlHwnd, row, col, Text = "")
{
; -------------- row and column are 0 based !!
nRC := DllCall("AHKCtrlSupport\SetGridItemText"
, int, GridCtrlHwnd
, int, row
, int, col
, str, Text
, "Cdecl Int")
If (Errorlevel <> 0) || (nRC = 0)
{
MsgBox, Error while calling SetItemText Errorlevel: %Errorlevel% - RC: %nRC%
Return
}
}
CtrlGridSetRowHeight(GridCtrlHwnd, Row2Change, HeightofRow = 20, Refresh = 1)
{
; --------------- Set height of row in pixels----------------------------
; -----------------------------------------------------------------------
; --------------- Causes grid refresh by default ------------------------
nRC := DllCall("AHKCtrlSupport\SetGridRowHeight"
, int, GridCtrlHwnd
, int, Row2Change
, int, HeightofRow
, "Cdecl Int")
If (Errorlevel <> 0) || (nRC = 0)
{
MsgBox, Error while calling SetGridRowHeight Errorlevel: %Errorlevel% - RC: %nRC%
Return
}
If (Refresh = 1)
CtrlGridRefresh(GridCtrlHwnd)
}
CtrlGridSetColWidth(GridCtrlHwnd, Col2Change, WidthOfRow = 60, Refresh = 1)
{
; --------------- Set width of colum in pixels---------------------------
; -----------------------------------------------------------------------
; --------------- Causes grid refresh by default ------------------------
nRC := DllCall("AHKCtrlSupport\SetGridColumnWidth"
, int, GridCtrlHwnd
, int, Col2Change
, int, WidthOfRow
, "Cdecl Int")
If (Errorlevel <> 0) || (nRC = 0)
{
MsgBox, Error while calling SetGridColumnWidth Errorlevel: %Errorlevel% - RC: %nRC%
Return
}
If (Refresh = 1)
CtrlGridRefresh(GridCtrlHwnd)
}
CtrlGridRefresh(GridCtrlHwnd)
{
; --------------- After changing height, width and other things ---------
; ----------------the grid must be redrawn. Call this function-----------
; ----------------to refresh the grid------------------------------------
nRC := DllCall("AHKCtrlSupport\GridRefresh"
, int, GridCtrlHwnd
, "Cdecl Int")
If (Errorlevel <> 0) || (nRC = 0)
{
MsgBox, Error while calling GridRefresh Errorlevel: %Errorlevel% - RC: %nRC%
Return
}
}
CtrlGridSetLines(GridCtrlHwnd, options = "")
{
nValh = 0
nValv = 0
; --------------- parse options -----------------------------------------
; -----------------------------------------------------------------------
Stringsplit option, options, %A_space%`,
loop %option0% {
Stringleft, option, option%A_index%, 1
Stringtrimleft, value, option%A_index%, 1
if (option = "h")
if (value = 1)
nValh = 1
else
nValh = 0
if (option = "v")
if (value = 1)
nValv = 2
else
nValv = 0
}
nVal := nValh + nValv
;Set both lines: 0= none; 1=horizontal, 2=vertical, 3=both
; --------------- How to draw lines of the grid--------------------------
; -----------------------------------------------------------------------
nRC := DllCall("AHKCtrlSupport\SetGridLines"
, int, GridCtrlHwnd
, int, nVal
, "Cdecl Int")
If (Errorlevel <> 0) || (nRC = 0)
{
MsgBox, Error while calling SetGridLines Errorlevel: %Errorlevel% - RC: %nRC%
Return
}
}
CtrlGridCopyPasteMode(GridCtrlHwnd, allowCommaDelimiter)
{
; --------------- When pasting 2,3 or 2{tab}3 the result is splitted-----
; ----------------into 2 different cells---------------------------------
; allowCommaDelimiter = 0 means only Tab to delimit!
nRC := DllCall("AHKCtrlSupport\GridIgnoreCommaDotPaste"
, int, GridCtrlHwnd
, int, allowCommaDelimiter
, "Cdecl Int")
If (Errorlevel <> 0) || (nRC = 0)
{
MsgBox, Error while calling SetEditible Errorlevel: %Errorlevel% - RC: %nRC%
Return
}
}
CtrlGridSetCellType(GridCtrlHwnd, nRow, nCol, nType = -1)
{
; --------------- Set the new type of a cell. Syntax: -----------------------
; --------------- 1: Text, 2: Checkbox, 3: DateTime, 4: URL, 5: Combo, 6: Numeric
; --------------- You can pass either the number or name of desired type
If nType = Text
nType = 1
If nType = CheckBox
nType = 2
If nType = DateTime
nType = 3
If nType = URL
nType = 4
If nType = Combo
nType = 5
If nType = Numeric
nType = 6
if nType = -1
return
nRC := DllCall("AHKCtrlSupport\GridSetCellType"
, int, GridCtrlHwnd
, int, nRow
, int, nCol
, int, nType
, "Cdecl Int")
If (Errorlevel <> 0) || (nRC = 0)
{
MsgBox, Error while calling GridSetCellType off Errorlevel: %Errorlevel% - RC: %nRC%
Return
}
}
CtrlGridSetCellFont(GridCtrlHwnd, options = "", Refresh = 1)
{
;defaults
Row2Change = -1
Col2Change = -1
Height = -1
Width = -1
Escapement = -1
Orientation = -1
Weight = -1
Italic = -1
Underline = -1
Strikeout = -1
CharSet = -1
OutPrec = -1
ClipPrec = -1
Quality = -1
Pitch = -1
GridFontName = -1
; --------------- parse options -----------------------------------------
; -----------------------------------------------------------------------
Stringsplit option, options, %A_space%`,
loop %option0% {
Stringleft, option, option%A_index%, 1
Stringtrimleft, value, option%A_index%, 1
if (option = "r")
Row2Change := value
if (option = "c")
Col2Change := value
if (option = "h")
Height := value
if (option = "w")
Width := value
if (option = "b")
Weight := value
if (option = "i")
Italic := value
if (option = "u")
Underline := value
if (option = "s")
Strikeout := value
if (option = "o")
Orientation := value
if (option = "e")
Escapement := value
if (option = "q")
Quality := value
if (option = "p")
Pitch := value
if (option = "f")
{
GridFontName := value
StringReplace, GridFontName, GridFontName, _ , %A_space%, All
}
}
; Prototype (DWORD hWnd, DWORD nRow, DWORD nCol, DWORD Height, DWORD Width,
; DWORD Escapement, DWORD Orientation, DWORD Weight, DWORD Italic,
; DWORD Unterline, DWORD Strikeout, DWORD CharSet, DWORD OutPrec,
; DWORD ClipPrec, DWORD Quality, DWORD Pitch, LPCTSTR fontname)
nRC := DllCall("AHKCtrlSupport\SetGridItemFont"
, int, GridCtrlHwnd
, int, Row2Change
, int, Col2Change
, int, Height
, int, Width
, int, Escapement
, int, Orientation
, int, Weight
, int, Italic
, int, Underline
, int, Strikeout
, int, CharSet
, int, OutPrec
, int, ClipPrec
, int, Quality
, int, Pitch
, str, GridFontName
, "Cdecl Int")
If (Errorlevel <> 0) || (nRC = 0)
{
MsgBox, Error while calling SetGridItemFont Errorlevel: %Errorlevel% - RC: %nRC%
Return
}
If (Refresh = 1)
CtrlGridRefresh(GridCtrlHwnd)
}
;not yet working!!
CtrlGridSetCellFormat(GridCtrlHwnd, options = "", Refresh = 1)
{
;default
GridCellFormat = 0
; --------------- parse options -----------------------------------------
; -----------------------------------------------------------------------
Stringsplit option, options, %A_space%`,
loop %option0% {
Stringleft, option, option%A_index%, 1
Stringtrimleft, value, option%A_index%, 1
if (option = "r")
Row2Change := value
if (option = "c")
Col2Change := value
if (option = "h")
if (InStr(value ,"l")=1)
GridCellFormat += 0
else if (InStr(value ,"c")=1)
GridCellFormat += 1
else if (InStr(value ,"r")=1)
GridCellFormat += 2
if (option = "c")
if (value = "t")
GridCellFormat += 0
else if (value = "c") ;center
GridCellFormat += 1
else if (value = "m") ;middle = center
GridCellFormat += 1
else if (value = "b")
GridCellFormat += 2
if (option = "a") ;advanced
GridCellFormat += value
}
msgbox, %GridCellFormat%
; -----------------------------------------------------------------------
; --------------- Set cell format ---------------------------------------
; -----------------------------------------------------------------------
/*
See MSDN for more informataion
#define DT_TOP 0x00000000
#define DT_LEFT 0x00000000
#define DT_CENTER 0x00000001
#define DT_RIGHT 0x00000002
#define DT_VCENTER 0x00000004
#define DT_BOTTOM 0x00000008
#define DT_WORDBREAK 0x00000010
#define DT_SINGLELINE 0x00000020
#define DT_EXPANDTABS 0x00000040
#define DT_TABSTOP 0x00000080
#define DT_NOCLIP 0x00000100
#define DT_EXTERNALLEADING 0x00000200
#define DT_CALCRECT 0x00000400
#define DT_NOPREFIX 0x00000800
#define DT_INTERNAL 0x00001000
*/
GuiControlGet, Row2Change, , GroupRow
GuiControlGet, Col2Change, , GroupCol
GuiControlGet, GridCellFormat, , GridCellFormat
;MsgBox, %GridCellFormat% - %Col2Change% - %Row2Change%
nRC := DllCall("AHKCtrlSupport\SetGridCellFormat"
, int, GridCtrlHwnd
, int, Row2Change
, int, Col2Change
, int, GridCellFormat
, "Cdecl Int")
If (Errorlevel <> 0) || (nRC = 0)
{
MsgBox, Error while calling SetGridCellFormat Errorlevel: %Errorlevel% - RC: %nRC%
Return
}
If (Refresh = 1)
CtrlGridRefresh(GridCtrlHwnd)
}
CtrlUnInit()
{
nRC := DllCall("AHKCtrlSupport\UnInit", "Cdecl Int")
If (Errorlevel <> 0) || (nRC = 0)
{
MsgBox, Error while calling UnInit Errorlevel: %Errorlevel% - RC: %nRC%
Return
}
}
And a script that uses the wrapper
Code:
;ScheduleDisplayer
#Persistent
#Include GridControl.ahk
OnExit, ExitScript
GUItitle = Test Script
Gui, Show, w610 h510, %GUItitle%
CtrlInit(GUItitle)
CtrlGridCreate(GUItitle, GridCtrlHwnd, GridID,"w600 h500 x5 y5")
;these functions use GridID, and as such are 1 based
CtrlGridSetRowColCount(GridID, "r5 c5")
sleep, 750
CtrlGridSetRowColCount(GridID, "r7")
sleep, 750
CtrlGridSetRowColCount(GridID, "c7")
CtrlGridSetFixedRowColCount(GridID, "r1 c1")
;These functions use GridCtrlHwnd, and as such are 0 based
CtrlGridSetCellText(GridCtrlHwnd, 5, 5, "Cell (5,5)")
;Write Data to the grid
loop, 6
{
nCol := a_index
loop, 6
{
Text = Cell(%a_index%,%nCol%)
CtrlGridSetCellText(GridCtrlHwnd, A_index, nCol, Text)
}
}
loop, 6
{
CtrlGridSetRowHeight(GridCtrlHwnd, A_index - 1, 35 ,0) ;do not refresh
}
loop, 6
{
CtrlGridSetColWidth(GridCtrlHwnd, A_index - 1, 100 ,0) ;do not refresh
}
CtrlGridRefresh(GridCtrlHwnd) ;now refresh
Loop, 1
{
CtrlGridSetLines(GridCtrlHwnd, "v1")
sleep, 250
CtrlGridSetLines(GridCtrlHwnd, "") ;h0 and v0 also supported
sleep, 250
CtrlGridSetLines(GridCtrlHwnd, "h1")
sleep, 250
CtrlGridSetLines(GridCtrlHwnd, "h1 v1")
sleep, 250
}
;change cell types
Loop, 6
{
CtrlGridSetCellType(GridCtrlHwnd, A_index, 1, A_index)
}
;populate the Combo control
;set fonts
CtrlGridSetCellText(GridCtrlHwnd, 1, 2, "Strikethrough")
CtrlGridSetCellFont(GridCtrlHwnd, "r1 c2 s1")
CtrlGridSetCellText(GridCtrlHwnd, 2, 2, "Italic")
CtrlGridSetCellFont(GridCtrlHwnd, "r2 c2 i1")
CtrlGridSetCellText(GridCtrlHwnd, 3, 2, "Underline")
CtrlGridSetCellFont(GridCtrlHwnd, "r3 c2 u1")
CtrlGridSetCellText(GridCtrlHwnd, 4, 2, "Courier New")
CtrlGridSetCellFont(GridCtrlHwnd, "r4 c2 fCourier_New")
CtrlGridSetCellText(GridCtrlHwnd, 5, 2, "Tahoma")
CtrlGridSetCellFont(GridCtrlHwnd, "r5 c2 fTahoma")
CtrlGridSetCellText(GridCtrlHwnd, 6, 2, "Arial")
CtrlGridSetCellFont(GridCtrlHwnd, "r6 c2 fArial")
;set formats (Not yet working)
CtrlGridSetCellText(GridCtrlHwnd, 1, 3, "Left")
CtrlGridSetCellFormat(GridCtrlHwnd, "r1 c3 hl")
CtrlGridSetCellText(GridCtrlHwnd, 2, 3, "Center")
CtrlGridSetCellFormat(GridCtrlHwnd, "r2 c3 hc")
CtrlGridSetCellText(GridCtrlHwnd, 3, 3, "Right")
CtrlGridSetCellFormat(GridCtrlHwnd, "r3 c3 hr")
CtrlGridSetCellText(GridCtrlHwnd, 4, 3, "Top")
CtrlGridSetCellFormat(GridCtrlHwnd, "r4 c3 vt")
CtrlGridSetCellText(GridCtrlHwnd, 5, 3, "Middle")
CtrlGridSetCellFormat(GridCtrlHwnd, "r5 c3 hc")
CtrlGridSetCellText(GridCtrlHwnd, 6, 3, "Bottom")
CtrlGridSetCellFormat(GridCtrlHwnd, "r6 c3 hb")
return
GuiClose:
Exitscript:
CtrlUnInit()
ExitApp
EDIT: Updated code 2007/04/17 - 7:21 PDT
EDIT: Updated code 2007/04/18 - 5:11 PDT