視窗編程__小樣本(2)

來源:互聯網
上載者:User

 

 

// WndEx2Dlg.cpp : implementation file<br />//<br />#include "stdafx.h"<br />#include "WndEx2.h"<br />#include "WndEx2Dlg.h"<br />#ifdef _DEBUG<br />#define new DEBUG_NEW<br />#undef THIS_FILE<br />static char THIS_FILE[] = __FILE__;<br />#endif<br />/////////////////////////////////////////////////////////////////////////////<br />// CAboutDlg dialog used for App About<br />class CAboutDlg : public CDialog<br />{<br />public:<br />CAboutDlg();<br />// Dialog Data<br />//{{AFX_DATA(CAboutDlg)<br />enum { IDD = IDD_ABOUTBOX };<br />//}}AFX_DATA<br />// ClassWizard generated virtual function overrides<br />//{{AFX_VIRTUAL(CAboutDlg)<br />protected:<br />virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support<br />//}}AFX_VIRTUAL<br />// Implementation<br />protected:<br />//{{AFX_MSG(CAboutDlg)<br />//}}AFX_MSG<br />DECLARE_MESSAGE_MAP()<br />};<br />CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)<br />{<br />//{{AFX_DATA_INIT(CAboutDlg)<br />//}}AFX_DATA_INIT<br />}<br />void CAboutDlg::DoDataExchange(CDataExchange* pDX)<br />{<br />CDialog::DoDataExchange(pDX);<br />//{{AFX_DATA_MAP(CAboutDlg)<br />//}}AFX_DATA_MAP<br />}<br />BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)<br />//{{AFX_MSG_MAP(CAboutDlg)<br />// No message handlers<br />//}}AFX_MSG_MAP<br />END_MESSAGE_MAP()<br />/////////////////////////////////////////////////////////////////////////////<br />// CWndEx2Dlg dialog<br />CWndEx2Dlg::CWndEx2Dlg(CWnd* pParent /*=NULL*/)<br />: CDialog(CWndEx2Dlg::IDD, pParent)<br />{<br />//{{AFX_DATA_INIT(CWndEx2Dlg)<br />// NOTE: the ClassWizard will add member initialization here<br />//}}AFX_DATA_INIT<br />// Note that LoadIcon does not require a subsequent DestroyIcon in Win32<br />m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);<br />}<br />void CWndEx2Dlg::DoDataExchange(CDataExchange* pDX)<br />{<br />CDialog::DoDataExchange(pDX);<br />//{{AFX_DATA_MAP(CWndEx2Dlg)<br />// NOTE: the ClassWizard will add DDX and DDV calls here<br />//}}AFX_DATA_MAP<br />}<br />BEGIN_MESSAGE_MAP(CWndEx2Dlg, CDialog)<br />//{{AFX_MSG_MAP(CWndEx2Dlg)<br />ON_WM_SYSCOMMAND()<br />ON_WM_PAINT()<br />ON_WM_QUERYDRAGICON()<br />ON_BN_CLICKED(IDC_BTN_RECT, OnBtnRect)<br />ON_BN_CLICKED(IDC_BTN_ELL, OnBtnEll)<br />ON_BN_CLICKED(IDC_BTN_POLYG, OnBtnPolyg)<br />ON_BN_CLICKED(IDC_BTN_ROUND, OnBtnRound)<br />ON_BN_CLICKED(IDC_BTN_TRANS, OnBtnTrans)<br />ON_BN_CLICKED(IDC_BTN_TRANS2, OnBtnTrans2)<br />ON_BN_CLICKED(IDC_BTN_FLASH, OnBtnFlash)<br />ON_BN_CLICKED(IDC_BTN_STOPF, OnBtnStopf)<br />ON_WM_TIMER()<br />ON_BN_CLICKED(IDC_BTN_PICTURE, OnBtnPicture)<br />ON_BN_CLICKED(IDC_BTN_ANIMATE, OnBtnAnimate)<br />ON_WM_ERASEBKGND()<br />//}}AFX_MSG_MAP<br />END_MESSAGE_MAP()<br />/////////////////////////////////////////////////////////////////////////////<br />// CWndEx2Dlg message handlers<br />BOOL CWndEx2Dlg::OnInitDialog()<br />{<br />CDialog::OnInitDialog();<br />// Add "About..." menu item to system menu.<br />// IDM_ABOUTBOX must be in the system command range.<br />ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);<br />ASSERT(IDM_ABOUTBOX < 0xF000);<br />CMenu* pSysMenu = GetSystemMenu(FALSE);<br />if (pSysMenu != NULL)<br />{<br />CString strAboutMenu;<br />strAboutMenu.LoadString(IDS_ABOUTBOX);<br />if (!strAboutMenu.IsEmpty())<br />{<br />pSysMenu->AppendMenu(MF_SEPARATOR);<br />pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);<br />}<br />}<br />// Set the icon for this dialog. The framework does this automatically<br />// when the application's main window is not a dialog<br />SetIcon(m_hIcon, TRUE);// Set big icon<br />SetIcon(m_hIcon, FALSE);// Set small icon</p><p>// TODO: Add extra initialization here<br />CenterWindow();<br />AnimateWindow(GetSafeHwnd(),500,AW_BLEND);//淡入</p><p>return TRUE; // return TRUE unless you set the focus to a control<br />}<br />void CWndEx2Dlg::OnSysCommand(UINT nID, LPARAM lParam)<br />{<br />if ((nID & 0xFFF0) == IDM_ABOUTBOX)<br />{<br />CAboutDlg dlgAbout;<br />dlgAbout.DoModal();<br />}<br />else<br />{<br />CDialog::OnSysCommand(nID, lParam);<br />}<br />}<br />// If you add a minimize button to your dialog, you will need the code below<br />// to draw the icon. For MFC applications using the document/view model,<br />// this is automatically done for you by the framework.<br />void CWndEx2Dlg::OnPaint()<br />{<br />if (IsIconic())<br />{<br />CPaintDC dc(this); // device context for painting<br />SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);<br />// Center icon in client rectangle<br />int cxIcon = GetSystemMetrics(SM_CXICON);<br />int cyIcon = GetSystemMetrics(SM_CYICON);<br />CRect rect;<br />GetClientRect(&rect);<br />int x = (rect.Width() - cxIcon + 1) / 2;<br />int y = (rect.Height() - cyIcon + 1) / 2;<br />// Draw the icon<br />dc.DrawIcon(x, y, m_hIcon);<br />}<br />else<br />{<br />CDialog::OnPaint();<br />}<br />}<br />// The system calls this to obtain the cursor to display while the user drags<br />// the minimized window.<br />HCURSOR CWndEx2Dlg::OnQueryDragIcon()<br />{<br />return (HCURSOR) m_hIcon;<br />}<br />void CWndEx2Dlg::OnBtnRect()<br />{<br />CRect rect;<br />GetClientRect(&rect);<br />//建立矩形地區<br />CRgn rgn;<br />rgn.CreateRectRgn(rect.left,rect.top,rect.right,rect.bottom);<br />//設定視窗的地區<br />SetWindowRgn((HRGN)rgn,TRUE);</p><p>}<br />void CWndEx2Dlg::OnBtnEll()<br />{<br />CRect rect;<br />GetClientRect(&rect);<br />//建立橢圓形地區<br />CRgn rgn;<br />rgn.CreateEllipticRgn(0,0,rect.Width(),rect.Height());<br />//設定地區<br />SetWindowRgn((HRGN)rgn,TRUE);</p><p>}<br />void CWndEx2Dlg::OnBtnPolyg()<br />{<br />CRect rect;<br />GetClientRect(rect);<br />//常見多邊形地區<br />CPoint point[6]; //六邊形的六個點<br />point[0].x=0;<br />point[0].y=rect.Height()/2;<br />point[1].x=rect.Width()/3;<br />point[1].y=0;<br />point[2].x=2*rect.Width()/3;<br />point[2].y=0;<br />point[3].x=rect.Width();<br />point[3].y=rect.Height()/2;<br />point[4].x=2*rect.Width()/3;<br />point[4].y=rect.Height();<br />point[5].x=rect.Width()/3;<br />point[5].y=rect.Height();<br />CRgn rgn;<br />rgn.CreatePolygonRgn(point,6,ALTERNATE);<br />//設定視窗 的地區<br />SetWindowRgn((HRGN)rgn,TRUE);</p><p>}<br />void CWndEx2Dlg::OnBtnRound()<br />{<br />CRect rect;<br />GetClientRect(rect);<br />//建立圓形地區<br />CRgn rgn;<br />rgn.CreateRoundRectRgn(0,0,rect.Width(),rect.Height(),rect.Width()/2,rect.Height()/2);<br />//設定視窗地區<br />SetWindowRgn((HRGN)rgn,TRUE);<br />}<br />void CWndEx2Dlg::OnBtnTrans()<br />{<br />CRect rect1;<br />GetWindowRect(rect1);<br />CRect rect2;<br />GetWindowRect(rect2);<br />ClientToScreen(rect2);</p><p>CRgn rgn1;<br />rgn1.CreateRectRgn(rect1.left,rect2.top,rect1.right,rect1.bottom);</p><p>CRgn rgn2;<br />rgn2.CreateRectRgn(rect2.left,rect2.top,rect2.right,rect2.bottom);</p><p>CRgn rgn;<br />rgn.CreateRectRgn(0,0,1,1);<br />rgn.CombineRgn(&rgn1,&rgn2,RGN_DIFF);<br />//設定視窗地區<br />SetWindowRgn((HRGN)rgn,TRUE);</p><p>}<br />void CWndEx2Dlg::OnBtnTrans2()<br />{<br />//添加WS_EX_LAYERED(0X80000)擴充風格<br />ModifyStyleEx(0,0x80000);<br />//載入User32.dll 動態連結程式庫<br />HMODULE hModule=LoadLibrary("User32.DLL");<br />if (hModule!=NULL)<br />{<br />typedef BOOL(WINAPI*FUNC)(HWND,COLORREF,BYTE,DWORD);<br />//獲得SetLayeredWindowAttributes函數指標<br />FUNC func=(FUNC)GetProcAddress(hModule,"SetLayeredWindowAttributes");<br />if (func!=NULL)<br />{<br />func(GetSafeHwnd(),0,50,2);//具體參看SetLayeredWindowAttributes 函數<br />}<br />FreeLibrary(hModule);<br />}</p><p>}<br />void CWndEx2Dlg::OnBtnFlash()<br />{<br />//設定定時器<br />SetTimer(1,100,NULL);</p><p>}<br />void CWndEx2Dlg::OnBtnStopf()<br />{<br />//關閉定時器<br />KillTimer(1);<br />//視窗返回原始的狀態<br />FlashWindow(FALSE);</p><p>}<br />void CWndEx2Dlg::OnTimer(UINT nIDEvent)<br />{<br />if (nIDEvent==1)<br />{<br />//視窗從一種狀態閃爍到另一種狀態<br />FlashWindow(TRUE);<br />}<br />CDialog::OnTimer(nIDEvent);<br />}<br />void CWndEx2Dlg::OnBtnPicture()<br />{<br />CRect rect;<br />GetWindowRect(&rect);<br />//載入位元影像<br />CBitmap bmp;<br />bmp.LoadBitmap(IDB_BITMAP);<br />//將位元影像選入裝置環境<br />CDC dc;<br />CDC* pDC=GetDC();<br />dc.CreateCompatibleDC(pDC);<br />dc.SelectObject(&bmp);<br />//將位元影像中的黑色地區變成透明<br />CRgn rgn;<br />rgn.CreateRectRgn(0,0,rect.Width(),rect.Height());<br />for (int x=0;x<rect.Height();x++)<br />{<br />for (int y=0;y<rect.Width();y++)<br />{<br />COLORREF cr=dc.GetPixel(x,y);<br />if (cr==RGB(0,0,0))<br />{<br />CRgn rgn2;<br />rgn2.CreateRectRgn(x,y,x+1,y+1);<br />rgn.CombineRgn(&rgn,&rgn2,RGN_XOR);//XOR 結合倆個地區,去掉重疊的部分<br />}<br />}<br />}<br />//設定視窗地區<br />SetWindowRgn((HRGN)rgn,TRUE);<br />ReleaseDC(pDC);</p><p>}<br />void CWndEx2Dlg::OnBtnAnimate()<br />{<br />//CenterWindow();<br />//AnimateWindow(GetSafeHwnd(),100,AW_BLEND);</p><p>}<br />BOOL CWndEx2Dlg::OnEraseBkgnd(CDC* pDC)<br />{<br />CRect rect;<br />GetWindowRect(&rect);<br />CBitmap bmp;<br />bmp.LoadBitmap(IDB_BITMAP);<br />CDC dc;<br />dc.CreateCompatibleDC(pDC);<br />dc.SelectObject(&bmp);<br />pDC->BitBlt(0,0,rect.Width(),rect.Height(),&dc,0,0,SRCCOPY);<br />return TRUE;<br />} 

 

圖片視窗操作中,需要自己添加訊息響應

afx_msg BOOL OnEraseBkgnd(CDC* pDC);

 

ON_WM_ERASEBKGND()

 

:http://ishare.iask.sina.com.cn/f/14464563.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.