windows 建立窗口的程序代码

来源:互联网 发布:男生双肩包 知乎 编辑:程序博客网 时间:2024/05/30 19:33



#include <windows.h>#include <windowsx.h>//函数声明static BOOL InitWindow( HINSTANCE hInstance, int nCmdShow );LRESULT CALLBACK WinProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam );//*******************************************************************//函数:WinMain()//功能:Win32应用程序入口函数。创建主窗口,处理消息循环//*******************************************************************int WINAPI WinMain( HINSTANCE hInstance, //当前实例句柄HINSTANCE hPrevInstance, //前一个实例句柄LPSTR lpCmdLine, //命令行字符int nCmdShow) //窗口显示方式{MSG msg;//创建主窗口if ( !InitWindow( hInstance, nCmdShow ) )return FALSE;//进入消息循环://从该应用程序的消息队列中检取消息,送到消息处理过程,//当检取到WM_QUIT消息时,退出消息循环。while (GetMessage(&msg, NULL, 0, 0)){TranslateMessage(&msg);DispatchMessage(&msg);}//程序结束return msg.wParam;}//******************************************************************//函数:InitWindow()//功能:创建窗口。//******************************************************************static BOOL InitWindow( HINSTANCE hInstance, int nCmdShow ){HWND hwnd; //窗口句柄WNDCLASS wc; //窗口类结构//填充窗口类结构wc.style = CS_VREDRAW | CS_HREDRAW;wc.lpfnWndProc = (WNDPROC)WinProc;wc.cbClsExtra = 0;wc.cbWndExtra = 0;wc.hInstance = hInstance;wc.hIcon = LoadIcon( hInstance, IDI_APPLICATION );wc.hCursor = LoadCursor( NULL, IDC_ARROW );wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);wc.lpszMenuName = NULL;wc.lpszClassName = "EasyWin";//注册窗口类RegisterClass( &wc );//创建主窗口hwnd = CreateWindow("EasyWin", //窗口类名称"hello", //窗口标题WS_OVERLAPPEDWINDOW, //窗口风格,定义为普通型200, //窗口位置的x坐标200, //窗口位置的y坐标320, //窗口的宽度200, //窗口的高度NULL, //父窗口句柄NULL, //菜单句柄hInstance, //应用程序实例句柄NULL ); //窗口创建数据指针if( !hwnd )return FALSE;//显示并更新窗口ShowWindow( hwnd, nCmdShow );UpdateWindow( hwnd );return TRUE;}///******************************************************************//函数:WinProc()//功能:处理主窗口消息///******************************************************************LRESULT CALLBACK WinProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam ){switch( message ){case WM_KEYDOWN://击键消息switch( wParam ){case VK_ESCAPE:MessageBox(hWnd,"ESC键按下了!","Keyboard",MB_OK);break;}break;case WM_RBUTTONDOWN://鼠标消息{MessageBox(hWnd,"鼠标右键按下了!","Mouse",MB_OK);break;}case WM_LBUTTONDOWN://鼠标消息{MessageBox(hWnd,"OK!You click the right place!!!","Mouse",MB_OK);break;}case WM_PAINT://窗口重画消息{char hello[]="hello world!!!It's the first windows!!!";HDC hdc;PAINTSTRUCT ps;hdc=BeginPaint( hWnd,&ps ); //取得设备环境句柄SetTextColor(hdc, RGB(100,150,100)); //设置文字颜色TextOut( hdc, 40, 80, hello, strlen(hello) );//输出文字EndPaint( hWnd, &ps ); //释放资源break;}case WM_DESTROY://退出消息PostQuitMessage( 0 );//调用退出函数break;}//调用缺省消息处理过程return DefWindowProc(hWnd, message, wParam, lParam);}




原创粉丝点击