首页 > 学院 > 开发设计 > 正文

第一个Win32程序

2019-11-10 18:43:32
字体:
来源:转载
供稿:网友
//第一个Win32程序#include <windows.h>int main(){ MessageBox(NULL,TEXT("Hello!"),TEXT("YPP"),MB_OK); return 0;}

在VS2010运行 VS2010

另一种形式

//第一个Win32程序#include <windows.h>int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPRevInstance, LPSTR lpCmdLine, int nCmdShow){ MessageBox(NULL,TEXT("Hello!"),TEXT("YPP"),MB_OK); return 0;}

运行结果 VC6

完整版的“窗口”

出自《Windows程序设计,第五版》作者Charles Petzold代码的核心是WinProc,也就是真正实现消息响应的模块

窗口

//完整的“窗口”#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 ("YPP"), szAppName, MB_ICONERROR) ; return 0 ; } hwnd = CreateWindow (szAppName, TEXT ("The Hello Program"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL) ; 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_PAINT: hdc = BeginPaint (hwnd, &ps) ; GetClientRect (hwnd, &rect) ; DrawText (hdc, TEXT ("这是一个完整的“窗口”"), -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) ;}

在VC6下 新建Win32 Application工程,在工程里新建.cpp 粘贴代码,F7组建,Ctrl+F5运行 工程文件下载 有疑问咨询 QQ/微信 784126524 公众号: 公众号


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表