犹记得大二暑假在无锡汉方公司培训的情景,在学MFC之初,包大哥给我们讲了这堂课:最基本的Win32程序。
而如今毕业快一年了,有同事看我会编写这些简单的可视化程序,还向我请教,其实我知道这些都不一定有多少技术含量,只不过用得多,多熟悉点而已。实在没什么值得骄傲,虽说目前还只是初来乍到的新人,也没有多少特长技能,也没有那些一毕业就进大公司做大项目的同学优秀,但在汉方培训的时候本人还是学到不少东东的,今天分享下“最基本的Win32程序”。
#include <windows.h>
//function declaration
BOOL InitApplication(HINSTANCE hInstance); //registers a window class for subsequent use in calls to CreateWindow
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow); //create window
LRESULT CALLBACK MainWndProc(HWND,UINT,WPARAM,LPARAM); //window function declaration
//global variable
HWND hwnd; //handle to a window
char lpszClassName[] = "MyWindow"; //window class name
//------------ Initialize the window class----------------
int WINAPI WinMain(HINSTANCE hInstance, //handle to current instance
HINSTANCE hPrevInst, //handle to previous instance
LPSTR lpszCmdLine, //command line
int nCmdShow) //show state
{
MSG Msg ;
if (!InitApplication(hInstance)) //registers a window class for subsequent use in calls to CreateWindow
{
return FALSE;
}
if (!InitInstance(hInstance, nCmdShow)) //create window
{
return FALSE;
}
while( GetMessage(&Msg, NULL, 0, 0)) //message loop
{
TranslateMessage( &Msg) ; //translates virtual-key messages into character message, example: WM_CHAR message
DispatchMessage( &Msg) ; //dispatches a message to a window procedure
}
return Msg.wParam; //loop end, return value to system
}
//registers a window class for subsequent use in calls to CreateWindow
BOOL InitApplication(HINSTANCE hInstance)
{
WNDCLASS wndclass; //WNDCLASS只是一个结构体,下面给其成员赋值
//窗口类的定义
wndclass.style = 0; //窗口类型为缺省类型
wndclass.lpfnWndProc = (WNDPROC)MainWndProc ; //窗口处理函数为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 = lpszClassName; //窗口类名为"窗口示例"
//--------------- 以下进行窗口类的注册 -----------------
if( !RegisterClass( &wndclass)) //如果注册失败则发出警告声音
{
MessageBeep(0) ;
return FALSE ;
}
return TRUE;
}
//create window
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
char lpszTitle[]= "My_Windows"; //窗口标题名
//创建窗口
hwnd=CreateWindow( lpszClassName, //窗口类名
lpszTitle, //窗口实例的标题名
WS_OVERLAPPEDWINDOW, //窗口的风格
CW_USEDEFAULT,
CW_USEDEFAULT, //窗口左上角坐标为缺省值
CW_USEDEFAULT,
CW_USEDEFAULT, //窗口的高和宽为缺省值
NULL, //此窗口无父窗口
NULL, //此窗口无主菜单
hInstance, //创建此窗口的应用程序的当前句柄
NULL) ; //不使用该值
if (!hwnd)
{
return FALSE;
}
ShowWindow( hwnd, nCmdShow); //显示窗口
UpdateWindow(hwnd); //绘制用户区
return TRUE;
}
//window function
LRESULT CALLBACK MainWndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
switch (message)
{
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
TextOut(hdc, 100, 50, "hello world", lstrlen("hello world"));
EndPaint(hwnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
文章来源: http://blog.csdn.net/erick08/article/details/7353162