www.pudn.com > my2cadtest07261930.rar > SetPenAttr.cpp, change:2006-07-26,size:8144b


// SetPenAttr.cpp: implementation of the CSetPenAttr class. 
// 
////////////////////////////////////////////////////////////////////// 
 
#include "stdafx.h" 
#include "SetPenAttr.h" 
#include "commctrl.h" 
#pragma comment(lib, "comctl32.lib") 
 
////////////////////////////////////////////////////////////////////// 
// Construction/Destruction 
////////////////////////////////////////////////////////////////////// 
 
 
extern CSetPenAttr * penattrib ; 
 
 
CSetPenAttr::CSetPenAttr() 
{ 
	m_bgcolor=RGB(255,255,255); 
 
	//初始化划笔 
	m_penstruct.lopnColor=RGB(255,0,0); 
    m_penstruct.lopnStyle=PS_SOLID; 
    m_penstruct.lopnWidth.x=2; 
    m_penstruct.lopnWidth.y=2; 
 
    m_brushstruct.lbColor=RGB(255,255,255);  
    m_brushstruct.lbHatch=HS_VERTICAL ; 
    m_brushstruct.lbStyle=BS_HOLLOW  ; 
 
 
	m_hWnd = NULL;	 
	for(int i = 0; i < 8; i++) 
	{ 
		m_cCustomColors[i] = RGB(255, 255, 255); 
	} 
	 
	m_cDlgStruct.lStructSize = sizeof(CHOOSECOLOR); 
	m_cDlgStruct.hwndOwner = NULL; 
	m_cDlgStruct.hInstance = NULL; 
	m_cDlgStruct.lpTemplateName = NULL; 
	m_cDlgStruct.rgbResult = m_penstruct.lopnColor; 
	m_cDlgStruct.lpCustColors = m_cCustomColors; 
	m_cDlgStruct.Flags = CC_ANYCOLOR | CC_RGBINIT; 
	m_cDlgStruct.lCustData = 0; 
	m_cDlgStruct.lpfnHook = NULL; 
 
    m_cDlgStruct1.lStructSize = sizeof(CHOOSECOLOR); 
	m_cDlgStruct1.hwndOwner = NULL; 
	m_cDlgStruct1.hInstance = NULL; 
	m_cDlgStruct1.lpTemplateName = NULL; 
	m_cDlgStruct1.rgbResult = m_bgcolor; 
	m_cDlgStruct1.lpCustColors = m_cCustomColors; 
	m_cDlgStruct1.Flags = CC_ANYCOLOR | CC_RGBINIT; 
	m_cDlgStruct1.lCustData = 0; 
	m_cDlgStruct1.lpfnHook = NULL; 
 
 
 
 
} 
 
 
CSetPenAttr::~CSetPenAttr() 
{ 
 
} 
 
CSetPenAttr::PenAttribDlg(HINSTANCE &hInst,HWND &hWnd) 
{ 
    int nResult; 
	nResult = ::DialogBox(hInst, (LPCTSTR)IDD_PENSET, hWnd, (DLGPROC)CSetPenAttr::PenDlgProc);	 
	return nResult; 
	 
} 
 
