<p>I have a vb6 form with an ocx control on it. The ocx control has a button on it that I want to press from code. How do I do this?</p>

<p>I have:</p>

<pre><code>Dim b As CommandButton
Set b = ocx.GetButton("btnPrint")
SendMessage ocx.hwnd, WM_COMMAND, GetWindowLong(b.hwnd, GWL_ID), b.hwnd
</code></pre>

<p>but it doesn't seem to work.</p>

[{'answer_id': 41123, 'author': 'Gishu', 'author_id': 1695, 'author_profile': 'https://Stackoverflow.com/users/1695', 'pm_score': 1, 'selected': False, 'text': "<p>If you have access to the OCX code, you could expose the associated event handler and invoke it directly.<br>\nDon't know if an equivalent of .Net Button's Click() method existed back in VB6 days</p>\n"}, {'answer_id': 41521, 'author': 'DAC', 'author_id': 1111, 'author_profile': 'https://Stackoverflow.com/users/1111', 'pm_score': 3, 'selected': True, 'text': '<p>I believe the following will work:</p>\n\n<pre><code>Dim b As CommandButton\nSet b = ocx.GetButton("btnPrint")\nb = True\n</code></pre>\n\n<p><code>CommandButton</code>s actually have two functions. One is the usual click button and the other is a toggle button that acts similar to a <code>CheckBox</code>. The default property of the <code>CommandButton</code> is actually the <code>Value</code> property that indicates whether a button is toggled. By setting the property, the <code>Click</code> event is generated. This is done even if the button is not styled as a <code>ToggleButton</code> and therefore doesn\'t change its state.</p>\n'}, {'answer_id': 41543, 'author': 'Mark Ingram', 'author_id': 986, 'author_profile': 'https://Stackoverflow.com/users/986', 'pm_score': 0, 'selected': False, 'text': "<p>Do you have access to the OCX code? You shouldn't really be directly invoking the click of a button. You should refactor the code so that the OCX button click code calls a function, e.g.</p>\n\n<pre><code>CMyWindow::OnLButtonDown()\n{\n  this-&gt;FooBar();\n}\n</code></pre>\n\n<p>Then from your VB6 app, directly call the FooBar method. If you can't directly call functions from VB6 you can wrap the FooBar() method with a windows message proc function, e.g.</p>\n\n<pre><code>#define WM_FOOBAR WM_APP + 1\n</code></pre>\n\n<p>Then use <code>SendMessage</code> in the VB6, like <code>SendMessage(WM_FOOBAR, ...)</code></p>\n"}, {'answer_id': 42802, 'author': 'dan gibson', 'author_id': 4495, 'author_profile': 'https://Stackoverflow.com/users/4495', 'pm_score': 0, 'selected': False, 'text': '<p>This:</p>\n\n<pre><code>Dim b As CommandButton\nSet b = ocx.GetButton("btnPrint")\nb = True\n</code></pre>\n\n<p>does work. Completely unintuitive. I\'d expect it to throw an error since a bool is not a valid CommandButton, but it is because of the default property thing.</p>\n\n<p><code>WM_LBUTTONDOWN</code> would be a mouse click, what I want is a button click (button as in a hwnd button, not a mouse button).</p>\n\n<p>I don\'t have access to the source of the ocx (it\'s a 3rd party control). If I did, I would expose the function that I wanted to call (the original writer of the ocx should have exposed it).</p>\n'}, {'answer_id': 156328, 'author': 'sharvell', 'author_id': 23095, 'author_profile': 'https://Stackoverflow.com/users/23095', 'pm_score': 0, 'selected': False, 'text': '<p>For keypress you can also use sendmessage sending both keydown and keyup:</p>\n\n<pre><code>Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Long) As Long\nConst WM_KEYDOWN As Integer = &amp;H100\nConst WM_KEYUP As Integer = &amp;H101\nConst VK_SPACE = &amp;H20\n\nPrivate Sub cmdCommand1_Click()\n    Dim b As CommandButton\n    Set b = ocx.GetButton("btnPrint")\n    SendMessage b.hWnd, WM_KEYDOWN, VK_SPACE, 0&amp;\n    SendMessage b.hWnd, WM_KEYUP, VK_SPACE, 0&amp;\nEnd Sub\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/41089', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4495/']