代码改错

#include <windows.h>#include <string>string MenuName = "NULL";string Windowclass = "w32";string Tit = "jiandan";LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hprevInstance,LPSTR lpCmdLine,int nShowCmd){ MSG msg; WNDCLASS wc; wc.style =CS_HREDRAW|CS_VREDRAW; wc.lpfnWndProc = (WNDPROC)|WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = NULL; wc.hCursor = NULL; wc.hbrBackground = NULL; wc.lpszMenuName = MenuName; wc.lpszClassName = WindowClass; RegisterClass(&wc); HWND hwnd; hwnd = GreateWindow( WindowClass, Tit, WS_OVERLAPPEDWINDOW|WS_VSCROLL|WS_HSCROLL, 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){ PAINTSTRUCT ps; HDC hdc; string szHello = "hello"; switch (message) { case WM_PAINT: hdc = BeginPaint(hwnd,&ps); TextOut(hdc,80,150,szHello,strlen(szHello)); EndPaint(hwnd,&ps); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd,message,wParam,lParam); } return 0;}问下,我是初学者。这段代码哪里错了?我看不出来。
2026年09月22日 10:33
有3个网友回答
网友(1):

呵呵,错误非常多啊!有打错字的,比如CreateWindows,把C写成了G;还有漏了“)”,还有一些基本概念的错误……非常非常多……都不知从何说起。
干脆直接把我写的源码给你看吧。注意:和你的程序有多处不同,但大致一个效果:在屏幕上显示一个窗口,上边写着“Hello”,源代码如下,你可以自己对照着我写的,把你的程序改一下:

#include
#include
HWND hWnd;
LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hprevInstance,LPSTR lpCmdLine,int nShowCmd)
{
MSG msg;
WNDCLASS wc;
wc.style =CS_HREDRAW|CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = NULL;
wc.hCursor = NULL;
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = "w32";
RegisterClass(&wc);
hWnd = CreateWindow(
"w32",
"jiandan",
WS_OVERLAPPEDWINDOW|WS_VSCROLL|WS_HSCROLL,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hWnd,nShowCmd);
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)
{
PAINTSTRUCT ps;
HDC hdc;
char szHello[10] = "hello";
switch (message)
{
case WM_PAINT:
hdc = BeginPaint(hWnd,&ps);
TextOut(hdc,80,150,szHello,strlen(szHello));
EndPaint(hWnd,&ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd,message,wParam,lParam);
}
return 0;
}

附注:如果你想运行看效果,那请创建一个Win32工程,注意不要建控制台,否则编译器会说找不到main()入口点。应该建Win32 Application,然后在Source Files文件夹里添加一个C/C++ Source File,把我写的这段代码全部粘上即可。按F7编译连接,肯定通过!之后按Ctrl+F5就可以看到那个窗口啦!快试试吧。

网友(2):

string使用之前要加个using namespace std;而且在需要char*的时候要用c_str()成员来或取。

网友(3):

学习