bool CALLBACK CSetPenAttr::PenDlgProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) 
{ 
	int nIndex; 
	char szText[50]="";  
 
   
 
  	switch(message) 
	{ 
	case WM_INITDIALOG:                    //对话框初使化	    
	   HWND hOnOk; 
	   hOnOk=::GetDlgItem(hwnd,IDOK); 
	   ::SetFocus(hOnOk); 
 
		penattrib->OnInitDialog(hwnd, lParam); 
		break; 
	case WM_COMMAND: 
		switch(LOWORD(wParam)) 
		{ 
		 case IDC_CBOPENSTYLE: 
            { 
             
            	if (HIWORD(wParam) == CBN_SELCHANGE)//如果选择 
					{ 
						nIndex = ::SendMessage(penattrib->m_hCBO, CB_GETCURSEL, 0, 0);//获得选中项序号 
						::SendMessage(penattrib->m_hCBO, CB_GETLBTEXT,nIndex,(LPARAM)szText);//获得选中项的文本 
					//	::MessageBox(penattrib->m_hCBO,szText,"selected",MB_OK); 
						switch (nIndex) 
						{ 
						case 0: 
							{								 
								penattrib->m_penstruct.lopnStyle = PS_DASH;	 
							} 
							break; 
						case 1: 
							{ 
								penattrib->m_penstruct.lopnStyle = PS_DASHDOT; 
							} 
							break; 
						case 2: 
							{ 
								penattrib->m_penstruct.lopnStyle = PS_DOT;							 
							} 
							break; 
						case 3: 
							{ 
								penattrib->m_penstruct.lopnStyle = PS_SOLID; 
							} 
							break; 
						} 
					} 
				}			 
			break; 
			 
		case IDOK: 
			penattrib->OnOk(); 
           ::EndDialog(hwnd,LOWORD(wParam)); 
			return true; 
			break; 
		case IDCANCEL: 
			::EndDialog(hwnd,LOWORD(wParam)); 
			return true; 
			break; 
		case IDC_PENCOLORSET: 
			penattrib->GetPenColor(hwnd); 
			break; 
        case IDC_BGCOLORSET: 
			penattrib->GetBgColor(hwnd); 
			break; 
//		case IDC_COLORVIEW: 
//			::MessageBox(hwnd,"123","selected",MB_OK); 
//			if(STN_DBLCLK == HIWORD(wParam)) 
//			penattrib->GetPenColor(hwnd); 
//			break; 
		case IDC_RADIO1: 
			penattrib->m_brushstruct.lbStyle=BS_HOLLOW; 
			//MessageBox(hwnd,"123","123",MB_OK); 
			break; 
        case IDC_RADIO2: 
			penattrib->m_brushstruct.lbStyle=BS_SOLID; 
			//MessageBox(hwnd,"123","123",MB_OK); 
			break;         
		} 
		break; 
	case WM_CTLCOLORSTATIC: 
		penattrib->OnCtlColorStatic(wParam, lParam);		 
		break; 
	case WM_CLOSE: 
		::EndDialog(hwnd, LOWORD(wParam)); 
		return true; 
		break; 
	} 
	 
	return 0; 
	 
} 
 
void CSetPenAttr::OnInitDialog(HWND &hwnd, LPARAM &lParam) 
{ 
     
	char buffer[10]=""; 
    char szText[30]=""; 
	m_hWnd = hwnd; 
	m_hPenWidth = ::GetDlgItem(m_hWnd, IDC_EDITLINEWIDTH); 
	m_hBgColor = ::GetDlgItem(m_hWnd, IDC_BGCOLORVIEW); 
	m_hPenColor = ::GetDlgItem(m_hWnd,IDC_COLORVIEW); 
    m_hCBO = ::GetDlgItem(m_hWnd,IDC_CBOPENSTYLE); 
	 
	 
    sprintf(buffer,"%s","请选择线型"); 
	::SetWindowText(m_hCBO,buffer); 
 
	//添加画笔宽度数直 
    sprintf(buffer,"%d",m_penstruct.lopnWidth.x); 
	::SetWindowText(m_hPenWidth,buffer); 
	 
	//添加下拉框项目			  
	::strcpy(szText,"PS_SOLID        实线"); 
	::SendMessage(m_hCBO,CB_ADDSTRING,0,(LPARAM)szText); 
 
	::strcpy(szText,"PS_DOT           虚线"); 
	::SendMessage(m_hCBO,CB_ADDSTRING,0,(LPARAM)szText); 
 
	::strcpy(szText,"PS_DASHDOT  点划线"); 
	::SendMessage(m_hCBO,CB_ADDSTRING,0,(LPARAM)szText); 
	 
	::strcpy(szText,"PS_DASH         划线"); 
	::SendMessage(m_hCBO,CB_ADDSTRING,0,(LPARAM)szText); 
 
 
 
} 
 
void CSetPenAttr::OnOk() 
{ 
	  char buffer[10]=""; 
      ::GetWindowText( m_hPenWidth,buffer,10);				 
	   sscanf(buffer,"%d",&(penattrib->m_penstruct.lopnWidth.x)); 
	   							    
} 
 
