VC++学习笔记(1)

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

1 ,#include  < windows.h >


LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam,LPARAM lParam)
{
    HDC hdc;
//设备环境句柄
    PAINTSTRUCT ps;

    RECT rect;
    POINT point;

    
switch(iMsg)
    
{
    
case WM_PAINT:
        
{
            hdc 
= BeginPaint(hwnd,&ps);
            GetClientRect(hwnd,
&rect);
            DrawText(hdc,
"Hello,VC6.0!",-1,&rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
            EndPaint(hwnd,
&ps);
            
return 0;
        }

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

    
case WM_DESTROY:
        
{
            PostQuitMessage(
0);
            
return 0;
        }

    }

        
return DefWindowProc(hwnd,iMsg,wParam,lParam);

}


int  WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine, int  nShowCmd)
{
    
static char szAppName[] = "HelloWin32";//应用程序名
    HWND hwnd;//窗口句柄
    MSG    msg;//消息
    WNDCLASSEX wndclass;//窗口类
    wndclass.cbSize = sizeof(wndclass);//窗口类结构的大小
    wndclass.style = CS_HREDRAW|CS_VREDRAW;//类风格:水平和垂直方向重画
    wndclass.lpfnWndProc = 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.lpszClassName 
= szAppName;
    wndclass.lpszMenuName 
= NULL;
    wndclass.hIconSm 
= LoadIcon(NULL,IDI_APPLICATION);

    RegisterClassEx(
&wndclass);

    hwnd
=CreateWindow(szAppName,
                      
"The Hello App",
                      WS_OVERLAPPEDWINDOW,
                      CW_USEDEFAULT,
                      CW_USEDEFAULT,
                      CW_USEDEFAULT,
                      CW_USEDEFAULT,
                      NULL,
                      NULL,
                      hInstance,
                      NULL);
    ShowWindow(hwnd,nShowCmd);
    UpdateWindow(hwnd);

    
while(GetMessage(&msg,NULL,0,0))
    
{
        TranslateMessage(
&msg);
        DispatchMessage(
&msg);
    }


    
return msg.wParam;
}


 

2 // t1.h
class  CMyApp: public  CWinApp
{
public:
    
virtual BOOL InitInstance();
}
;

class  CMainWindow: public  CFrameWnd
{
public:
    CMainWindow();
protected:
    afx_msg 
void OnPaint();
    afx_msg 
void OnLButtonDown(UINT nFlags,CPoint point);
    DECLARE_MESSAGE_MAP()
}
;

// t1.cpp
#include  < afxwin.h >
#include 
" t1.h "

CMyApp myApp;

BOOL CMyApp::InitInstance()
{
    
this->m_pMainWnd = new CMainWindow;
    
this->m_pMainWnd->ShowWindow(this->m_nCmdShow);
    
this->m_pMainWnd->UpdateWindow();
    
return TRUE;
}


BEGIN_MESSAGE_MAP(CMainWindow,CFrameWnd)
    ON_WM_PAINT()
    ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()

CMainWindow::CMainWindow()
{
    Create(NULL,_T(
"The MFC Application"));
}


void  CMainWindow::OnPaint()
{
    CPaintDC dc(
this);
    CRect rect;
    GetClientRect(
&rect);
    dc.DrawText(
"Weclome to VC!!",-1,&rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);

}


void  CMainWindow::OnLButtonDown(UINT nFlags,CPoint point)
{
    CClientDC dc(
this);
    CRect rect(point.x
-50,point.y-50,point.x+50,point.y+50);
    dc.Ellipse(rect);
}

你可能感兴趣的:(vc++)