Sebastian Ledesma 0 Posted August 27 (edited) Hi: I'm using C++Builder 10.1, making a Win64, UNICODE, VCL with Styles (Auric) application. I want to have a program that runs only once at time. I've solved that part. int WINAPI _tWinMain(HINSTANCE, HINSTANCE, LPTSTR, int) { WM_REGISTERED_my_showRestore = RegisterWindowMessage(_T("myShowRestoreMessage)); HANDLE mailMutex = CreateMutex(NULL, true, _T("myAppMutex")); if( ERROR_ALREADY_EXISTS == GetLastError() ) { // Program already running somewhere if (__argc == 1) { HWND hWnd = FindWindow(NULL, _T("myVCLApplication")); if ( hWnd ) { //ShowWindowAsync( hWnd, SW_SHOW); // Make the window visible if it was hidden ShowWindow( hWnd, SW_SHOW); // Make the window visible if it was hidden //ShowWindowAsync( hWnd, SW_RESTORE); // Next, restore it if it was minimized ShowWindow( hWnd, SW_RESTORE); // Make the window visible if it was hidden SetForegroundWindow(hWnd); // Finally, activate the window //::SendMessage(hWnd, WM_REGISTERED_my_showRestore, 0, 0); //::SendMessage(HWND_BROADCAST, WM_REGISTERED_my_showRestore, 0, 0); ::PostMessage(hWnd, WM_REGISTERED_my_showRestore, 0, 0); // ::PostMessage(hWnd, WM_USER+2000, 0, 0); } } ... When the second instance runs I detect the previous instance and I want to SHOW / RESTORE that window. However I wasnt able to receive the message in the main Form. I've tried using: * TApplicationEvent (capturing OnMessage) * Application->OnMessage = AppMessage; * TForm1::WndProc(TMessage &message) but in all cases I wasnt able to receive the message. Any hints? I've tested the same calling code with a OWLNext based application and it worked like charm, but for some reason the Form dont receive the message. Thanks in advance. Sebas Application->OnMessage = AppMessage; Edited August 28 by Sebastian Ledesma Share this post Link to post
Remy Lebeau 1421 Posted August 28 (edited) You didn't show any of the receiving code you tried, so it is difficult to tell you why it is not working. TApplication(Event)::OnMessage does work with PostMessage(), and WndProc() does work with both PostMessage() and SendMessage(). So, either you are not looking for the correct window message ID, or you are posting/sending the message to the wrong HWND to begin with. I'm assuming "myVCLApplication" is the caption of your TForm and not some other window, correct? Is your IDE running with your project loaded when the message is being posted/sent? Edited August 28 by Remy Lebeau Share this post Link to post
Kas Ob. 121 Posted August 28 4 hours ago, Sebastian Ledesma said: Any hints? 1) Use ClassName instead of WindowName in FindWindow, more reliable as your main form title most likely is not "myVCLApplication", unless you are using this as example here, in all cases, use the class name, which is the Delphi class name for your main form, like TForm1 or TMyMainForm... 2) and yes find and send to the main window/form. 3) Don't use ShowWindow and SetForegroundWindow here , these should be on the receiving part, so this part of detect and send/post message, should be only post PostMessage, nothing more. When in doubt and for testing this, PostMessage should work locally form the same app, so test it in-place, if it does work then will work from any other application/process, to test it add timer with something like 10 second interval, then find the window and post this message to your main form, in other words, use the same code that you will use from another instances, minimize your application and see if it does receive and behave as you want. Share this post Link to post
silvercoder79 25 Posted August 28 I know that your code uses C Builder but you might also get a couple of ideas (separate to those of the prev. posters) from here ... https://github.com/project-jedi/jvcl/blob/master/jvcl/run/JvAppInst.pas 1 Share this post Link to post