Open/Close Tabs with COM

Please post any questions regarding the program here.

Moderator: 2020vision

Open/Close Tabs with COM

Postby laffo16 » Mon Jun 24, 2013 10:18 pm

Hi, is it possible to open/close tabs (new pages) within COM interface?
User avatar
laffo16
 
Posts: 172
Joined: Sun Jan 27, 2008 6:52 pm

Postby laffo16 » Tue Jun 25, 2013 12:00 am

public event ComClass.pricesUpdatedEventHandler pricesUpdated;

anyone know if its possible to setup this event to fire when pricesUpdate for multiple tabs? I have tried changing the tabIndex before adding each event handler but didnt seem to work.
User avatar
laffo16
 
Posts: 172
Joined: Sun Jan 27, 2008 6:52 pm

Postby kiint » Tue Jun 25, 2013 4:43 pm

I'd be interested in this too.
I ended up using a Timer event and then cycling through each tab getting the prices. Obviously being able to do this via an event firing would be more elegant.
kiint
 
Posts: 148
Joined: Tue Feb 23, 2010 12:12 am

Postby laffo16 » Fri Jun 28, 2013 10:45 pm

kiint, from your experience were you only able to get the pricesUpdated event to work for one tab at a time? can anyone else confirm this?
User avatar
laffo16
 
Posts: 172
Joined: Sun Jan 27, 2008 6:52 pm

Postby osknows » Sat Jun 29, 2013 9:41 am

It is possible to fire events on more than one tab. If you don't mind running several COM objects then

CLASS MODULE: clsBA
Code: Select all
Option Explicit

Public WithEvents ba As BettingAssistantCom.ComClass

Private Sub Class_Initialize()
    Set ba = New BettingAssistantCom.ComClass
End Sub

Private Sub Class_Terminate()
    Set ba = Nothing
End Sub

Private Sub ba_betPlaced(ByVal ref As String, ByVal selecId As Long, ByVal avgPriceMatched As Double, ByVal sizeMatched As Double, ByVal resultCode As String, ByVal token As String)

    Debug.Print ref
    Debug.Print selecId
    Debug.Print avgPriceMatched
    Debug.Print sizeMatched
    Debug.Print resultCode
    Debug.Print token

End Sub

Private Sub ba_pricesUpdated()
Dim prices As Variant
Dim priceItem As Variant
Dim i As Long
On Error Resume Next 'Event will cause error in Excel Edit mode
    prices = ba.getPrices
    i = 4
    For Each priceItem In prices
        i = i + 1
        Sheets(ba.tabindex + 1).Cells(i, 1).value = priceItem.Selection
        Sheets(ba.tabindex + 1).Cells(i, 2).value = priceItem.backOdds1
        Sheets(ba.tabindex + 1).Cells(i, 3).value = priceItem.layOdds1
        Sheets(ba.tabindex + 1).Cells(i, 4).value = priceItem.lastMatched
        Sheets(ba.tabindex + 1).Cells(i, 5).value = priceItem.totalMatched
    Next
On Error GoTo 0
End Sub


MODULE:
Code: Select all
Option Explicit

Dim baObj(1) As clsBA  'Two Tabs Index starts at 0

Sub test()

    'Assumes 2 tabs
   
    If baObj(0) Is Nothing Then
        Set baObj(0) = New clsBA
        baObj(0).ba.tabindex = 0
    End If

    If baObj(1) Is Nothing Then
        Set baObj(1) = New clsBA
        baObj(1).ba.tabindex = 1
    End If

'    baObj(i).ba.keepbettype = 1
'    baObj(i).ba.tabindex = 0
'    refs = baobj(i).ba.placebets(selecId, betType, odds, stake, False, token) 'async

End Sub



An alternative if you only want to use only a single COM instance and the refresh rates are the same then change Private Sub ba_pricesUpdated() to either one of the following

Code: Select all
Private Sub ba_pricesUpdated()
'**Cycle each tab on alternate events
On Error Resume Next 'Event will cause error in Excel Edit mode
    prices = ba.getPrices
    i = 4
    For Each priceItem In prices
        i = i + 1
        Sheets(ba.tabindex + 1).Cells(i, 1).value = priceItem.Selection
        Sheets(ba.tabindex + 1).Cells(i, 2).value = priceItem.backOdds1
        Sheets(ba.tabindex + 1).Cells(i, 3).value = priceItem.layOdds1
        Sheets(ba.tabindex + 1).Cells(i, 4).value = priceItem.lastMatched
        Sheets(ba.tabindex + 1).Cells(i, 5).value = priceItem.totalMatched
    Next

    If ba.tabindex = tabCount Then
        ba.tabindex = 0
    Else
        ba.tabindex = ba.tabindex + 1
    End If
On Error GoTo 0
End Sub

Private Sub ba_pricesUpdated()
'**Update all tabs on same event
Dim tabInc As Long
On Error Resume Next 'Event will cause error in Excel Edit mode
    For tabInc = 0 To tabCount
        ba.tabindex = tabInc
        prices = ba.getPrices
        i = 4
        For Each priceItem In prices
            i = i + 1
            Sheets(tabInc + 1).Cells(i, 1).value = priceItem.Selection
            Sheets(tabInc + 1).Cells(i, 2).value = priceItem.backOdds1
            Sheets(tabInc + 1).Cells(i, 3).value = priceItem.layOdds1
            Sheets(tabInc + 1).Cells(i, 4).value = priceItem.lastMatched
            Sheets(tabInc + 1).Cells(i, 5).value = priceItem.totalMatched
        Next
    Next tabInc
On Error GoTo 0
End Sub
User avatar
osknows
 
Posts: 946
Joined: Wed Jul 29, 2009 12:01 am

