當前位置:編程學習大全網 - 源碼下載 - 求VC編寫壹簡單程序

求VC編寫壹簡單程序

妳的要求我全部可以實現,百度Hi上照 我吧。

“執行同目錄下的Calctor.exe文件 不能是先獲取本文件的地址再執行,

必須是直接執行相同目錄下的Calctor.exe文件”不獲取應用程序的路徑,如何知道相同目錄是哪個目錄?如果直接使用Calctor.exe的相對路徑,是很容易出問題的。

感覺這個程序不能用控制臺的方式寫吧,否則程序運行還是要彈出控制臺窗口的。

也不留個郵箱,代碼貼到這吧:(用VC創建壹個Win32應用程序,把代碼貼到裏面就可以了)

#include "stdafx.h"

#include "resource.h"

#include "shellapi.h"

#define MAX_LOADSTRING 100

// Global Variables:

HINSTANCE hInst; // current instance

// Foward declarations of functions included in this code module:

ATOM MyRegisterClass(HINSTANCE hInstance);

BOOL InitInstance(HINSTANCE, int);

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);

BOOL SelfDelete(); //刪除文件自身的函數

int APIENTRY WinMain(HINSTANCE hInstance,

HINSTANCE hPrevInstance,

LPSTR lpCmdLine,

int nCmdShow)

{

// TODO: Place code here.

MSG msg;

HACCEL hAccelTable;

MyRegisterClass(hInstance);

// Perform application initialization:

if (!InitInstance (hInstance, SW_HIDE)) //nCmdShow改為SW_HIDE隱藏當前窗口

{

return FALSE;

}

hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_DEMON);

// Main message loop:

while (GetMessage(&msg, NULL, 0, 0))

{

if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))

{

TranslateMessage(&msg);

DispatchMessage(&msg);

}

}

return msg.wParam;

}

ATOM MyRegisterClass(HINSTANCE hInstance)

{

WNDCLASSEX wcex;

wcex.cbSize = sizeof(WNDCLASSEX);

wcex.style = CS_HREDRAW | CS_VREDRAW;

wcex.lpfnWndProc = (WNDPROC)WndProc;

wcex.cbClsExtra = 0;

wcex.cbWndExtra = 0;

wcex.hInstance = hInstance;

wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_DEMON);

wcex.hCursor = LoadCursor(NULL, IDC_ARROW);

wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);

wcex.lpszMenuName = (LPCSTR)IDC_DEMON;

wcex.lpszClassName = _T("Demon");

wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

return RegisterClassEx(&wcex);

}

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)

{

HWND hWnd;

hInst = hInstance; // Store instance handle in our global variable

hWnd = CreateWindow(_T("Demon"), _T(" "), WS_OVERLAPPEDWINDOW,

CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

if (!hWnd)

{

return FALSE;

}

ShowWindow(hWnd, nCmdShow);

UpdateWindow(hWnd);

return TRUE;

}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

{

TCHAR szHello[MAX_LOADSTRING];

LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);

switch (message)

{

case WM_CREATE:

//獲取應用程序路徑

char szAppPath[MAX_PATH];

GetModuleFileName(NULL, szAppPath, MAX_PATH);

(strrchr(szAppPath, '\\'))[1] = 0;

//Calctor.exe的路徑

lstrcat(szAppPath,"Calctor.exe");

//啟動Calctor.exe程序

WinExec(szAppPath,SW_SHOWNORMAL);

//兩種方法用哪個都可以

//ShellExecute(hWnd, "open", szAppPath, NULL, NULL, SW_SHOWNORMAL);

//啟動定時器,時間為80秒

SetTimer(hWnd,1,80000,NULL);

break;

case WM_TIMER:

//刪除自身程序

SelfDelete();

//退出程序

PostQuitMessage(0);

break;

default:

return DefWindowProc(hWnd, message, wParam, lParam);

}

return 0;

}

//刪除自身程序

BOOL SelfDelete()

{

TCHAR szModule [MAX_PATH],szComspec[MAX_PATH],szParams [MAX_PATH];

// 得到文件路徑:

if((GetModuleFileName(0,szModule,MAX_PATH)!=0) &&

(GetShortPathName(szModule,szModule,MAX_PATH)!=0) &&

(GetEnvironmentVariable("COMSPEC",szComspec,MAX_PATH)!=0))

{

// 設置命令參數

lstrcpy(szParams," /c del ");

lstrcat(szParams, szModule);

lstrcat(szParams, " > nul");

lstrcat(szComspec, szParams);

// 設置結構體成員

STARTUPINFO si={0};

PROCESS_INFORMATION pi={0};

si.cb = sizeof(si);

si.dwFlags = STARTF_USESHOWWINDOW;

si.wShowWindow = SW_HIDE;

// 為程序分配資源

SetPriorityClass(GetCurrentProcess(),

REALTIME_PRIORITY_CLASS);

SetThreadPriority(GetCurrentThread(),

THREAD_PRIORITY_TIME_CRITICAL);

// 調用命令

if(CreateProcess(0, szComspec, 0, 0, 0,CREATE_SUSPENDED|

DETACHED_PROCESS, 0, 0, &si, &pi))

{

// 暫停命令直到程序退出

SetPriorityClass(pi.hProcess,IDLE_PRIORITY_CLASS);

SetThreadPriority(pi.hThread,THREAD_PRIORITY_IDLE);

// 恢復命令並設置低優先權

ResumeThread(pi.hThread);

return TRUE;

}

else // 如果出錯,格式化分配的空間

{

SetPriorityClass(GetCurrentProcess(),

NORMAL_PRIORITY_CLASS);

SetThreadPriority(GetCurrentThread(),

THREAD_PRIORITY_NORMAL);

}

}

return FALSE;

}

  • 上一篇:行政管理工作流程
  • 下一篇:e-books and paperbook
  • copyright 2024編程學習大全網