
怎么用visual studio.net2003 编的c++例子程序在XP下运行?
怎么用的?黄维通编的那本面向对象设计里的例子。sample:#include<windows.h>#include<stdlib.h>#include<string.h>...
怎么用的?黄维通编的那本面向对象设计里的例子。sample:
#include <windows.h>
#include <stdlib.h>
#include <string.h>
long WINAPI WndProc(HWND hWnd,UINT iMessage,UINT wParam,LONG lParam);
BOOL InitWindowsClass(HINSTANCE hInstance);
BOOL InitWindows(HINSTANCE hInstance,int nCmdShow);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
MSG Message;
if(!InitWindowsClass(hInstance))
return FALSE;
if(!InitWindows(hInstance,nCmdShow))
return FALSE;
while(GetMessage(&Message,0,0,0))
{
TranslateMessage(&Message);
DispatchMessage(&Message);
}
return Message.wParam;
}
long WINAPI WndProc(HWND hWnd,UINT iMessage,UINT wParam,LONG lParam)
{
HDC hDC;
HBRUSH hBrush;
HPEN hPen;
PAINTSTRUCT PtStr;
switch(iMessage)
{
case WM_PAINT:
hDC=BeginPaint(hWnd,&PtStr);
SetMapMode(hDC,MM_ANISOTROPIC);
hPen=(HPEN)GetStockObject(BLACK_PEN);
hBrush=(HBRUSH)GetStockObject(DKGRAY_BRUSH);
SelectObject(hDC,hBrush);
SelectObject(hDC,hPen);
RoundRect(hDC,50,120,100,200,15,15);
hBrush=(HBRUSH)GetStockObject(LTGRAY_BRUSH);
SelectObject(hDC,hBrush);
Ellipse(hDC,150,50,200,150);
hBrush=(HBRUSH)GetStockObject(HOLLOW_BRUSH);
SelectObject(hDC,hBrush);
Pie(hDC,250,50,300,100,250,50,300,50);
EndPaint(hWnd,&PtStr);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return(DefWindowProc(hWnd,iMessage,wParam,lParam));
}
}
BOOL InitWindows(HINSTANCE hInstance,int nCmdShow)
{
HWND hWnd;
hWnd=CreateWindow("WinFill",
"填充示例程序",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
0,
CW_USEDEFAULT,
0,
NULL,
NULL,
hInstance,
NULL);
if(!hWnd)
return FALSE;
ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
BOOL InitWindowsClass(HINSTANCE hInstance)
{
WNDCLASS WndClass;
WndClass.cbClsExtra=0;
WndClass.cbWndExtra=0;
WndClass.hbrBackground=(HBRUSH)(GetStockObject(WHITE_BRUSH));
WndClass.hCursor=LoadCursor(NULL,IDC_ARROW);
WndClass.hIcon=LoadIcon(NULL,"END");
WndClass.hInstance=hInstance;
WndClass.lpfnWndProc=WndProc;
WndClass.lpszClassName="WinFill";
WndClass.lpszMenuName=NULL;
WndClass.style=CS_HREDRAW|CS_VREDRAW;
return RegisterClass(&WndClass);
}
就是用哪种工程类别如:W32控制台程序,还是别的什么;为什么用W32控制台程序编译链接生成的exe程序在xp下运行,一个DOS窗口一闪即过,看不到绘图过程啊。有高手相助吗? 展开
#include <windows.h>
#include <stdlib.h>
#include <string.h>
long WINAPI WndProc(HWND hWnd,UINT iMessage,UINT wParam,LONG lParam);
BOOL InitWindowsClass(HINSTANCE hInstance);
BOOL InitWindows(HINSTANCE hInstance,int nCmdShow);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
MSG Message;
if(!InitWindowsClass(hInstance))
return FALSE;
if(!InitWindows(hInstance,nCmdShow))
return FALSE;
while(GetMessage(&Message,0,0,0))
{
TranslateMessage(&Message);
DispatchMessage(&Message);
}
return Message.wParam;
}
long WINAPI WndProc(HWND hWnd,UINT iMessage,UINT wParam,LONG lParam)
{
HDC hDC;
HBRUSH hBrush;
HPEN hPen;
PAINTSTRUCT PtStr;
switch(iMessage)
{
case WM_PAINT:
hDC=BeginPaint(hWnd,&PtStr);
SetMapMode(hDC,MM_ANISOTROPIC);
hPen=(HPEN)GetStockObject(BLACK_PEN);
hBrush=(HBRUSH)GetStockObject(DKGRAY_BRUSH);
SelectObject(hDC,hBrush);
SelectObject(hDC,hPen);
RoundRect(hDC,50,120,100,200,15,15);
hBrush=(HBRUSH)GetStockObject(LTGRAY_BRUSH);
SelectObject(hDC,hBrush);
Ellipse(hDC,150,50,200,150);
hBrush=(HBRUSH)GetStockObject(HOLLOW_BRUSH);
SelectObject(hDC,hBrush);
Pie(hDC,250,50,300,100,250,50,300,50);
EndPaint(hWnd,&PtStr);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return(DefWindowProc(hWnd,iMessage,wParam,lParam));
}
}
BOOL InitWindows(HINSTANCE hInstance,int nCmdShow)
{
HWND hWnd;
hWnd=CreateWindow("WinFill",
"填充示例程序",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
0,
CW_USEDEFAULT,
0,
NULL,
NULL,
hInstance,
NULL);
if(!hWnd)
return FALSE;
ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
BOOL InitWindowsClass(HINSTANCE hInstance)
{
WNDCLASS WndClass;
WndClass.cbClsExtra=0;
WndClass.cbWndExtra=0;
WndClass.hbrBackground=(HBRUSH)(GetStockObject(WHITE_BRUSH));
WndClass.hCursor=LoadCursor(NULL,IDC_ARROW);
WndClass.hIcon=LoadIcon(NULL,"END");
WndClass.hInstance=hInstance;
WndClass.lpfnWndProc=WndProc;
WndClass.lpszClassName="WinFill";
WndClass.lpszMenuName=NULL;
WndClass.style=CS_HREDRAW|CS_VREDRAW;
return RegisterClass(&WndClass);
}
就是用哪种工程类别如:W32控制台程序,还是别的什么;为什么用W32控制台程序编译链接生成的exe程序在xp下运行,一个DOS窗口一闪即过,看不到绘图过程啊。有高手相助吗? 展开
2个回答
展开全部
这个是WIN32程序,不是控制台程序,应该先建立一个WIN32程序,在添加代码.
展开全部
在您想要暂停的地方加上 system("pause"); 就可以使 C/C++ 程序暂停。不过,这个办法奏效的前提是系统中必须存在 pause 这个命令。此外,还需要包含标准头文件 stdlib.h(对于 C)或者 cstdlib(对于 C++)。
这个是一个方法 还有 方法2
这种方法稍微有点复杂,但它通用于任何系统,只要这个系统拥有符合标准的 C/C++ 编译器。在您想要暂停的地方加上 getchar();(对于 C 和 C++)或者 cin.get();(仅适用于 C++)就可以使程序暂停,然后按回车程序就会继续执行。不过,您会发现,这种办法却不一定奏效。如果您够细心,会发现只有当 getchar();/cin.get(); 前面有接收输入的语句的时候,该办法才会失效。如果之前没有接收任何输入,该办法是 100% 奏效的!这是因为,如果前面接收了输入,输入流中可能会有残留数据,getchar();/cin.get(); 就会直接读取输入流中的残留数据,而不会等待我们按回车。解决该问题的办法是,先清空输入流,再用 getchar();/cin.get();。清空输入流的办法如下:
1). /* 适用于 C 和 C++。需要包含 stdio.h(对于 C)或者 cstdio(对于 C++)*/
while ( (c = getchar()) != '\n' && c != EOF ) ; /* 对于 C 和 C++ */
2). cin.clear(); // 仅适用于 C++,而且还需要包含标准头文件 limits
cin.ignore( numeric_limits<streamsize>::max(), '\n' );
例如
#include <iostream>
#include <limits>
#include <cstdio>
using namespace std;
int main()
{
int i_test, c;
printf("Please enter an integer: ");
scanf("%d", &i_test);
printf("You just entered %d.\nPress enter to continue...", i_test);
while ( (c = getchar()) != '\n' && c != EOF ) ; // 清空输入流
clearerr(stdin); // 清除流的错误标记
getchar(); // 等待用户输入回车
cout << "Please enter an integer: ";
cin >> i_test;
cout << "You just entered " << i_test << ".\nPress enter to continue...";
cin.clear(); // 清除流的错误标记
cin.ignore( numeric_limits<streamsize>::max(), '\n' ); // 清空输入流
cin.get(); // 等待用户输入回车
return 0;
}
最后还有一个方法 :
先运行 Windows 下的 MS-DOS(对于 Windows 98 等较老的 Windows 系统)或者“命令提示符”(对于 Windows XP 等较新的 Windows 系统),然后进入您想运行的 C/C++ 程序所在的目录。假设该程序位于 D 盘的 cprog 目录下,依次输入以下内容就可以进入该目录:
D:
cd cprog
接着输入该程序的名字,按回车,该程序就会运行起来。假设该程序的名字为 test.exe,我们可以这样输入:
test
其实,不用进入 cprog 也可以运行 test,输入如下:
D:\cprog\test
这个是一个方法 还有 方法2
这种方法稍微有点复杂,但它通用于任何系统,只要这个系统拥有符合标准的 C/C++ 编译器。在您想要暂停的地方加上 getchar();(对于 C 和 C++)或者 cin.get();(仅适用于 C++)就可以使程序暂停,然后按回车程序就会继续执行。不过,您会发现,这种办法却不一定奏效。如果您够细心,会发现只有当 getchar();/cin.get(); 前面有接收输入的语句的时候,该办法才会失效。如果之前没有接收任何输入,该办法是 100% 奏效的!这是因为,如果前面接收了输入,输入流中可能会有残留数据,getchar();/cin.get(); 就会直接读取输入流中的残留数据,而不会等待我们按回车。解决该问题的办法是,先清空输入流,再用 getchar();/cin.get();。清空输入流的办法如下:
1). /* 适用于 C 和 C++。需要包含 stdio.h(对于 C)或者 cstdio(对于 C++)*/
while ( (c = getchar()) != '\n' && c != EOF ) ; /* 对于 C 和 C++ */
2). cin.clear(); // 仅适用于 C++,而且还需要包含标准头文件 limits
cin.ignore( numeric_limits<streamsize>::max(), '\n' );
例如
#include <iostream>
#include <limits>
#include <cstdio>
using namespace std;
int main()
{
int i_test, c;
printf("Please enter an integer: ");
scanf("%d", &i_test);
printf("You just entered %d.\nPress enter to continue...", i_test);
while ( (c = getchar()) != '\n' && c != EOF ) ; // 清空输入流
clearerr(stdin); // 清除流的错误标记
getchar(); // 等待用户输入回车
cout << "Please enter an integer: ";
cin >> i_test;
cout << "You just entered " << i_test << ".\nPress enter to continue...";
cin.clear(); // 清除流的错误标记
cin.ignore( numeric_limits<streamsize>::max(), '\n' ); // 清空输入流
cin.get(); // 等待用户输入回车
return 0;
}
最后还有一个方法 :
先运行 Windows 下的 MS-DOS(对于 Windows 98 等较老的 Windows 系统)或者“命令提示符”(对于 Windows XP 等较新的 Windows 系统),然后进入您想运行的 C/C++ 程序所在的目录。假设该程序位于 D 盘的 cprog 目录下,依次输入以下内容就可以进入该目录:
D:
cd cprog
接着输入该程序的名字,按回车,该程序就会运行起来。假设该程序的名字为 test.exe,我们可以这样输入:
test
其实,不用进入 cprog 也可以运行 test,输入如下:
D:\cprog\test
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询