void CSetPenAttr::OnCtlColorStatic(WPARAM &wParam, LPARAM &lParam) 
{ 
	HBRUSH m_Brush; 
	HDC hdc; 
	if((HWND)lParam == m_hPenColor) 
	{ 
        m_penstruct.lopnColor= m_cDlgStruct.rgbResult; 
		m_Brush = ::CreateSolidBrush(m_cDlgStruct.rgbResult); 
         
		hdc = (HDC)wParam; 
		::SelectObject(hdc, m_Brush); 
		::Rectangle(hdc, 0, 0, 200, 200); 
	} 
  
   if((HWND)lParam == m_hBgColor) 
	{ 
        m_bgcolor= m_cDlgStruct1.rgbResult; 
		m_Brush = ::CreateSolidBrush(m_cDlgStruct1.rgbResult);         
		hdc = (HDC)wParam; 
		::SelectObject(hdc, m_Brush); 
		::Rectangle(hdc, 0, 0, 200, 200); 
	} 
 
	::DeleteObject(m_Brush); 
	::ReleaseDC((HWND)lParam,hdc); 
} 
 
 
bool CSetPenAttr::GetPenColor(HWND &hwnd) 
{ 
  	m_cDlgStruct.hwndOwner = hwnd; 
	if(::ChooseColor(&m_cDlgStruct)) 
	{ 
		m_penstruct.lopnColor= m_cDlgStruct.rgbResult; 
		InvalidateRect(m_hPenColor, NULL, 1);			 
		return true; 
	} 
	else 
	{ 
		return false; 
	} 
	 
} 
 
bool CSetPenAttr::GetBgColor(HWND &hwnd) 
{ 
  	m_cDlgStruct1.hwndOwner = hwnd; 
	if(::ChooseColor(&m_cDlgStruct1)) 
	{	 
		m_bgcolor= m_cDlgStruct1.rgbResult;	 
		InvalidateRect(m_hBgColor, NULL, 1); 
		InvalidateRect(g_hwnd,NULL,true); 
		return true; 
	} 
	else 
	{ 
		return false; 
	} 
	 
} 
 
 
void CSetPenAttr::GetPen(HDC &hdc,bool whatpen,bool isredrawpen) 
{ 
   
	 if(whatpen) 
		  {   
 
			m_orpen = CreatePen(PS_SOLID,m_penstruct.lopnWidth.x,RGB(63,0,0)); 
			SelectObject(hdc,m_orpen); 
			SelectObject(hdc,(HBRUSH)GetStockObject(NULL_BRUSH)); 
			SetROP2(hdc,R2_XORPEN); 
		  } 
	else  
		  {	   
			  if(isredrawpen) 
				  {	 
				    m_redrawPen = CreatePenIndirect(&m_redraw_penstruct); 
					SelectObject(hdc,m_redrawPen);  
				//	GetBrush(HDC &hdc) 
					SelectObject(hdc,(HBRUSH)GetStockObject(NULL_BRUSH)); 
					SetROP2(hdc,R2_COPYPEN); 
				 
				  } 
			  else 
				  { 
					m_pen = CreatePenIndirect(&m_penstruct); 
					SelectObject(hdc,m_pen);  
				//	GetBrush(HDC &hdc) 
					SelectObject(hdc,(HBRUSH)GetStockObject(NULL_BRUSH)); 
					SetROP2(hdc,R2_COPYPEN);		   
				  } 
 
		  }	    
} 
 
void CSetPenAttr::ReleasePen(bool whatpen,bool isredrawpen) 
{ 
	if(whatpen) 
	{ 
      ::DeleteObject(m_orpen); 
	} 
	else 
	{ 
		  if(isredrawpen) 
		  { 
 
            ::DeleteObject(m_redrawPen); 
		     
		  } 
		  else 
		  { 
		     ::DeleteObject(m_pen); 
		  } 
	} 
	 
} 
 
void CSetPenAttr::ReleaseBrush(bool isredrawpen) 
{ 
   if(isredrawpen) 
   { 
    ::DeleteObject(m_redrawBrush); 
   } 
	else 
   { 
    ::DeleteObject(m_brush); 
   } 
	 
} 
 
void CSetPenAttr::GetBrush(HDC &hdc,bool isredrawpen) 
{ 
    if(isredrawpen) 
	{ 
	    m_redrawBrush=CreateBrushIndirect(&m_redraw_brushstruct); 
//      SelectObject(hdc,m_brush); 
	}	 
    else 
	{ 
    	m_brush=CreateBrushIndirect(&m_brushstruct); 
//      SelectObject(hdc,m_brush); 
 
	} 
 
}