首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VC/MFC >

自己写的一个WIN32程序,提示异常N多,不知道为什么

2012-05-13 
自己写的一个WIN32程序,提示错误N多,不知道为什么!请各位帮忙看看,谢谢了!#include Windows.h#include

自己写的一个WIN32程序,提示错误N多,不知道为什么!
请各位帮忙看看,谢谢了!

#include <Windows.h>
#include <stdio.h>

LRESULT CALLBACK WndSunProc(HWND hwnd, UINT uMsg,
WPARAM wparam, LPARAM lparam);
int WINAPI WinMain(
  HINSTANCE hInstance, 
  HINSTANCE hPrevInstance, 
  LPWSTR lpCmdLine, 
  int nShowCmd )
{
WNDCLASS wndclass;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wndclass.hCursor = LoadCursor(NULL, IDC_APPSTARTING);
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.lpfnWndProc = WndSunProc;
wndclass.lpszClassName = TEXT("MyFirstWin32");
wndclass.lpszMenuName = NULL;
wndclass.style = CS_VREDRAW | CS_HREDRAW;

RegisterClass(&wndclass);
HWND hwnd;
hwnd = CreateWindow(TEXT("MyFirstWin32"), "谢谢使用", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, NULL, NULL, hInstance,NULL);
UpdateWindow(hwnd);

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

LRESULT CALLBACK WndSunProc(HWND hwnd, UINT uMsg,
WPARAM wparam, LPARAM lparam)
{
switch (uMsg)
{
case WM_PAINT:
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
TextOut(hdc, 10, 20, TEXT("I love you more than my life!"), 
strlen("I love you more than my life!"));
EndPaint(hwnd, &ps);
break;
case WM_LBUTTONDOWN:
MessageBox(hwnd, "您点击了鼠标左键", "鼠标左键", MB_OK);
HDC hdc;
hdc = GetDC(hwnd);
  TextOut(hdc, 20, 30,TEXT("您点击了鼠标左键"), strlen("您点击了鼠标左键"));
ReleaseDC(hwnd, hdc);
break;
case WM_RBUTTONDOWN:
MessageBox(hwnd, "您点击了鼠标右键", "鼠标右键", MB_OK);
HDC hdc = GetDC(hwnd);
TextOut(hdc, 30, 40,"您点击了鼠标右键", strlen("您点击了鼠标右键"));
ReleaseDC(hwnd, hdc);
break;
case WM_CHAR:
char szChar[20];
sprintf(szChar, "it is %c", wparam);
MessageBox(hwnd, szChar, TEXT("Thankyou"), MB_OK);
break;
case WM_CLOSE:
if (IDYES == MessageBox(hwnd, "确定关闭?", "离开", MB_YESNO))
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);

default:
return DefWindowProc(hwnd, uMsg, wparam, lparam);
}
return 0;
}



G:\C++学习\我的C++程序\MFC\WinMain\WinMain.cpp(11) : error C2731: 'WinMain' : function cannot be overloaded
  G:\C++学习\我的C++程序\MFC\WinMain\WinMain.cpp(6) : see declaration of 'WinMain'
G:\C++学习\我的C++程序\MFC\WinMain\WinMain.cpp(52) : error C2360: initialization of 'hdc' is skipped by 'case' label
  G:\C++学习\我的C++程序\MFC\WinMain\WinMain.cpp(47) : see declaration of 'hdc'
G:\C++学习\我的C++程序\MFC\WinMain\WinMain.cpp(54) : error C2086: 'hdc' : redefinition
G:\C++学习\我的C++程序\MFC\WinMain\WinMain.cpp(59) : error C2360: initialization of 'hdc' is skipped by 'case' label
  G:\C++学习\我的C++程序\MFC\WinMain\WinMain.cpp(47) : see declaration of 'hdc'
G:\C++学习\我的C++程序\MFC\WinMain\WinMain.cpp(61) : error C2374: 'hdc' : redefinition; multiple initialization
  G:\C++学习\我的C++程序\MFC\WinMain\WinMain.cpp(47) : see declaration of 'hdc'
G:\C++学习\我的C++程序\MFC\WinMain\WinMain.cpp(65) : error C2360: initialization of 'hdc' is skipped by 'case' label
  G:\C++学习\我的C++程序\MFC\WinMain\WinMain.cpp(61) : see declaration of 'hdc'
G:\C++学习\我的C++程序\MFC\WinMain\WinMain.cpp(65) : error C2360: initialization of 'hdc' is skipped by 'case' label


  G:\C++学习\我的C++程序\MFC\WinMain\WinMain.cpp(47) : see declaration of 'hdc'
G:\C++学习\我的C++程序\MFC\WinMain\WinMain.cpp(70) : error C2360: initialization of 'hdc' is skipped by 'case' label
  G:\C++学习\我的C++程序\MFC\WinMain\WinMain.cpp(61) : see declaration of 'hdc'
