初学win32编程,抄的书上随机生成线段的代码,但是报错
winmain 无法重载
全代码如下,请朋友们救我
#define _CRT_SECURE_NO_WARNINGS 1
#define WIN32_LEAN_AND_MEAN
#include<Windows.h>
#include<windowsx.h>
#include<time.h>
#include<stdlib.h>
#define MAX_STRING 124
//全局变量
HINSTANCE hInst;
TCHAR szTitle[MAX_STRING]; //标题栏文字
TCHAR szWindowClass[MAX_STRING];
HWND hWnd; //主窗口句柄
//函数声明
ATOM RegisterWindowClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void OnPaint(HDC pDC);
//主函数
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
MSG msg;
wcscpy_s(szTitle, L"GDIDemo");
wcscpy_s(szWindowClass, L"Win32");
RegisterWindowClass(hInstance);
hInst = hInstance;
if (!InitInstance(hInstance, nCmdShow))
{
return FALSE;
}
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
//注册窗口类
ATOM RegisterWindowClass(HINSTANCE hInstance)
{
WNDCLASSEXW wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc =(WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL,IDC_ARROW);
wcex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); ;
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(NULL,IDI_APPLICATION);
return RegisterClassEx(&wcex);
}
//创建主窗口
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hWnd = CreateWindowEx(NULL,szWindowClass, szTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0,
CW_USEDEFAULT, 0,NULL,
NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//消息处理
LRESULT CALLBACK WndProc(HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
//绘图
OnPaint(hdc);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_LBUTTONDOWN:
MessageBox(hWnd, L"Mouse left button down", L"win32sample", MB_OK);
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
void OnPaint(HDC hdc)
{
srand((unsigned)time(NULL));
RECT clientRect;
GetClientRect(hWnd, &clientRect);
int maxX = clientRect.right;
int maxY = clientRect.bottom;
for (int i = 0; i < 20; i++)
{
int r = rand() % 256;
int g = rand() % 256;
int b = rand() % 256;
HPEN newPen = CreatePen(PS_SOLID, 1, RGB(r, g, b));
HPEN oldPen = (HPEN)SelectObject(hdc,newPen);
int x0 = rand() % maxX;
int y0 = rand() % maxY;
MoveToEx(hdc, x0, y0, NULL);
int x1 = rand() % maxX;
int y1 = rand() % maxY;
LineTo(hdc, x1, y1);
DeleteObject(newPen);
SelectObject(hdc, oldPen);
}
HBRUSH newbrush = CreateSolidBrush(RGB(255, 0, 0));
HBRUSH oldbrush = (HBRUSH)SelectObject(hdc,newbrush);
Ellipse(hdc, 20, 20, 40, 40);
Ellipse(hdc, 100, 100, 200, 140);
SelectObject(hdc, oldbrush);
DeleteObject(newbrush);
}