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);
}

posted on   Phinecos(洞庭散人)  阅读(1186)  评论(0编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述

导航

统计

点击右上角即可分享
微信分享提示