孙鑫的教程视频网址是http://www.sunxin.org/
先把孙鑫老师的VC++教程学一遍。算是VC++入门吧。
所有的代码在vs2008下调试通过。
下面是第一课内容:创建一个窗口。
//vs2008
#include <TCHAR.h>//长字符
#include<windows.h>
#include <stdio.h>
LRESULT CALLBACK WinSunProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
);//windows程序入口函数
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nShowCmd
)
{
WNDCLASS wndcls; //设计一个窗口类
wndcls.cbClsExtra=0;
wndcls.cbWndExtra=0;
wndcls.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndcls.hCursor=LoadCursor(NULL,IDC_ARROW);
wndcls.hIcon=LoadIcon(NULL,IDI_WINLOGO);
wndcls.hInstance=hInstance;
wndcls.lpfnWndProc=WinSunProc;
wndcls.lpszClassName=_T("study_01");//注意这里_T("study_01")转换为长字符集。
wndcls.lpszMenuName=NULL;
wndcls.style=CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wndcls); //注册窗口类
HWND hwnd; //创建窗口
hwnd=CreateWindow(_T("study_01"),_T("Study 01"),WS_OVERLAPPEDWINDOW | WS_HSCROLL | WS_VSCROLL,CW_USEDEFAULT,CW_USEDEFAULT,600,400,NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,SW_SHOWNORMAL); //显示窗口
UpdateWindow(hwnd); //更新窗口
MSG msg;//消息循环
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}//end of WinMain//消息回调函数
LRESULT CALLBACK WinSunProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
)
{
switch(uMsg)
{
case WM_CHAR:
{
TCHAR szChar[20];
wsprintf(szChar, _T("char is %d"),wParam);
MessageBox(hwnd, szChar, _T("tell you"), 0);
break;
}
case WM_LBUTTONDOWN:
{
// MessageBox(hwnd,_T("mouse clicked"),_T("mouse"),0);
HDC hdc;
hdc=GetDC(hwnd);
//
static int i=50;
i+=20;
if(i>=300)i=50;
TextOut(hdc,10,i,_T("mouse left clicked"),wcslen(_T("mouse left clicked")));//wcslen(_T(
ReleaseDC(hwnd,hdc);
break;
}
case WM_PAINT:
{
HDC hDC;
PAINTSTRUCT ps;
hDC=BeginPaint(hwnd,&ps);
TextOut(hDC,10,10,_T("haha"),wcslen(_T("haha")));
EndPaint(hwnd,&ps);
break;
}
case WM_CLOSE:
if(IDYES==MessageBox(hwnd,_T("真的要结束么?"),_T("close"),MB_YESNO))
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}//end of WinSunProc