1. 程式人生 > 實用技巧 >視窗樣式

視窗樣式

新: 作品簡介: 本文演示了通過在顯示的表單檢視中選擇各種選項,在執行時建立不同樣式視窗的技術 特點: 您可以建立以下型別的視窗 重疊 孩子 彈出 標準對話方塊 您還可以選擇建立選單 視窗也可以建立 最小化 Maxmiized 規模 視窗可以有以下按鈕 最大化 最小化 系統選單 你可以用預設的大小和預設的位置建立視窗,或者指定左邊,頂部的值,以及指定的寬度和高度 注意: 如果您繼續單擊“建立視窗”按鈕,視窗將建立一個在另一個多次,您繼續單擊,並將不得不關閉每個視窗,通過單擊關閉按鈕多次 關於程式碼: 隱藏,收縮,複製Code

//
//  Depending on the Type of Window to be created
//  we enable of disble various options as below
//
//  If you select the Overlapped Window option
//  the following checkboxes are Enabled / Disabled
void CMainForm::SetOverlappedProperties()
{
	GetDlgItem(IDC_DIALOGFRAME)->EnableWindow(FALSE);
	GetDlgItem(IDC_MINIMIZE_BOX)->EnableWindow(TRUE);
	GetDlgItem(IDC_MAXIMIZE_BOX)->EnableWindow(TRUE);
	GetDlgItem(IDC_SYSTEM_MENU)->EnableWindow(TRUE);
	GetDlgItem(IDC_DEFAULT_TOPLEFT)->EnableWindow(TRUE);
	GetDlgItem(IDC_DEFAULT_WIDTHHEIGHT)->EnableWindow(TRUE);
	GetDlgItem(IDC_TOP)->EnableWindow(FALSE);
	GetDlgItem(IDC_LEFT)->EnableWindow(FALSE);
	GetDlgItem(IDC_WIDTH)->EnableWindow(FALSE);
	GetDlgItem(IDC_HEIGHT)->EnableWindow(FALSE);
	m_DefaultTopLeft = m_DefaultWidthHeight = TRUE;
	m_MinimizeBox = m_MaximizeBox = m_SystemMenu = 
					m_Overlapped = TRUE;
	m_ToSize = TRUE;
	m_Popup = m_Child = FALSE;

	UpdateData(FALSE);	
}

//
//  If you select the Child Window option
//  the following checkboxes are Enabled / Disabled
//

void CMainForm::SetChildProperties()
{
	m_MinimizeBox = m_MaximizeBox = m_SystemMenu = m_Overlapped = 
					m_Popup = FALSE;
	m_ToSize = TRUE;
	m_Child = TRUE;
	GetDlgItem(IDC_DIALOGFRAME)->EnableWindow(TRUE);

	GetDlgItem(IDC_MINIMIZE_BOX)->EnableWindow(FALSE);
	GetDlgItem(IDC_MAXIMIZE_BOX)->EnableWindow(FALSE);
	GetDlgItem(IDC_SYSTEM_MENU)->EnableWindow(FALSE);

	GetDlgItem(IDC_CAPTION)->EnableWindow(TRUE);
	m_Caption = FALSE;

	UpdateData(FALSE);
		
}

//
//  If you select the Popup Window option
//  the following checkboxes are Enabled / Disabled
//

void CMainForm::SetPopupProperties()
{
	m_MinimizeBox = m_MaximizeBox = m_SystemMenu = 
		m_Overlapped = m_Child = FALSE;
	m_Popup = TRUE;

	m_ToSize = TRUE;
	m_SystemMenu = TRUE;
	GetDlgItem(IDC_MINIMIZE_BOX)->EnableWindow(TRUE);
	GetDlgItem(IDC_MAXIMIZE_BOX)->EnableWindow(TRUE);
	GetDlgItem(IDC_SYSTEM_MENU)->EnableWindow(TRUE);

	GetDlgItem(IDC_CAPTION)->EnableWindow(TRUE);
	m_Caption = FALSE;
	UpdateData(FALSE);		
}

