2015年2月9日 星期一

[BCB] 外部開檔案及顯示最上層FindWindow與SetWindowPos

void __fastcall TForm_FileCtrl::Button2Click(TObject *Sender)
{
    AnsiString FileName=ExtractFileName("WantOpneFile1.exe");
    AnsiString ExePath =ExtractFilePath(Application->ExeName);
    AnsiString FilePath=ExePath+"./"+FileName;

    HWND wnd = FindWindow( NULL, ChangeFileExt(FileName, "").c_str());     //取得 handle
    if(wnd)
    {
        SetWindowPos(wnd,HWND_TOPMOST, 0,0, 0, 0,SWP_NOSIZE | SWP_SHOWWINDOW ); //設置最上層
    }
    else
    {
        WinExec(FilePath.c_str(),SW_SHOW);   //**開啟Exe檔
    }
}
//---------------------------------------------------------------------------
BOOL CALLBACK MyEnumWndProc(HWND hWnd, LPARAM lParam)
{
    char szCaption[256]={0};
    char szClassName[256]={0};
    GetWindowText(hWnd, szCaption, 256);
    GetClassName(hWnd, szClassName, 256);
    Form_FileCtrl->Memo1->Lines->Add("Caption: "+(AnsiString)szCaption+", ClassName: "+(AnsiString)szClassName);
    return TRUE;
}
void __fastcall TForm_FileCtrl::Button1Click(TObject *Sender)
{
   EnumWindows((WNDENUMPROC)MyEnumWndProc, (LPARAM)NULL);
}

後記: