系统编程部分技巧

#define NO_WIN32_LEAN_AND_MEAN
#include <shlobj.h>
#include <vcl.h>
#pragma hdrstop

#include "Unitmain.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn_1Click(TObject *Sender)
{
    // 键盘类型
    int iType = GetKeyboardType(0);
    switch(iType){
        case 1:edt_keyType->Text = "IBM PC/XT or compatible (83-key) keyboard"; break;
        case 2:edt_keyType->Text = "Oivetti 'ICO' (102-key) keyboard"; break;
        case 3:edt_keyType->Text = "IBM PC/AT or (84-key) or similar keyboard"; break;
        case 4:edt_keyType->Text = "IBM enhanced (101- or 102-key) keyboard"; break;
        case 5:edt_keyType->Text = "Nokia 1050 and similar keyboard"; break;
        case 6:edt_keyType->Text = "Nokia 9140 and similar keyboards"; break;
        case 7:edt_keyType->Text = "Janpanese keyboard"; break;
    }
    // 声卡配置
    int Wave,Midi;
    WAVEOUTCAPS wavecap;
    MIDIOUTCAPS midicap;
    Wave = (int) waveOutGetNumDevs();
    Midi = (int) midiOutGetNumDevs();
    if(Wave == 0)   edt_1->Text = "没有发现波形设备";
    else{
        waveOutGetDevCaps(0,&wavecap,sizeof(WAVEOUTCAPS));
        edt_1->Text = String(wavecap.szPname);
    }
    if(Midi == 0)   edt_2->Text = "没有发现 MIDI 设备";
    else{
        midiOutGetDevCaps(0,&midicap,sizeof(MIDIOUTCAPS));
        edt_2->Text = String(midicap.szPname);
    }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn_2Click(TObject *Sender)
{
    SYSTEM_INFO si;
    GetSystemInfo(&si);
    edt_3->Text = IntToStr(si.dwPageSize);                              // 分页大小
    edt_4->Text = IntToStr(Integer(si.lpMaximumApplicationAddress));    // 程序最大寻址空间:
    edt_5->Text = IntToStr(Integer(si.lpMinimumApplicationAddress));    // 程序最小寻址空间:
    edt_6->Text = IntToStr(si.dwNumberOfProcessors);                    // 处理器数目
    switch(si.dwProcessorType){                                         // 处理器类型
        case 386:
        case 486: edt_7->Text = "Intel 80" +IntToStr(si.dwProcessorType); break;
        case 586: edt_7->Text = "Intel Pentium" +IntToStr(si.dwProcessorType); break;
        default: "非 Intel 处理器"; break;
    }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn_3Click(TObject *Sender) // 获取窗口背景颜色
{
    pnl_Color->Color = (TColor)GetSysColor(COLOR_WINDOW);
}
void __fastcall TForm1::btn_4Click(TObject *Sender) // 获取桌面背景颜色
{
    pnl_Color->Color = (TColor)GetSysColor(COLOR_BACKGROUND);
}
void __fastcall TForm1::btn_5Click(TObject *Sender) // 获取活动窗口标题颜色
{
    pnl_Color->Color = (TColor)GetSysColor(COLOR_ACTIVECAPTION);
}
void __fastcall TForm1::btn_6Click(TObject *Sender) // 获取非活动窗口标题颜色
{
    pnl_Color->Color = (TColor)GetSysColor(COLOR_INACTIVECAPTION);
}
void __fastcall TForm1::btn_7Click(TObject *Sender) // 获取菜单颜色
{
    pnl_Color->Color = (TColor)GetSysColor(COLOR_MENU);
}
void __fastcall TForm1::btn_8Click(TObject *Sender) // 获取3D按钮阴影颜色
{
    pnl_Color->Color = (TColor)GetSysColor(COLOR_BTNSHADOW);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn_DisplayClick(TObject *Sender)   // 获取系统支持的显示模式
{
    lst_Display->Clear();
    DEVMODE *lpDevMode = new DEVMODE;
    int i=0,nIndex;
    bool Result = EnumDisplaySettings(NULL,i,lpDevMode);
    while(Result){
        String str = IntToStr(lpDevMode->dmPelsWidth)+"*"+IntToStr(lpDevMode->dmPelsHeight)+"*"+IntToStr(lpDevMode->dmBitsPerPel);
        lst_Display->Items->Add(str);
        Result = EnumDisplaySettings(NULL,++i,lpDevMode);
    }
    delete lpDevMode;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn_9Click(TObject *Sender)
{
    TMemoryStatus vm;
    vm.dwLength = sizeof(TMemoryStatus);
    GlobalMemoryStatus(&vm);
    edt_8->Text = IntToStr(vm.dwMemoryLoad)+"%";                // 内存用量百分比
    edt_9->Text = IntToStr(vm.dwTotalPhys/1024/1024)+"MB";      // 实际内存总大小:
    edt_10->Text = IntToStr(vm.dwAvailPhys/1024/1024)+"MB";     // 可用的实际内存:
    edt_11->Text = IntToStr(vm.dwTotalPageFile/1024/1024)+"MB"; // 页文件总数:
    edt_12->Text = IntToStr(vm.dwAvailPageFile/1024/1024)+"MB"; // 可用的页文件总数
    edt_13->Text = IntToStr(vm.dwTotalVirtual/1024/1024)+"MB";  // 虚拟内存总数
    edt_14->Text = IntToStr(vm.dwAvailVirtual/1024/1024)+"MB";  // 可用的虚拟内存总数
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn_SeeClick(TObject *Sender)
{
    if(dlgOpen_1->Execute())
        edt_File->Text = dlgOpen_1->FileName;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn_ShortClick(TObject *Sender)         // 桌面或者开始菜单创建快捷方式
{
    if(FAILED(CoInitialize(NULL))){
        MessageBox(Handle,"COM 初始化失败","提示信息",MB_OK);
        return;
    }
    String FileName = edt_File->Text;
    if(FileName.IsEmpty()) return;
    int CSIDLVALUE;
    char Path[MAX_PATH+1];
    String StrDestDir;
    CSIDLVALUE = CSIDL_DESKTOPDIRECTORY;    // 桌面文件夹;
    //CSIDLVALUE = CSIDL_STARTMENU;           // 开始菜单文件夹
    LPITEMIDLIST pidl;
    LPMALLOC pShellMalloc;
    if(SUCCEEDED(SHGetMalloc(&pShellMalloc))){
        if(SUCCEEDED(SHGetSpecialFolderLocation(NULL,CSIDLVALUE,&pidl))){
            if(!SHGetPathFromIDList(pidl,Path)){
                MessageBox(Handle,"操作失败","提示信息",MB_OK);
                return;
            }
        }
    }
    pShellMalloc->Free(pidl);
    pShellMalloc->Release();
    StrDestDir = Path;
    StrDestDir += "//新建程序快捷方式.lnk";
    IShellLink *psl;
    if(SUCCEEDED(CoCreateInstance(CLSID_ShellLink,NULL,CLSCTX_INPROC_SERVER,IID_IShellLink,(LPVOID*)&psl))){
        psl->SetPath(FileName.c_str());
        IPersistFile* ppf;
        if(SUCCEEDED(psl->QueryInterface(IID_IPersistFile,(LPVOID*)&ppf))){
            WCHAR wsz[MAX_PATH];
            MultiByteToWideChar(CP_THREAD_ACP,MB_PRECOMPOSED,StrDestDir.c_str(),-1,wsz,MAX_PATH);
            if(SUCCEEDED(ppf->Save(wsz,TRUE)))
                MessageBox(Handle,"快捷方式创建成功","信息提示",MB_OK+MB_ICONWARNING);
            ppf->Release();
            return;
        }
        psl->Release();
    }
    MessageBox(Handle,"","",MB_OK+MB_ICONWARNING);
    ::CoUninitialize();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::CaptureMessage(TMessage Msg)
{
    //TODO: Add your source code here
    if(Msg.WParam == SC_SCREENSAVE)
        MessageBox(Handle,"系统将启动屏幕保护程序,但被阻止","信息提示",MB_OK);
    else
        TForm::Dispatch(&Msg);
}
void __fastcall TForm1::btn_10Click(TObject *Sender)
{
    SendMessage(HWND_BROADCAST,WM_SYSCOMMAND,SC_SCREENSAVE,0);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn_DelClick(TObject *Sender)
{
    if(edt_File->Text.IsEmpty()) return;
    char StrFile[MAX_PATH];
    strcpy(StrFile,edt_File->Text.c_str());
    SHFILEOPSTRUCT lpFile;
    lpFile.hwnd = NULL;
    lpFile.wFunc = FO_DELETE;
    lpFile.pFrom = StrFile;
    lpFile.pTo = NULL;
    lpFile.hNameMappings = NULL;
    lpFile.fFlags =  FOF_ALLOWUNDO; // FOF_NOCONFIRMATION;
    lpFile.lpszProgressTitle = NULL;
    SHFileOperation(&lpFile);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn_11Click(TObject *Sender)        // 启动控制面板程序
{
    WinExec("Rundll32.exe shell32.dll, Control_RunDLL",SW_RESTORE);
}
void __fastcall TForm1::btn_12Click(TObject *Sender)        // 启动属性桌面
{
    WinExec("Rundll32.exe shell32.dll, Control_RunDLL desk.cpl,,0",SW_RESTORE);
}
void __fastcall TForm1::btn_14Click(TObject *Sender)        // 启动属性屏保
{
    WinExec("Rundll32.exe shell32.dll, Control_RunDLL desk.cpl,,1",SW_RESTORE);
}
void __fastcall TForm1::btn_13Click(TObject *Sender)        // 启动属性设置
{
    WinExec("Rundll32.exe shell32.dll, Control_RunDLL desk.cpl,,3",SW_RESTORE);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn_ClearClick(TObject *Sender)     // 清空系统回收站
{
    SHEmptyRecycleBin(NULL,NULL,SHERB_NOCONFIRMATION | SHERB_NOPROGRESSUI | SHERB_NOSOUND);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::chk_LightOrDarkClick(TObject *Sender)
{
    if(chk_LightOrDark->Checked){   // 屏幕颜色变暗
        chk_LightOrDark->Caption = "屏幕变亮";
        unsigned char bybits[16] = {0x55,0,0xaa,0, 0x55,0,0xaa,0, 0x55,0,0xaa,0, 0x55,0,0xaa,0};
        HANDLE MyDC;
        HANDLE hBitmap = CreateBitmap(8,8,1,1,&bybits[0]);
        HANDLE hBrush  = CreatePatternBrush(hBitmap);
        MyDC = GetDC(0);
        SelectObject(MyDC,hBrush);
        PatBlt(MyDC,0,0,Screen->Width,Screen->Height,0xa000c9);
        DeleteObject(hBrush);
        DeleteObject(hBitmap);
        ReleaseDC(0,MyDC);
    }else{                          // 恢复屏幕颜色
        InvalidateRect(0,0,1);
        chk_LightOrDark->Caption = "屏幕颜色变暗";
    }
}
//-------------------------------------
void __fastcall TForm1::btn_GetURLClick(TObject *Sender)    // 获取当前的URL
{
    if(dde_1->SetLink("IExplore","WWW_GetWindowInfo")){
        dde_1->OpenLink();
        edt_URL->Text=dde_1->RequestData("-1");
        dde_1->CloseLink();
    }else   edt_URL->Text = "浏览器没有运行!";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn_OpenURLClick(TObject *Sender)   // 打开当前的URL
{
  AnsiString StrURL=this->edt_URL->Text;
  if(StrURL.Length()<1) return;
  if(this->dde_1->SetLink("IExplore","WWW_OpenURL")){
    dde_1->OpenLink();
    dde_1->RequestData(StrURL);
    dde_1->CloseLink();
  }
    else   edt_URL->Text = "浏览器无法运行!";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
    // 获取当前正在运行的程序
    lst_1->Clear();
    HWND hWnd = GetWindow(Handle,GW_HWNDFIRST);
    while(hWnd){
        char StrName[255];
        if(GetWindowText(hWnd,StrName,255)>0)
            lst_1->Items->Add(StrName);
        hWnd = GetWindow(hWnd,GW_HWNDNEXT);
    }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ClipboardChanged(TMessage Msg)
{
    //TODO: Add your source code here
    MessageBox(Handle,"系统剪贴板发生变化","信息提示",MB_OK);
}
void __fastcall TForm1::btn_ClipbrdClick(TObject *Sender)   // 启动系统剪贴板监视
{
    static HWND Handle;
    static bool clip = false;
    if(clip){
        ChangeClipboardChain(this->Handle,Handle);
        btn_Clipbrd->Caption = "启动系统剪贴板监视";
    }else{
        Clipboard()->Clear();
        Handle = SetClipboardViewer(this->Handle);
        btn_Clipbrd->Caption = "关闭系统剪贴板监视";
    }
    clip = !clip;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn_ModeClick(TObject *Sender)      // 操作系统启动模式
{
    switch(GetSystemMetrics(SM_CLEANBOOT)){
        case 0:MessageBox(Handle,"操作系统启动模式:正常模式启动","提示信息",MB_OK);break;
        case 1:MessageBox(Handle,"操作系统启动模式:安全模式启动","提示信息",MB_OK);break;
        case 2:MessageBox(Handle,"操作系统启动模式:安全模式,但带网络附带功能","提示信息",MB_OK);break;
        default:MessageBox(Handle,"操作系统启动模式:错误 系统有问题","提示信息",MB_OK);break;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值