高分求VC++写的简单的对话框程序
3个回答
2014-05-22
展开全部
“执行同目录下的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;
}
必须是直接执行相同目录下的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;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2014-05-22
展开全部
MFC建立个对话框程序即可
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2014-05-22
展开全部
1=二合
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询