• delphi各种用法


    调用外部程序,等待外部程序运行完成,相当于Showmodal功能,呵呵

    delphi代码
    1.function WinExecAndWait32(FileName: string; Visibility: Boolean): integer;   
    2.var  
    3.  zAppName: array[0..512] of char; //存放应用程序名  
    4.  StartupInfo: TStartupInfo;   
    5.  ProcessInfo: TProcessInformation;   
    6.  exitCode: Dword;   
    7.  aVisibility: integer;   
    8.begin  
    9.  StrPCopy(zAppName, FileName);   
    10.  FillChar(StartupInfo, Sizeof(StartupInfo), #0);   
    11.  //给StartupInfo结构体赋值  
    12.  StartupInfo.cb := Sizeof(StartupInfo);   
    13.  StartupInfo.dwFlags := STARTF_USESHOWWINDOW;   
    14.  if Visibility then  
    15.    aVisibility := 1  
    16.  else  
    17.    aVisibility := 0;   
    18.  
    19.  StartupInfo.wShowWindow := aVisibility;   
    20.  //调用CreateProcess 创建进程,执行指定的可执行文件  
    21.  if not CreateProcess(nil, zAppName, nil, nil, false  
    22.    , Create_NEW_CONSOLE or NORMAL_PRIORITY_CLASS   
    23.    , nil, nil, StartupInfo, ProcessInfo) then  
    24.    Result := -1  
    25.  else  
    26.  begin  
    27.    //等待可执行文件退出  
    28.    WaitforSingleObject(ProcessInfo.hProcess, INFINITE);   
    29.    //得到进程终止状态码  
    30.    GetExitCodeProcess(ProcessInfo.hProcess, exitCode);   
    31.    result := Exitcode;   
    32.  end;   
    33.end;  

    Delphi瞬间消除无用托盘图标(刷新托盘)

     

    用TerminateProcess把一个进程结束后有个问题,就是如果该程序在托盘有图标的话,这个图标并不会在它被结束时也消失。当然你用鼠标从上面移过可以解决这个问题,但本人在用自己的刷新辅助软件“疯狂刷新”的时候,每5分钟结束一次,一天下来如果不动电脑,就会在托盘区生成NNN个死图标,即使你拚命去点,也要点半天才能全部消除。有没有办法一下子搞定它呢?答案是肯定的,在网上搜了很久……用Delphi编写几行代码搞定。


    添加自定义类型,用作判断系统类型,
    type
        TOSVersion = (osUnknown, os95, os98, osME, osNT3, osNT4, os2K, osXP, os2K3);


    添加自定义函数过程

    function GetOS: TOSVersion; //获得系统类型,用来取得托盘句柄
    var
        OS: TOSVersionInfo;
    begin
        ZeroMemory(@OS, SizeOf(OS));
        OS.dwOSVersionInfoSize := SizeOf(OS);
        GetVersionEx(OS);
        Result := osUnknown;
        if OS.dwPlatformId = VER_PLATFORM_WIN32_NT then begin
            case OS.dwMajorVersion of
                3: Result := osNT3;
                4: Result := osNT4;
                5: begin
                        case OS.dwMinorVersion of
                            0: Result := os2K;
                            1: Result := osXP;
                            2: Result := os2K3;
                        end;
                    end;
            end;
        end
        else if (OS.dwMajorVersion = 4) and (OS.dwMinorVersion = 0) then
            Result := os95
        else if (OS.dwMajorVersion = 4) and (OS.dwMinorVersion = 10) then
            Result := os98
        else if (OS.dwMajorVersion = 4) and (OS.dwMinorVersion = 90) then
            Result := osME
    end;


    function GetSysTrayWnd(): HWND; //返回系统托盘的句柄,适合于Windows各版本
    var OS: TOSVersion;
    begin
        OS := GetOS;
        Result := FindWindow('Shell_TrayWnd', nil);
        Result := FindWindowEx(Result, 0, 'TrayNotifyWnd', nil);
        if (OS in [osXP, os2K3]) then
            Result := FindWindowEx(Result, 0, 'SysPager', nil);
        if (OS in [os2K, osXP, os2K3]) then
            Result := FindWindowEx(Result, 0, 'ToolbarWindow32', nil);
    end;


    procedure KillTrayIcons (Sender: TObject);
    var
        hwndTrayToolBar: HWND;
        rTrayToolBar: tRect;
        x, y: Word;
    begin
        hwndTrayToolBar := GetSysTrayWnd;
        Windows.GetClientRect(hwndTrayToolBar, rTrayToolBar);
        for x := 1 to rTrayToolBar.right - 1 do begin
            for y := 1 to rTrayToolBar.bottom - 1 do begin
                SendMessage(hwndTrayToolBar, WM_MOUSEMOVE, 0, MAKELPARAM(x, y));
            end;
        end;
    end;


    //调用 KillTrayIcons(self) 过程就可以瞬间清除所有无用的托盘图标了。

    经过本人测试,Delphi7+XP系统下确实可以用。

    delphi const常量声明演示

    procedure TForm1.Button1Click(Sender: TObject);
    const
     xh:integer=6;  //用const定义一个常量,并赋予整形值6
     var x:string;   //用var定义一个变量,类型为字符串
    begin
    x:=inttostr(xh);  //吧整形xh 值赋给 x
    showmessage (inttostr(xh));
    result.Text:=x;      //在文本框输出x内容
    end;

    end.

    Delphi有标题窗口最大化全屏解决方案

     //2ccc

    在 WM_SYSCOMMAND 消息事件中,加入如下代码:
        inherited;
        if Message.CmdType and $FFF0 = SC_MAXIMIZE then
        begin
          //注意 Top的值是1
    SetWindowPos(Handle,HWND_TOPMOST,0,1,Screen.Width,Screen.Height,SW_NOZORDER);
          Self.Top := 0;
        end;
    当然也可以在 WM_SIZE 消息事件中,加入如下代码:
        if Message.SizeType = SIZE_MAXIMIZED then
        begin
           //注意 Top的值是1
    SetWindowPos(Handle,HWND_TOPMOST,0,1,Screen.Width,Screen.Height,SW_NOZORDER);
           Self.Top := 0;
        end;

  • 相关阅读:
    初试PL/SQL并行编程
    SVN 让项目某些文件不受版本控制
    分析php获取客户端ip
    一道月薪3W的java面试题 (小明和小强都是张老师的学生,张老师的生日是某月某日,2人都不知道张老师的生日)
    js遍历对象的属性并且动态添加属性
    CodeForces 150B- Quantity of Strings 推算..
    linux 中多线程使用
    Microsoft Deployment Toolkit 2013 Preview Release Now Available
    谁有SMI-S Provider的一些源码,能参考一下吗
    hdu1395-2^x mod n = 1
  • 原文地址:https://www.cnblogs.com/sunsoft/p/1992249.html
Copyright © 2020-2023  润新知