G:\C++学习\我的C++程序\MFC\WinMain\WinMain.cpp(70) : error C2360: initialization of 'hdc' is skipped by 'case' label
  G:\C++学习\我的C++程序\MFC\WinMain\WinMain.cpp(47) : see declaration of 'hdc'
G:\C++学习\我的C++程序\MFC\WinMain\WinMain.cpp(76) : error C2360: initialization of 'hdc' is skipped by 'case' label
  G:\C++学习\我的C++程序\MFC\WinMain\WinMain.cpp(61) : see declaration of 'hdc'
G:\C++学习\我的C++程序\MFC\WinMain\WinMain.cpp(76) : error C2360: initialization of 'hdc' is skipped by 'case' label
  G:\C++学习\我的C++程序\MFC\WinMain\WinMain.cpp(47) : see declaration of 'hdc'
G:\C++学习\我的C++程序\MFC\WinMain\WinMain.cpp(79) : error C2361: initialization of 'hdc' is skipped by 'default' label
  G:\C++学习\我的C++程序\MFC\WinMain\WinMain.cpp(61) : see declaration of 'hdc'
G:\C++学习\我的C++程序\MFC\WinMain\WinMain.cpp(79) : error C2361: initialization of 'hdc' is skipped by 'default' label
  G:\C++学习\我的C++程序\MFC\WinMain\WinMain.cpp(47) : see declaration of 'hdc'
执行 cl.exe 时出错.

WinMain.obj - 1 error(s), 0 warning(s)


[解决办法]

C/C++ code
#include <Windows.h>#include <stdio.h>LRESULT CALLBACK WndSunProc(HWND hwnd, UINT uMsg,WPARAM wparam, LPARAM lparam);int WINAPI WinMain(  HINSTANCE hInstance,    HINSTANCE hPrevInstance,    LPSTR lpCmdLine,                // error 1  int nShowCmd ){WNDCLASS wndclass;wndclass.cbClsExtra = 0;wndclass.cbWndExtra = 0;wndclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);wndclass.hCursor = LoadCursor(NULL, IDC_APPSTARTING);wndclass.hInstance = hInstance;wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);wndclass.lpfnWndProc = WndSunProc; wndclass.lpszClassName = TEXT("MyFirstWin32");wndclass.lpszMenuName = NULL;wndclass.style = CS_VREDRAW | CS_HREDRAW;RegisterClass(&wndclass);HWND hwnd;hwnd = CreateWindow(TEXT("MyFirstWin32"), "谢谢使用", WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,CW_USEDEFAULT, NULL, NULL, hInstance,NULL);UpdateWindow(hwnd);MSG msg;while (GetMessage(&msg, NULL, 0, 0)){TranslateMessage(&msg);DispatchMessage(&msg);}return 0;}LRESULT CALLBACK WndSunProc(HWND hwnd, UINT uMsg,WPARAM wparam, LPARAM lparam){switch (uMsg){case WM_PAINT:    {//error 2 加括号,否则case下定义的变量编译通不过    PAINTSTRUCT ps;HDC hdc = BeginPaint(hwnd, &ps);TextOut(hdc, 10, 20, TEXT("I love you more than my life!"),  strlen("I love you more than my life!"));EndPaint(hwnd, &ps);    }break;case WM_LBUTTONDOWN:    {MessageBox(hwnd, "您点击了鼠标左键", "鼠标左键", MB_OK);HDC hdc;hdc = GetDC(hwnd);  TextOut(hdc, 20, 30,TEXT("您点击了鼠标左键") , strlen("您点击了鼠标左键"));ReleaseDC(hwnd, hdc);    }break;case WM_RBUTTONDOWN:    {MessageBox(hwnd, "您点击了鼠标右键", "鼠标右键", MB_OK);HDC hdc = GetDC(hwnd);TextOut(hdc, 30, 40,"您点击了鼠标右键", strlen("您点击了鼠标右键"));ReleaseDC(hwnd, hdc);    }break;case WM_CHAR: char szChar[20];sprintf(szChar, "it is %c", wparam);MessageBox(hwnd, szChar, TEXT("Thankyou"), MB_OK);break;case WM_CLOSE:if (IDYES == MessageBox(hwnd, "确定关闭?", "离开", MB_YESNO)){DestroyWindow(hwnd);}break;case WM_DESTROY:PostQuitMessage(0);default:return DefWindowProc(hwnd, uMsg, wparam, lparam);}return 0;}
[解决办法]
探讨

引用:
请各位帮忙看看,谢谢了!

#include <Windows.h>
#include <stdio.h>

LRESULT CALLBACK WndSunProc(HWND hwnd, UINT uMsg,


WPARAM wparam, LPARAM lparam);
int WINAPI WinMain(
HINSTANCE hInstan……

……


[解决办法]
你用的不是VC6吧
[解决办法]
看编译选项,如果是_UNICODE,楼主的程序应该没问题。如果不是_UNICODE选项,那就不能用 LPWSTR 之类的宏了

热点排行