helloworld问题(windows编程)

yangzifish 2006-09-28 10:23:03
#include<windows.h>

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

int WINAPI WinMain(HINSTANCE hinstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
static TCHAR szAppName[]=TEXT("HelloWin");
static TCHAR szClassName[]=TEXT("HelloWinClass");

HWND hwnd;
MSG msg;
WNDCLASS wndclass;

wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szClassName;

if(!RegisterClass(&wndclass))
{
MessageBox(NULL,TEXT("This program requires Windows NT!"),szAppName,MB_ICONERROR);
return 0;
}

hwnd = CreateWindow(szClassName,TEXT("The HelloWin Program"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);

ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);

while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;

LRESULT CALLBACK WndProc(HWND hwnd,UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;

switch(message)
{
case WM_CREATE:
MessageBox(NULL,"Creating... ...","HelloWinMessage:",MB_OK);
return 0;

case WM_PAINT:
hdc = BeginPaint(hwnd,&ps);
GetClientRect(hwnd,&rect);
DrawText(hdc,TEXT("Hello,Windows XP!"),-1,&rect,
DT_SINGLELINE | DT_CENTER | DT_VCENTER);
EndPaint(hwnd,&ps);
return 0;

case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}
这段源代码是《Windows下的C/C++高级编程》书上的源码,可我却编译不过(WinXP+VC 6.0),不知是什么原因,代码我对照过了,和书上一样!还有我觉得好像括号不匹配,可加了以后,编译器又报错!
本人初学windows编程,对你的帮助,小弟不胜感激!
...全文
189 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Heaven_Redsky 2006-09-29
  • 打赏
  • 举报
回复
类似这种框架程序
不需要自己敲 反而容易落下东西
找找看有没有电子版的例子
或者直接在.net什么的里边建一个工程
它就提供出来
hailongchang 2006-09-29
  • 打赏
  • 举报
回复
lz,I have recompiled the source code for you, you can copy them to your source code file, then compile it in the console mode

cl ***.c user32.lib GDI32.lib

#include<windows.h>

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

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
static TCHAR szAppName[]=TEXT("HelloWin");
static TCHAR szClassName[]=TEXT("HelloWinClass");

HWND hwnd;
MSG msg;
WNDCLASS wndclass;

wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szClassName;

if(!RegisterClass(&wndclass))
{
MessageBox(NULL,TEXT("This program requires Windows NT!"),szAppName,MB_ICONERROR);
return 0;
}

hwnd = CreateWindow(szClassName,TEXT("The HelloWin Program"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);

ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);

while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT message, WPARAM wParam,LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;

switch(message)
{
case WM_CREATE:
MessageBox(NULL,"Creating... ...","HelloWinMessage:",MB_OK);
return 0;

case WM_PAINT:
hdc = BeginPaint(hwnd,&ps);
GetClientRect(hwnd,&rect);
DrawText(hdc,TEXT("Hello,Windows XP!"),-1,&rect,
DT_SINGLELINE | DT_CENTER | DT_VCENTER);
EndPaint(hwnd,&ps);
return 0;

case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}
carbonic 2006-09-29
  • 打赏
  • 举报
回复
少了一个括弧。
建议lz使用这样的代码用复制粘贴,否则是没事找事。
yangzifish 2006-09-29
  • 打赏
  • 举报
回复
谢谢各位的指点和帮助,是我不够细心导致了这个问题!哈哈,以后一定注意!
a_b_c_abc11 2006-09-28
  • 打赏
  • 举报
回复
一个大小写输入错误
int WINAPI WinMain(HINSTANCE hinstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)

hinstance ->> hInstance


return msg.wParam;

} // 这里少了括号


其他没问题,已运行过。
beginnow 2006-09-28
  • 打赏
  • 举报
回复
没有几个错误
//wndclass.hInstance = hInstance; ->wndclass.hInstance = hinstance;
hwnd = CreateWindow(szClassName,TEXT("The HelloWin Program"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance, ->hinstance
NULL);

最后 WinMain()
{
} //写上
wxspll 2006-09-28
  • 打赏
  • 举报
回复
你有没有建工程?Win32 Application --> A simple Win32 application


while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;

} // 这里少了括号

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