请大家来帮我解决这个问题
时间:2011-11-27
来源:互联网
一个求素数的程序,用API来编写让它输出到窗口中,可是它确不能正常输出我想要的结果代码如下:
[code=C/C++][/code]#include <Windows.h>
#include <stdlib.h>
#include <tchar.h>
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
TCHAR szAppName[]=TEXT("阿锋API练习");
TCHAR szClassName[]=TEXT("exercise");
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR szCmdLine,int iCmdShow)
{
WNDCLASS winclass;
MSG message;
HWND hWnd;
winclass.style = CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS;
winclass.lpfnWndProc = WndProc;
winclass.cbClsExtra = 0;
winclass.cbWndExtra = 0;
winclass.hInstance = hInstance;
winclass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
winclass.hCursor = LoadCursor(NULL,IDC_ARROW);
winclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
winclass.lpszMenuName = NULL;
winclass.lpszClassName = szClassName;
if (!RegisterClass(&winclass))
{
MessageBox(NULL,TEXT("Register fail"),szClassName,MB_ICONERROR);
return 0;
}
hWnd=CreateWindow(szClassName,szAppName,WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,
CW_USEDEFAULT,CW_USEDEFAULT,
NULL,NULL,hInstance,NULL);
ShowWindow(hWnd,iCmdShow);
UpdateWindow(hWnd);
while(GetMessage(&message,NULL,0,0))
{
TranslateMessage(&message);
DispatchMessage(&message);
}
return message.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
TCHAR szBuffer[20];
int a[1000],i,j;
PAINTSTRUCT ps;
static int cxClient,cyClient;
int x=0,y=0;
switch(message)
{
case WM_SIZE:
cxClient=LOWORD(lParam);
cyClient=HIWORD(lParam);
break
case WM_PAINT:
hdc=BeginPaint(hWnd,&ps);
hdc=GetDC(hWnd);
for (i=1;i<=1000;i++) //求素数代码,
{ //这段代码经过验证是正确的
a[i-1]=i;
}
for (i=0;i<=999;i++)
{
for (j=2;j<=a[i];j++)
{
if (a[i]%j==0)
{
a[i]=0;
break;
}
}
if (a[i]>1) //以下为输出,可是我只看见窗口,
{ //其他的什么也没看到
wsprintf(szBuffer,TEXT("%d"),a[i],10);
TextOut(hdc,50,50,szBuffer,20);
}
}
ReleaseDC(hWnd,hdc);
EndPaint(hWnd,&ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd,message,wParam,lParam);
}
[code=C/C++][/code]#include <Windows.h>
#include <stdlib.h>
#include <tchar.h>
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
TCHAR szAppName[]=TEXT("阿锋API练习");
TCHAR szClassName[]=TEXT("exercise");
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR szCmdLine,int iCmdShow)
{
WNDCLASS winclass;
MSG message;
HWND hWnd;
winclass.style = CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS;
winclass.lpfnWndProc = WndProc;
winclass.cbClsExtra = 0;
winclass.cbWndExtra = 0;
winclass.hInstance = hInstance;
winclass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
winclass.hCursor = LoadCursor(NULL,IDC_ARROW);
winclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
winclass.lpszMenuName = NULL;
winclass.lpszClassName = szClassName;
if (!RegisterClass(&winclass))
{
MessageBox(NULL,TEXT("Register fail"),szClassName,MB_ICONERROR);
return 0;
}
hWnd=CreateWindow(szClassName,szAppName,WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,
CW_USEDEFAULT,CW_USEDEFAULT,
NULL,NULL,hInstance,NULL);
ShowWindow(hWnd,iCmdShow);
UpdateWindow(hWnd);
while(GetMessage(&message,NULL,0,0))
{
TranslateMessage(&message);
DispatchMessage(&message);
}
return message.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
TCHAR szBuffer[20];
int a[1000],i,j;
PAINTSTRUCT ps;
static int cxClient,cyClient;
int x=0,y=0;
switch(message)
{
case WM_SIZE:
cxClient=LOWORD(lParam);
cyClient=HIWORD(lParam);
break
case WM_PAINT:
hdc=BeginPaint(hWnd,&ps);
hdc=GetDC(hWnd);
for (i=1;i<=1000;i++) //求素数代码,
{ //这段代码经过验证是正确的
a[i-1]=i;
}
for (i=0;i<=999;i++)
{
for (j=2;j<=a[i];j++)
{
if (a[i]%j==0)
{
a[i]=0;
break;
}
}
if (a[i]>1) //以下为输出,可是我只看见窗口,
{ //其他的什么也没看到
wsprintf(szBuffer,TEXT("%d"),a[i],10);
TextOut(hdc,50,50,szBuffer,20);
}
}
ReleaseDC(hWnd,hdc);
EndPaint(hWnd,&ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd,message,wParam,lParam);
}
作者: chenwfeng 发布时间: 2011-11-27
建议去网上找一些好的程序看一下。
作者: gofqjyie 发布时间: 2011-11-27
wsprintf(szBuffer,TEXT("%d"),a[i],10); ??
作者: xuddk727 发布时间: 2011-11-27
if (a[i]>1) //以下为输出,可是我只看见窗口,
{ //其他的什么也没看到
wsprintf(szBuffer,TEXT("%d "),a[i]);//多加几个空格覆盖输出
TextOut(hdc,50,50,szBuffer,_tcslen(szBuffer));
}
{ //其他的什么也没看到
wsprintf(szBuffer,TEXT("%d "),a[i]);//多加几个空格覆盖输出
TextOut(hdc,50,50,szBuffer,_tcslen(szBuffer));
}
作者: xuddk727 发布时间: 2011-11-27
试了一下,应该是if (a[i]>1)有问题,根本就没进去画字符串处理,你可以加个断点调试一下
作者: leegoex 发布时间: 2011-11-27
C/C++ code
看看是两个对话框运行正常么,判断语句在哪出错!
if (a[i]>1) //以下为输出,可是我只看见窗口, { //其他的什么也没看到 // MessageBox(1) wsprintf(szBuffer,TEXT("%d "),a[i]);//多加几个空格覆盖输出 // MessageBox(2) TextOut(hdc,50,50,szBuffer,_tcslen(szBuffer)); }
看看是两个对话框运行正常么,判断语句在哪出错!
作者: alln0211 发布时间: 2011-11-27
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28