【視窗程式設計】一個簡單的C語言視窗程式
阿新 • • 發佈:2019-01-31
【編譯環境】:Visual Studio 2010#include <windows.h> //回撥函式 LRESULT CALLBACK WinProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam) { switch(message) { case WM_CLOSE: DestroyWindow(hwnd); PostQuitMessage(0); break; default: return DefWindowProc(hwnd,message,wParam,lParam); } return 0; } //主函式入口 int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR szCmdLine,int iCmdShow) { TCHAR szname[]=TEXT("classname"); HWND hwnd; MSG msg; ZeroMemory(&msg,sizeof(msg)); WNDCLASS wndclass; wndclass.style=CS_HREDRAW | CS_VREDRAW; wndclass.lpfnWndProc=WinProc; wndclass.cbClsExtra=0; wndclass.cbWndExtra=0; wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH); wndclass.hCursor=LoadCursor(NULL,IDC_ARROW); wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION); wndclass.hInstance=hInstance; wndclass.lpszClassName=szname; wndclass.lpszMenuName=NULL; RegisterClass(&wndclass);//註冊視窗類 hwnd=CreateWindow(szname,TEXT("視窗"),WS_OVERLAPPEDWINDOW ,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL); if(hwnd) ShowWindow(hwnd,SW_SHOW); else { MessageBox(NULL,TEXT("建立視窗失敗!"),TEXT("提示"),MB_OK); UnregisterClass(szname,hInstance); return 1; } UpdateWindow(hwnd); while(GetMessage(&msg,hwnd,0,0)>0) { TranslateMessage(&msg); DispatchMessage(&msg); } UnregisterClass(szname,hInstance); return msg.wParam; }