主框架窗口和视窗的创建
CWinApp::InitInstance里面对主框架窗口进行了初始化,在ProcessShellCommand(cmdInfo)里面调用了
CMainFrame::OnCreate,创建主框架窗口.在CMainFrame::OnCreate里面的CFrameWnd::OnCreate
(lpCreateStruct)里面调用CView的构造函数来构造CView对象,然后会进入到CView::OnInitialUpdate()
来对CView对象的窗口进行初始化,再返回到CMainFrame::OnCreate.
以下代码使得一个菜单项变灰,要实现这个功能,必须先设置属性m_bAutoMenuEnable为FALSE(缺省为TRUE,
表示当菜单项没有消息响应时自动变灰),可以在CMainFrame构造函数里面写:m_bAutoMenuEnable =
FALSE;
CMenu* pAllMenu = AfxGetApp()->GetMainWnd()->GetMenu();
CMenu* pMyMenu = pAllMenu->GetSubMenu(4);
pMyMenu->EnableMenuItem(ID_RECT, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
弹出式(上下文)菜单的弹出应该放在WM_CONTEXTMENU的处理函数里:
void CGfgfView::OnContextMenu(CWnd* pWnd, CPoint point)
{
CMenu MyMenu;
MyMenu.LoadMenu(IDR_POP);
CMenu* pPopMenu = MyMenu.GetSubMenu(0);
pPopMenu->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON,
point.x, point.y, pWnd, 0);
}
CGfgfView* pView = (CGfgfView*)AfxGetApp()->GetMainWnd()->GetActiveWindow();这句得到的是视图
窗口的指针,不是对象的指针,不能操作对象的属性.
HWND hWnd = ((CFrameWnd*)AfxGetApp()->GetMainWnd())->GetActiveView()->m_hWnd;
CGfgfView* pView = (CGfgfView*)FromHandle(hWnd); 这两句得到的是跟窗口关联的对象的指针,可以
操作对象的属性.
在工具栏m_wndMyToolBar上添加一个combobox的代码: //CComboBox m_ComboBox 为成员变量
CRect rect;
m_wndMyToolBar.SetButtonInfo(3,ID_COMBOBOX,TBBS_SEPARATOR,160);
m_wndMyToolBar.GetItemRect(3,&rect);
rect.bottom += 100;
if(!m_ComboBox.Create(CBS_DROPDOWNLIST | WS_VISIBLE |
WS_TABSTOP | CBS_AUTOHSCROLL, rect, &m_wndMyToolBar,
ID_COMBOBOX))
return -1;
m_ComboBox.AddString("First Item");
m_ComboBox.AddString("Second Item");
m_ComboBox.AddString("Third Item");
m_ComboBox.SetCurSel(0);
在主框架的状态栏上显示一行文字的一种方法:
void CEtView::OnMouseMove(UINT nFlags, CPoint point)
{
//HWND hWnd = ((CMainFrame*)AfxGetApp()->GetMainWnd())->m_hWnd;
//CMainFrame* pMainFrame = (CMainFrame*)FromHandle(hWnd);
//pMainFrame->m_wndStatusBar.SetPaneText(1, "dfdfdk");
char x[20], y[10];
sprintf(x, "X:%d ", point.x);
sprintf(y, "Y:%d", point.y);
strcat(x, y);
CMainFrame* pMainFrame = (CMainFrame*)GetParent();
pMainFrame->SetMessageText(x);
CView::OnMouseMove(nFlags, point);
}
创建一个CDialogBar对象的方法: //CDialogBar m_wndMyDialogBar;
#define ID_MYDIALOGBAR 101 //DialogBar对象ID
m_wndMyDialogBar.Create(this/*父窗口指针,如CMainFrame* */, IDD_DIALOGBAR/*关联的DialogBar资
源ID*/, CBRS_LEFT|CBRS_TOOLTIPS|CBRS_FLYBY|CBRS_BOTTOM, ID_MYDIALOGBAR);
CTreeCtrl的使用.IDC_TREE是在DialogBar上的一个树形控件.
void CMainFrame::InitTreeCtrl()
{
CTreeCtrl* pTree = (CTreeCtrl*)m_wndMyDialogBar.GetDlgItem(IDC_TREE);
TV_INSERTSTRUCT TreeCtrlItem;
HTREEITEM hTreeItem;
TreeCtrlItem.hInsertAfter = TVI_LAST;
TreeCtrlItem.item.mask = TVIF_TEXT|TVIF_PARAM;
//插入根结点
TreeCtrlItem.hParent = TVI_ROOT;
TreeCtrlItem.item.pszText = "Root";
hTreeItem = pTree->InsertItem(&TreeCtrlItem); //记录根结点句柄
TreeCtrlItem.hParent = hTreeItem;
TreeCtrlItem.item.pszText = "Child1";
pTree->InsertItem(&TreeCtrlItem);
TreeCtrlItem.hParent = hTreeItem;
TreeCtrlItem.item.pszText = "Child2";
hTreeItem = pTree->InsertItem(&TreeCtrlItem);
TreeCtrlItem.hParent = hTreeItem;
TreeCtrlItem.item.pszText = "Child2_1";
pTree->InsertItem(&TreeCtrlItem);
}
BitBlt使用的一个例子
void CJijeView::OnMove()
{
// TODO: Add your command handler code here
isTrace = FALSE;
int n = 0;
CBitmap bitmap1, bitmap2;
bitmap1.LoadBitmap(IDB_BITMAP1);
bitmap2.LoadBitmap(IDB_BITMAP2);
point_node* p = pPoint_head;
CPoint old_point;
while (p != NULL)
{
CClientDC dc(this);
CDC* memdc = new CDC;
memdc->CreateCompatibleDC(&dc); //创建内存DC
if (n == 0)
{
memdc->SelectObject(&bitmap1);
n = 1;
}
else
{
memdc->SelectObject(&bitmap2); //位图选入内存DC
n = 0;
}
//刷掉上一点的
if (n == 0)
{
memdc->SelectObject(&bitmap2);
dc.BitBlt(old_point.x-24, old_point.y-24, 48, 48, memdc, 0, 0,
PATPAINT);
}
else
{
memdc->SelectObject(&bitmap1);
dc.BitBlt(old_point.x-24, old_point.y-24, 48, 48, memdc, 0, 0,
PATPAINT);
}
dc.BitBlt(p->point.x-24, p->point.y-24, 48, 48, memdc, 0, 0, SRCAND);
old_point = p->point;
p = p->pPoint_next;
delete memdc;
memdc = NULL;
for(long j=0; j<9000000; j++)
;
}
//再给出轨迹
p = pPoint_head;
CClientDC dc(this);
while (p != NULL)
{
dc.SetPixel(p->point, RGB(0,0,0));
p = p->pPoint_next;
}
}
CFile类的使用一例子: //实现拷贝文件
BOOL CFileCopy::copy(CString strSource, CString strTarget)
{
/*virtual BOOL Open( LPCTSTR lpszFileName, UINT nOpenFlags,
CFileException* pError = NULL );*/
CFile fSource, fTarget;
CFileException e1, e2;
if (!fSource.Open(strSource, CFile::modeRead, &e1))
{
AfxMessageBox(_T("找开源文件失败"), MB_OK, 0);
return FALSE;
}
else if (!fTarget.Open(strTarget, CFile::modeCreate|CFile::modeWrite, &e2))
{
AfxMessageBox(_T("找开目标文件失败"));
return FALSE;
}
//拷贝
char c[1024*4];
int nRealRead;
nRealRead = fSource.Read(c, 1024*4);
while (nRealRead)
{
fTarget.Write(c, nRealRead);
nRealRead = fSource.Read(c, 1024*4);
}
fSource.Close();
fTarget.Close();
return TRUE;
}
当发现原工程不使用时,如dsp文件给删掉了,一个解决方法是建一个同名的工程,再将原工程下的文件拷到
新工程中即可(拷贝包括rc,res,但不包括dsp,ncb等).
有可能要反复删除clw文件再重建来达到正常工作的目的.
CList的一个用法 //CList<student, student> students
student s;
POSITION po = students.GetHeadPosition();
int len = students.GetCount();
CString name;
UINT score;
int j = 0;
for (int i=0; i<len; i++)
{
if (po != NULL)
{
s = students.GetNext(po);
name = s.name;
score = s.score;
str.Format(_T("%s的成绩是%u"), name, score);
pDC->TextOut(20, j, str);
j += 20;
}
}
设置单选框选上的一个方法: ((CButton*)GetDlgItem(IDC_RADIO1))->SetCheck(TRUE);
三个单选框使用的一个方法: 三个单选框按顺序排列,第一个的group属性选上,其它两个不选上. 第一个
关联一个值变量m_radio,当UpdateData(true)之后根据m_radio的值就知道哪个被选上,如果m_radio==0则
第一个选上,如果m_radio==1则第二个选上,如果m_radio==2则第三个选上.
CPtrList列表用来存放同一种任意类型的指针,指针被删除后,它指向的内存块不会自动被删除,需另行删
除.
获得一个画布上字体的尺寸:
TEXTMETRIC textMetric;
pDC->GetTextMetrics(&textMetric);
int fontHeight = textMetric.tmHeight;