//
//  If you select the Standard Window option
//  the following checkboxes are Enabled / Disabled
//
void CMainForm::SetStandardDialogProperties()
{
	CheckDlgButton(IDC_CAPTION, 1);
	GetDlgItem(IDC_CAPTION)->EnableWindow(TRUE);
	CheckDlgButton(IDC_SYSTEM_MENU,1);
	GetDlgItem(IDC_SYSTEM_MENU)->EnableWindow(TRUE);

	GetDlgItem(IDC_MINIMIZE_BOX)->EnableWindow(TRUE);
	GetDlgItem(IDC_MAXIMIZE_BOX)->EnableWindow(TRUE);
	GetDlgItem(IDC_SYSTEM_MENU)->EnableWindow(TRUE);
	
	CheckDlgButton(IDC_MAXIMIZE_BOX, 0);
	CheckDlgButton(IDC_MINIMIZE_BOX, 0);
	CheckDlgButton(IDC_MODAL_DIALOG_FRAME, 1);
	GetDlgItem(IDC_DEFAULT_WIDTHHEIGHT)->EnableWindow(FALSE);
	GetDlgItem(IDC_DEFAULT_TOPLEFT)->EnableWindow(FALSE);
	GetDlgItem(IDC_WIDTH)->EnableWindow(TRUE);
	GetDlgItem(IDC_HEIGHT)->EnableWindow(TRUE);
	GetDlgItem(IDC_TOP)->EnableWindow(TRUE);
	GetDlgItem(IDC_LEFT)->EnableWindow(TRUE);

	UpdateData(TRUE);
}

讓我們看看在您選擇了必要的選項並單擊“建立視窗”按鈕之後,在執行時建立視窗的程式碼 隱藏,收縮,複製Code

//
//  First of all we have to register the class 
//  the name of the Class is specified as a String
//  Name of the class is passed to the my member 
//  function eg RegisterClass("MyWindowStyles")
//
//
BOOL CMainForm::RegisterClass(CString stringClass)
{
    WNDCLASS wndclass;

    wndclass.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
    wndclass.lpfnWndProc = WndProc;
    wndclass.cbClsExtra = 0;
    wndclass.cbWndExtra = 0;
    wndclass.hInstance = AfxGetInstanceHandle();
    wndclass.hIcon = LoadIcon(NULL, _T(""));
    wndclass.hCursor = LoadCursor(NULL,  IDC_ARROW );
    wndclass.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
    wndclass.lpszMenuName = NULL;
    wndclass.lpszClassName = stringClass;

    if( !AfxRegisterClass( &wndclass ))
      {
        AfxMessageBox("Error Registering Class\n");
        return FALSE;
      }

	return TRUE;
}

//
//   Since the Window created at run-time cannot have its 
//   Messages in the Message Map of the Application , we
//   have the following procedure , which takes care
//   of the messages in the newly created Window
//
//   Note : WndProc is passed as the Windows procedure
//          to the above Registered Class as 
//
//          wndclass.lpfnWndProc = WndProc;
//
//   This procedure allows us to Destroy the Created
//   Window which would otherwise not be possible

LRESULT FAR PASCAL WndProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
	if(Message == WM_COMMAND)
	{
		DestroyWindow(hWnd);
	}

	return (DefWindowProc(hWnd, Message, wParam, lParam)); 
}
//
// If the Class was registered successfully
// we proceed with creating the Window
// With the CreateWindowEx function as below
//
//  The following variables passed to CreateWindowEx
//  contain information as selected on the Formview
//  dExtendedStyle  = Extended Window Styles
//  m_EditCaption   = Window Caption
//  dStyle          = Style of The Window
//  x               = Left Position of the Window
//  y               = Top Position of the WIndow
//  nWidth          = Width of the Window
//  nHeight         = Height of the Window
//  hMenu           = Menu (if Checked)
//                    (Menu is created in the Resources)
//

 
hWndMain = CreateWindowEx(
                dExtendedStyle,
                szClassName,           
                m_EditCaption,         
                dStyle,
                x,                     
                y,
                nWidth,                
                nHeight,                  
                this->m_hWnd,  
                hMenu,                    
                AfxGetInstanceHandle(), 
                NULL);                  

 if(!hWndMain)
 {
	AfxMessageBox("Error Creating Window");
	return;
 }

//
//
//  We create a button on the newly 
//  created Window , to enable us to close
//  the Window in case the created Window 
//  did not have the Menu option enabled or
//  Window cannot have a Menu as a Standard
//
//

HWND hwndBtn = CreateWindow(_T("BUTTON"),
                            _T("&Close"), 
                            WS_VISIBLE|
                            WS_CHILD|
                            BS_DEFPUSHBUTTON, 
                            30,20,90,30,
                            hWndMain,
                            (HMENU)ID_CLOSE_BUTTON, 
                            NULL,
                            NULL); 

//
//  Finally we Display the Window as shown below
//

 ::ShowWindow(hWndMain, SW_SHOW);
</code>

視窗快樂! ! 本文轉載於:http://www.diyabc.com/frontweb/news8341.html