- 新建项目,我选择的是windows桌面向导,选择windows桌面程序会自动加载模板,初学者看不懂,之后选择空项目。
- 在解决方案资源管理器窗口右键单击添加新建项。没有c文件,直接将名称改成hellowin.c即可。
/*-------------------------------------------------------- HELLOWIN.C--Diaplays"Hello,Windows 10!"in client area (c)Bing Lee,2018 --------------------------------------------------------*/ #include<windows.h> LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevinstance, PSTR szCmdLine, int iCmdShow) { static TCHAR szAppName[] = TEXT("HelloWin"); 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 = szAppName; if (!RegisterClass(&wndclass)) { MessageBox(NULL, TEXT("This program requires Window NT!"), szAppName, MB_ICONERROR); return 0; } hwnd = CreateWindow(szAppName, //window class name TEXT("The Hello Program"), //window caption WS_OVERLAPPEDWINDOW, //window style CW_USEDEFAULT, //initial x position CW_USEDEFAULT, //initial y position CW_USEDEFAULT, //initial x size CW_USEDEFAULT, //initial y size NULL, //parent window handle NULL, //window menu handle hinstance, //program instance handle NULL); //creation parameters ShowWindow(hwnd, iCmdShow); 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: PlaySound(TEXT("hellowin.wav"), NULL, SND_FILENAME | SND_ASYNC); return 0; case WM_PAINT: hdc = BeginPaint(hwnd, &ps); GetClientRect(hwnd, &rect); DrawText(hdc, TEXT("Hello, windows 10!"), -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); }
- 添加lib文件。资源管理器右键单击选择属性。C/C++>常规>附加包含目录 添加WINMM.LIB,链接器>常规>附加库目录 添加WINMM.LIB,链接>输入>附加依赖项 添加WINMM.LIB。
- 修改子系统。链接器>系统>子系统 选择窗口(/SUBSYSTEM:WINDOWS)。
vs2017写windows程序设计(第五版)hellowin.c程序
猜你喜欢
转载自blog.csdn.net/qq_28510897/article/details/81055223
今日推荐
周排行