第一个VC++ win32程序 绘制简单图形

创建一个VC++ win32 

打开VS 新工程类型中选择Win32----Win32 Project  自己取个名字(假如叫做My1stWin) 一路next

系统会自动生成好最基本的代码  然后我们就在上面改就可以了 

发现编译不通过? 不要使用Ctrl+F7 来编译   而应该使用Ctrl+Alt+F7  

下面是主程序My1stWin.cpp

// My1stWin.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "My1stWin.h"

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst;                                // current instance
TCHAR szTitle[MAX_LOADSTRING];                    // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];            // the main window class name

// Forward declarations of functions included in this code module:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

/* _tWinMain是系统生成的  */
int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

     // TODO: Place code here.
    MSG msg;
    HACCEL hAccelTable;

    // Initialize global strings
    LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadString(hInstance, IDC_MY1STWIN, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // Perform application initialization:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

    hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MY1STWIN));

    // Main message loop:
    while (GetMessage(&msg, NULL, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int) msg.wParam;
}

/*网上找的winMain  更直观 可以设置窗口的属性什么的  二者都可以*/
//int WINAPI WinMain(HINSTANCE hInstance,
//                 HINSTANCE hPrevInstance,
//                 PSTR szCmdLine,
//                 int iCmdShow) {
//    static TCHAR szAppName[] = TEXT("draw");
//    HWND hwnd;
//    MSG msg;
//    WNDCLASS 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.lpszMenuName = NULL;
//    wndclass.lpszClassName = szAppName;
//    if(!RegisterClass(&wndclass)) {
//        MessageBox(NULL, TEXT("Register failure..."),
//            szAppName, MB_ICONERROR);
//        return 0;
//    }
//    hwnd = CreateWindow(szAppName,
//        szAppName,
//        WS_OVERLAPPEDWINDOW,
//        CW_USEDEFAULT,
//        CW_USEDEFAULT,
//        CW_USEDEFAULT,
//        CW_USEDEFAULT,
//        NULL,
//        NULL,
//        hInstance,
//        NULL);
//    ShowWindow(hwnd, iCmdShow);
//    UpdateWindow(hwnd);
//    while(GetMessage(&msg, NULL, 0, 0)) {
//        TranslateMessage(&msg);
//        DispatchMessage(&msg);
//    }
//    return msg.wParam;
//}



//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage are only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEX wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style            = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra        = 0;
    wcex.cbWndExtra        = 0;
    wcex.hInstance        = hInstance;
    wcex.hIcon            = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MY1STWIN));
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground    = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName    = MAKEINTRESOURCE(IDC_MY1STWIN);
    wcex.lpszClassName    = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassEx(&wcex);
}

//
//   FUNCTION: InitInstance(HINSTANCE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND    - process the application menu
//  WM_PAINT    - Paint the main window
//  WM_DESTROY    - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int wmId, wmEvent;
    static int cxClient, cyClient;
    PAINTSTRUCT ps;
    HDC hdc;
    //hWnd是窗口句柄,其中包含窗口的属性。例如,窗口的大小,显示位置,父窗口。
    //hDC(Handle to Device Context)是图像的设备描述表,窗口显示上下文句柄,其中可以进行图形显示。
    //利用hDC=GetDC(hWnd),可以获得一个窗口的图形设备描述表。可以通过ReleaseDC()函数释放。

    //rakus code==================
    HPEN hPen, hPenOld;//定义画笔
    HBRUSH hBrush,HBruOld;//定义画刷
    TEXTMETRIC tm;//用于获取字体信息
    int x=10;
    int y=150;
    //end of rakus code=====================


    switch (message)
    {
    case WM_CREATE:
        return 0;
    case WM_SIZE:
        cxClient = LOWORD(lParam);
        cyClient = HIWORD(lParam);
        return 0;
    case WM_COMMAND:
        wmId    = LOWORD(wParam);
        wmEvent = HIWORD(wParam);
        // Parse the menu selections:
        switch (wmId)
        {
        case IDM_ABOUT:
            DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
            break;
        case IDM_EXIT:
            DestroyWindow(hWnd);
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
        }
        break;
    case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);
        // TODO: Add any drawing code here...

        //this is rakus code
        //划横线
        MoveToEx(hdc, 0, cyClient / 2, NULL);
        LineTo(hdc, cxClient, cyClient / 2);
        // 画竖线
        MoveToEx(hdc, cxClient / 2, 0, NULL);
        LineTo(hdc, cxClient / 2, cyClient);
        // 画椭圆
        Ellipse(hdc, cxClient / 2, 0, cxClient, cyClient / 2);
        // 画圆矩形
        RoundRect(hdc, cxClient / 8, 5 * cyClient / 8, 
            cxClient / 2 - cxClient / 8, cyClient - cyClient / 8,
            cxClient / 16, cyClient / 16);
        // 画弦
        Chord(hdc, cxClient / 2, cyClient / 2, 3 * cxClient / 2, 3* cyClient / 2, 
            cxClient, cyClient / 2, cxClient / 2, cyClient);

        hPen=(HPEN)CreatePen(PS_SOLID,3,RGB(0,50,220));//创建画笔  宽度为3  蓝色
        hBrush=(HBRUSH)CreateHatchBrush(HS_CROSS,RGB(220,50,0));//创建网格线画刷 红色
        //GetClientRect(hWnd,&rect);//rect无法解析 当然了 前面根本没有定义rect 
        Rectangle(hdc,10,10,80,80);//虽然上面创建了自己的画笔  但是这里并没有用自己的画笔
        
        hPenOld=(HPEN)SelectObject(hdc,hPen);//选择新画笔 返回旧画笔的句柄
        HBruOld=(HBRUSH)SelectObject(hdc,hBrush);//选择新画刷  返回旧画刷句柄
        //画刷就是用来画底色的
        Rectangle(hdc,100,10,180,580);
        Ellipse(hdc,250,10,350,80);//画椭圆//这里绘制的两个就是使用新画刷和新画笔  

        SelectObject(hdc,hBrush);//选择默认画笔
        //end of rakus code

        EndPaint(hWnd, &ps);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);
    switch (message)
    {
    case WM_INITDIALOG:
        return (INT_PTR)TRUE;

    case WM_COMMAND:
        if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
        {
            EndDialog(hDlg, LOWORD(wParam));
            return (INT_PTR)TRUE;
        }
        break;
    }
    return (INT_PTR)FALSE;
}

 

posted @ 2013-09-24 11:38  cart55free99  阅读(732)  评论(0编辑  收藏  举报