20160301 第1章 您的第一个MFC应用程序(来自《MFC Windows 程序设计(第2版)》)

来源:互联网 发布:下载农村淘宝免费下载 编辑:程序博客网 时间:2024/05/21 11:27



1.1.2 Windows 程序设计,SDK风格

#include <tchar.h>#include <Windows.h>LONG WINAPI WndProc(HWND, UINT, WPARAM, LPARAM);int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpszCmdLine, int nCmdShow){WNDCLASS wc;HWND hwnd;MSG msg;//初始化窗口类wc.style = 0;wc.lpfnWndProc = (WNDPROC)WndProc;wc.cbClsExtra = 0;wc.cbWndExtra = 0;wc.hInstance = hInstance;wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);wc.hCursor = LoadCursor(NULL, IDC_ARROW);wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);wc.lpszMenuName = NULL;wc.lpszClassName = _T("MyWndClass");//注册窗口类RegisterClass(&wc);//创建窗口hwnd = CreateWindow(_T("MyWndClass"),_T("SDK Application"),WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,HWND_DESKTOP,NULL,hInstance,NULL);//显示和更新窗口ShowWindow(hwnd, nCmdShow);UpdateWindow(hwnd);//消息循环while (GetMessage(&msg,NULL,0,0)){TranslateMessage(&msg);DispatchMessage(&msg);}return msg.wParam;}LRESULT CALLBACK WndProc(HWND hwnd, UINT message,WPARAM wParam, LPARAM lParam){PAINTSTRUCT ps;HDC hdc;switch (message){case WM_PAINT:hdc = BeginPaint(hwnd, &ps);Ellipse(hdc, 0, 0, 200, 100);EndPaint(hwnd, &ps);return 0;case WM_DESTROY:PostQuitMessage(0);return 0;}return DefWindowProc(hwnd, message, wParam, lParam);}


1.3 您的第一个MFC应用程序(第一步新建Win32应用程序)
Hello.h
#ifndef __HELLO_HH#define __HELLO_HH#include <afxwin.h>class CMyApp :public CWinApp{public:virtual BOOL InitInstance();};class CMainWindow:public CFrameWnd{public:CMainWindow();protected:afx_msg void OnPaint();DECLARE_MESSAGE_MAP()};#endif
Hello.cpp
#include "Hello.h"CMyApp myApp;BOOL CMyApp::InitInstance(){m_pMainWnd = new CMainWindow();m_pMainWnd->ShowWindow(m_nCmdShow);//m_nCmdShow也可以用SW_SHOWNORMAL代替;WS_VISIBLE也可以使窗口可见m_pMainWnd->UpdateWindow();//对应全局函数::UpdateWindow(hwnd);return TRUE;}BEGIN_MESSAGE_MAP(CMainWindow,CFrameWnd)//标识了消息映射所属的类是CMainWindowON_WM_PAINT()END_MESSAGE_MAP()CMainWindow::CMainWindow(){Create(NULL, _T("The Hello Application"));}void CMainWindow::OnPaint(){//CPaintDC dc(this);//CRect rect;//GetClientRect(&rect);//dc.SetBkMode(TRANSPARENT);//设置背景色为透明色//dc.DrawText(_T("Hello, MFC"), -1, &rect,//第二个参数是字符串中的字符数,-1代表字符串是以NULL字符终止的//DT_SINGLELINE | DT_VCENTER | DT_CENTER);//阴影字体效果/*CRect rect;GetClientRect(&rect);CFont font;font.CreatePointFont(720, _T("Arial"));CPaintDC dc(this);dc.SelectObject(&font);dc.SetBkMode(TRANSPARENT);CString string = _T("Hello, MFC");rect.OffsetRect(16, 16);dc.SetTextColor(RGB(192, 192, 192));dc.DrawText(string, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);rect.OffsetRect(-16, -16);dc.SetTextColor(RGB(0, 0, 0));dc.DrawText(string, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);*///旋转CRect rect;GetClientRect(&rect);CPaintDC dc(this);dc.SetViewportOrg(rect.Width() / 2, rect.Height() / 2);dc.SetBkMode(TRANSPARENT);for (int i = 0; i < 3600; i += 150){LOGFONT lf;::ZeroMemory(&lf, sizeof(lf));lf.lfHeight = 160;lf.lfWeight = FW_BOLD;lf.lfEscapement = i;lf.lfOrientation = i;::lstrcpy(lf.lfFaceName, _T("Arial"));CFont font; font.CreatePointFontIndirect(&lf);CFont *pOldFont = dc.SelectObject(&font);dc.TextOut(0, 0, CString(_T("Hello, MFC")));dc.SelectObject(pOldFont);}}



1 0
原创粉丝点击