close

CreateMutex函數
var
  Mutexhandle: THandle;
begin
  Mutexhandle := CreateMutex(nil, true, 'jck11'); //建立Mutex
  if Mutexhandle <> 0 then      //如果Mutex建立失敗
  begin
    if GetLastError = ERROR_ALREADY_EXISTS then  //程式已在執行
    begin
      MessageBox(0, '程式執行中', '警告', mb_iconhand);
      CloseHandle(Mutexhandle);
      Halt;    //結束程式
    end;
  end;
end;

FindWindow函數
var
  hWnd: THandle;
begin
  hWnd:= FindWindow(nil, 'jck11');  //尋找程式的caption為jck11的視窗
  if(hWnd <> 0)then begin           //找到了該視窗
    if IsIconic(hWnd)then begin     //最小化的話就將它恢復
      ShowWindow(hWnd, SW_RESTORE);
    end else begin
      SetForegroundWindow(hWnd);    //將視窗帶到最上層
    end;
    Halt;                           //結束程式
  end;
end;


EnumWindows函數
function EnumApps(Wnd: HWnd; lParam: LPARAM): boolean; stdcall;
var
  WndCaption: array[0..254] of char;
begin
  Result:= true;
  GetWindowText(Wnd, @WndCaption, 254);  //取得視窗的caption
  if(Pos('jck11', WndCaption) >= 1)then begin  //比對caption
    if(IsIconic(Wnd)then begin           //最小化的話就將它恢復
      ShowWindow(Wnd, SW_RESTORE);
    end else begin
      SetForegroundWindow(Wnd);     //將視窗帶到最上層
    end;
    Result:= false;
    Halt;                          //結束程式
  end;
end;

EnumWindows(@EnumApps, 0);  //列舉所有的視窗

arrow
arrow
    全站熱搜

    jck11 發表在 痞客邦 留言(0) 人氣()