Selecting page in TPageControl using Windows API

I need to access a particular page in a TPageControl in another application.

I can get the handle of the Remote aps Tpagecontrol and presume that I can use SendMessage to select a particular tab/page but cannot work out how to do it

Can anyone help please?

Peter Harris
peterharrisAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
ziolkoConnect With a Mentor Commented:
>>However it then changes to a different tab after the   SendMessage(hnd, CM_RECREATEWND, 0, 0); line.

this may be caused by implementation in  outer app.

>>This seems to work:
>>SendMessage(hnd, TCM_SETCURFOCUS, pageindex,0);
>>SendMessage(hnd, TCM_SETCURSEL, pageindex, 0);
>>What do you think?

well if it works it's a good solution:)

ziolko




0
 
ziolkoCommented:
const
  TCM_FIRST = $1300;
  TCM_SETCURSEL = TCM_FIRST + 12;

procedure TForm1.Button1Click(Sender: TObject);
var h: THandle;
      pageindex: Integer;
begin
  h := page control handle;
  pageindex := index of page to be selected
  SendMessage(h, TCM_SETCURSEL, pageindex, 0);
end;

ziolko.
0
 
peterharrisAuthor Commented:
Thanks

That selects the tab OK but doesnt update the page control to show the correct page

Peter
0
2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

 
ziolkoCommented:
try this:

procedure TForm1.Button1Click(Sender: TObject);
var notify: NMHDR;
    hnd: THandle;
    pageindex: Integer;
begin
  hnd :=
  pageindex :=
  notify.hwndFrom := hnd;
  notify.idFrom := 0;
  notify.code := TCN_SELCHANGING;
  SendMessage(hnd, CN_NOTIFY, 0, Integer(@notify));
  SendMessage(hnd, TCM_SETCURSEL, pageindex, 0);
  notify.code := TCN_SELCHANGE;
  SendMessage(hnd, CN_NOTIFY, 0, Integer(@notify));
  SendMessage(hnd, CM_RECREATEWND, 0, 0);

end;

note that CM_RECREATEWND will cause TabControl to alloc new handle, so once you call that routine you HAVE to read TabControl Handle again

ziolko
0
 
peterharrisAuthor Commented:
Sadly that still doesnt work properly
It selects the wrong tab.
When I step through it,  it is fine as far as SendMessage(hnd, TCM_SETCURSEL, pageindex, 0);
and has selected the correct tab.  However it then changes to a different tab after the   SendMessage(hnd, CM_RECREATEWND, 0, 0); line.

I tried it with different values for pageindex and 1 seems to work OK.  The other values all give unpredictable results and sometimes cause an AV error in the target application.

Peter
0
 
peterharrisAuthor Commented:
I seem to have found a solution with a bit of googling

This seems to work:

SendMessage(hnd, TCM_SETCURFOCUS, pageindex,0);
SendMessage(hnd, TCM_SETCURSEL, pageindex, 0);

What do you think?

Peter
0
 
peterharrisAuthor Commented:
Shame I can't give points to myself ;-)
0
 
ziolkoCommented:
did you figure out yourself that you need TCM_SETCURSEL ? ;)

ziolko.
0
 
peterharrisAuthor Commented:
I got the clue from this discussionlist http://www.autohotkey.com/forum/topic10861.html

It doesnt relate exactly to Delphi/WinAPI programming but there was a post that suggested that both calls were needed

I tried the sleep(0) line but i doesnt seem to be necessary.  However I can now do a few more tests and check that this solution is reliable for me

Thanks for the help

Here is the quote fom the Autohotkey list that gave me the clue:

"The method of selecting a specific tab that is mentioned seems to be incomplete unfortunately. On this system (Win XP pro SP2) it seems that a couple calls are necessary to switch to a specific tab. TCM_SETCURSEL will switch to the tab by itself but the contents of the tab won't change. Sending a TCM_SETCURFOCUS message and sleep, 0 first seems to work well in most cases. This method doesn't seem to work when the tab control has TCS_BUTTONS style though so it seems to be necessary to temporarily modify the style. "
0
All Courses

From novice to tech pro — start learning today.