今天开始学习VC++,照着书上写了几个Win32的程序。

None.gif1,#include <windows.h>
None.gif
None.gif
None.gifLRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam,LPARAM lParam)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    HDC hdc;
//设备环境句柄
InBlock.gif
    PAINTSTRUCT ps;
InBlock.gif
InBlock.gif    RECT rect;
InBlock.gif    POINT point;
InBlock.gif
InBlock.gif    
switch(iMsg)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif    
case WM_PAINT:
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            hdc 
= BeginPaint(hwnd,&ps);
InBlock.gif            GetClientRect(hwnd,
&rect);
InBlock.gif            DrawText(hdc,
"Hello,VC6.0!",-1,&rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
InBlock.gif            EndPaint(hwnd,
&ps);
InBlock.gif            
return 0;
ExpandedSubBlockEnd.gif        }

InBlock.gif    
case WM_LBUTTONDOWN:
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            hdc 
= GetDC(hwnd);
InBlock.gif            point.x 
= LOWORD(lParam);
InBlock.gif            point.y 
= HIWORD(lParam);
InBlock.gif            Ellipse(hdc,point.x
-50,point.y-50,point.x+50,point.y+50);
InBlock.gif            ReleaseDC(hwnd,hdc);
InBlock.gif            
return 0;
ExpandedSubBlockEnd.gif        }

InBlock.gif    
case WM_DESTROY:
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            PostQuitMessage(
0);
InBlock.gif            
return 0;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif        
return DefWindowProc(hwnd,iMsg,wParam,lParam);
InBlock.gif
ExpandedBlockEnd.gif}

None.gif
None.gif
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
static char szAppName[] = "HelloWin32";//应用程序名
InBlock.gif
    HWND hwnd;//窗口句柄
InBlock.gif
    MSG    msg;//消息
InBlock.gif
    WNDCLASSEX wndclass;//窗口类
InBlock.gif
    wndclass.cbSize = sizeof(wndclass);//窗口类结构的大小
InBlock.gif
    wndclass.style = CS_HREDRAW|CS_VREDRAW;//类风格:水平和垂直方向重画
InBlock.gif
    wndclass.lpfnWndProc = WndProc;//窗口过程
InBlock.gif
    wndclass.cbClsExtra = 0;
InBlock.gif    wndclass.cbWndExtra 
= 0;
InBlock.gif    wndclass.hInstance 
= hInstance;
InBlock.gif    wndclass.hIcon 
= LoadIcon(NULL,IDI_APPLICATION);
InBlock.gif    wndclass.hCursor 
= LoadCursor(NULL,IDC_ARROW);
InBlock.gif    wndclass.hbrBackground 
= (HBRUSH)GetStockObject(WHITE_BRUSH);
InBlock.gif    wndclass.lpszClassName 
= szAppName;
InBlock.gif    wndclass.lpszMenuName 
= NULL;
InBlock.gif    wndclass.hIconSm 
= LoadIcon(NULL,IDI_APPLICATION);
InBlock.gif
InBlock.gif    RegisterClassEx(
&wndclass);
InBlock.gif
InBlock.gif    hwnd
=CreateWindow(szAppName,
InBlock.gif                      
"The Hello App",
InBlock.gif                      WS_OVERLAPPEDWINDOW,
InBlock.gif                      CW_USEDEFAULT,
InBlock.gif                      CW_USEDEFAULT,
InBlock.gif                      CW_USEDEFAULT,
InBlock.gif                      CW_USEDEFAULT,
InBlock.gif                      NULL,
InBlock.gif                      NULL,
InBlock.gif                      hInstance,
InBlock.gif                      NULL);
InBlock.gif    ShowWindow(hwnd,nShowCmd);
InBlock.gif    UpdateWindow(hwnd);
InBlock.gif
InBlock.gif    
while(GetMessage(&msg,NULL,0,0))
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        TranslateMessage(
&msg);
InBlock.gif        DispatchMessage(
&msg);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
return msg.wParam;
ExpandedBlockEnd.gif}


 

None.gif2//t1.h
None.gif
class CMyApp:public CWinApp
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
public:
InBlock.gif    
virtual BOOL InitInstance();
ExpandedBlockEnd.gif}
;
None.gif
None.gif
class CMainWindow:public CFrameWnd
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
public:
InBlock.gif    CMainWindow();
InBlock.gif
protected:
InBlock.gif    afx_msg 
void OnPaint();
InBlock.gif    afx_msg 
void OnLButtonDown(UINT nFlags,CPoint point);
InBlock.gif    DECLARE_MESSAGE_MAP()
ExpandedBlockEnd.gif}
;
None.gif
None.gif
//t1.cpp
None.gif
#include <afxwin.h>
None.gif#include 
"t1.h"
None.gif
None.gifCMyApp myApp;
None.gif
None.gifBOOL CMyApp::InitInstance()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
this->m_pMainWnd = new CMainWindow;
InBlock.gif    
this->m_pMainWnd->ShowWindow(this->m_nCmdShow);
InBlock.gif    
this->m_pMainWnd->UpdateWindow();
InBlock.gif    
return TRUE;
ExpandedBlockEnd.gif}

None.gif
None.gifBEGIN_MESSAGE_MAP(CMainWindow,CFrameWnd)
None.gif    ON_WM_PAINT()
None.gif    ON_WM_LBUTTONDOWN()
None.gifEND_MESSAGE_MAP()
None.gif
None.gifCMainWindow::CMainWindow()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    Create(NULL,_T(
"The MFC Application"));
ExpandedBlockEnd.gif}

None.gif
None.gif
void CMainWindow::OnPaint()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    CPaintDC dc(
this);
InBlock.gif    CRect rect;
InBlock.gif    GetClientRect(
&rect);
InBlock.gif    dc.DrawText(
"Weclome to VC!!",-1,&rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
InBlock.gif
ExpandedBlockEnd.gif}

None.gif
None.gif
void CMainWindow::OnLButtonDown(UINT nFlags,CPoint point)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    CClientDC dc(
this);
InBlock.gif    CRect rect(point.x
-50,point.y-50,point.x+50,point.y+50);
InBlock.gif    dc.Ellipse(rect);
ExpandedBlockEnd.gif}