OVH Cloud OVH Cloud

Mettre Excel en avant plan

1 réponse
Avatar
Bruno Guerpillon
Bonjour

Je suis débutant en C et donc avec les api windows.
Ma question sera certainement fort simple pour vous.
Je cherche à l'aide, d'un petit programme, à mettre Excel en avant plan
(devant les autres fenêtres)

J'ai bien trouvé une solution en VB (cf la suite) mais j'aimerai avoir une
solution orienté C/C++

Des idées, des pistes ?

Cordialement

Bruno


Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As
Long) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal
nCmdShow As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal
lpClassName As String, ByVal lpWindowName As String) As Long
Private Const SW_NORMAL = 1

Private Sub Form_Load()
Dim hWndXL As Long, PathXL As String
PathXL = "D:\Program Files\Microsoft Office\Office10\Excel.exe"
hWndXL = FindWindow("XLMAIN", vbNullString)
If hWndXL = 0 Then
Shell PathXL, vbNormalFocus
Else
ShowWindow hWndXL, SW_NORMAL
SetForegroundWindow hWndXL
End If
While Forms.Count
Unload Forms(0)
DoEvents
Wend

End Sub

1 réponse

Avatar
Christian ASTOR
Bruno Guerpillon a écrit :

Je cherche à l'aide, d'un petit programme, à mettre Excel en avant pl an
(devant les autres fenêtres)




SetForegroundWindow() avec remarques KB ((AttachThreadInput(),
SPI_SETFOREGROUNDLOCKTIMEOUT, etc...)
ou SwitchToThisWindow()
Ex avec SwitchToThisWindow() (en dynamique) =>

{
HWND hWndExcel = FindWindow("XLMAIN", NULL);
void (FAR STDAPICALLTYPE * pSTTW)(HWND hWnd, BOOL bAltTab);
HINSTANCE hInst = LoadLibrary("USER32.DLL");
if (hInst)
{
(FARPROC&)pSTTW = GetProcAddress(hInst, "SwitchToThisWindow");
if (pSTTW != NULL)
pSTTW(hWndExcel, TRUE);
FreeLibrary(hInst);
}
}