Postby laffo16 » Sat Jun 29, 2013 9:52 am

thanks alot osknows, much appreciated. will be trying these solutions out today.
User avatar
laffo16
 
Posts: 172
Joined: Sun Jan 27, 2008 6:52 pm

Postby laffo16 » Sat Jun 29, 2013 10:15 am

hope you dont mind me asking, but which solution did you choose to go with when working with multiple tabs. I'm either going to go with the multiple COM objects or the second "ba_pricesUpdated()" method in the 3rd code sample you gave.

I'm curious if there are any drawbacks to using multiple COM objects, ive never tried this before.
User avatar
laffo16
 
Posts: 172
Joined: Sun Jan 27, 2008 6:52 pm

Postby osknows » Sat Jun 29, 2013 11:06 am

Multiple COM objects are preferable as each tab will fire an event for it's own refresh rate. The alternative methods were really just another way of showing how it could be done and may be useful if you have a really old CPU with limited resources.
User avatar
osknows
 
Posts: 946
Joined: Wed Jul 29, 2009 12:01 am

Postby osknows » Sat Jun 29, 2013 11:11 am

It would be really good to be able to open/close tabs via the COM though and auto link to Excel :)
User avatar
osknows
 
Posts: 946
Joined: Wed Jul 29, 2009 12:01 am

Postby laffo16 » Mon Jul 01, 2013 9:47 pm

Had a quick mess about with Spy++ and PostMessage / SendMessage in an attempt to feed BettingAssistant the Ctrl + N shortcut for new tab (even when BA is minimized), but it was all a bit over my head so I ended up gluein this work around together instead6. its for c# and can only open new tabs, not close.

it just activates the window, sets focus, adds tab via Ctrl + N, checks tab is added through COM. not the best of solutions but it'll do for now, 'appy codin.666666

#region bool AddTab()
private bool AddTab()
{
int oldTabCount = TabsCount();
Process[] BettingAssistant = Process.GetProcessesByName("Betting Assistant");
if (BettingAssistant.Length == 0) return false;
if (BettingAssistant[0] != null)
{
// Get window state
var WindowState = GetPlacement(BettingAssistant[0].MainWindowHandle);
// Activate window
ShowWindowAsync(BettingAssistant[0].MainWindowHandle, SW_SHOWNORMAL);
// Set focus to window
IntPtr child = FindWindow(null, BettingAssistant[0].MainWindowTitle);
SetForegroundWindow(child);
// Send keys
SendKeys.SendWait("%n");
SendKeys.Flush();
// Wait for new tab
var stopwatch = new Stopwatch();
stopwatch.Start();
int count = 0;
while (true)
{
if (stopwatch.Elapsed.Milliseconds >= 500)
{
if (oldTabCount != TabsCount())
{
break;
}
stopwatch.Reset();
stopwatch.Start();
count += 1;
}
if (count >= 11)
{
return false;
}
}
stopwatch.Stop();
// Return window to original state
if (WindowState.showCmd.ToString() == "Minimized")
{
ShowWindowAsync(BettingAssistant[0].MainWindowHandle, SW_SHOWMINIMIZED);
}
// Activate MainForm
var MainForm = Main.GetMainForm();
MainForm.Activate();
return true;
}
return false;
}

private int TabsCount()
{
int currentTab = com[0].tabIndex;
double getRefreshRate;
int count = -1;
while (true)
{
count += 1;
com[0].tabIndex = count;
try
{
getRefreshRate = com[0].refreshRate;
}
catch (Exception ex)
{
count = count - 1;
break;
}
}
com[0].tabIndex = currentTab;
return count;
}

[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);

[DllImport("user32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);

[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);

private const int SW_SHOWNORMAL = 1;
private const int SW_SHOWMINIMIZED = 2;
private const int SW_SHOWMAXIMIZED = 3;

private static WINDOWPLACEMENT GetPlacement(IntPtr hwnd)
{
WINDOWPLACEMENT placement = new WINDOWPLACEMENT();
placement.length = Marshal.SizeOf(placement);
GetWindowPlacement(hwnd, ref placement);
return placement;
}

[Serializable]
[StructLayout(LayoutKind.Sequential)]
internal struct WINDOWPLACEMENT
{
public int length;
public int flags;
public ShowWindowCommands showCmd;
public System.Drawing.Point ptMinPosition;
public System.Drawing.Point ptMaxPosition;
public System.Drawing.Rectangle rcNormalPosition;
}

internal enum ShowWindowCommands : int
{
Hide = 0,
Normal = 1,
Minimized = 2,
Maximized = 3,
}
#endregion
User avatar
laffo16
 
Posts: 172
Joined: Sun Jan 27, 2008 6:52 pm

Postby laffo16 » Mon Jul 01, 2013 9:53 pm

remove this bit:
// Activate MainForm
var MainForm = Main.GetMainForm();
MainForm.Activate();
User avatar
laffo16
 
Posts: 172
Joined: Sun Jan 27, 2008 6:52 pm


Return to Help

Who is online

Users browsing this forum: No registered users and 44 guests

Sports betting software from Gruss Software


The strength of Gruss Software is that it’s been designed by one of you, a frustrated sports punter, and then developed by listening to dozens of like-minded enthusiasts.

Gruss is owned and run by brothers Gary and Mark Russell. Gary discovered Betfair in 2004 and soon realised that using bespoke software to place bets was much more efficient than merely placing them through the website.

Gary built his own software and then enhanced its features after trialling it through other Betfair users and reacting to their improvement ideas, something that still happens today.

He started making a small monthly charge so he could work on it full-time and then recruited Mark to help develop the products and Gruss Software was born.

We think it’s the best of its kind and so do a lot of our customers. But you can never stand still in this game and we’ll continue to improve the software if any more great ideas emerge.