📄 osdlg.cpp
字号:
// osDlg.cpp : implementation file
//
#include "stdafx.h"
#include "os.h"
#include "osDlg.h"
#include "shd.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// COsDlg dialog
COsDlg::COsDlg(CWnd* pParent /*=NULL*/)
: CDialog(COsDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(COsDlg)
m_Pmem = 50;
m_Pprio = 20;
m_Ptime = 8;
m_select = 0;
m_Pid = 1;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void COsDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(COsDlg)
DDX_Control(pDX, IDC_OVERQ, m_OverQ);
DDX_Control(pDX, IDC_RESERVEQ, m_ReserveQ);
DDX_Control(pDX, IDC_READYQ, m_ReadyQ);
DDX_Text(pDX, IDC_Pmem, m_Pmem);
DDX_Text(pDX, IDC_Pprio, m_Pprio);
DDX_Text(pDX, IDC_Ptime, m_Ptime);
DDX_Radio(pDX, IDC_RADIO1, m_select);
DDX_Text(pDX, IDC_Pname, m_Pid);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(COsDlg, CDialog)
//{{AFX_MSG_MAP(COsDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_ADD, OnAdd)
ON_BN_CLICKED(IDC_RUN, OnRun)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_QUIT, OnQuit)
ON_WM_CLOSE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// COsDlg message handlers
BOOL COsDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void COsDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void COsDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR COsDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
//创建新的进程式
void COsDlg::OnAdd()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE); //刷新数据
schd.sched_kind=m_select; //默认优先权算法
CString str;
Cpcb* p=schd.randpcb();
p->pid=m_Pid; //赋值
p->priority=m_Pprio;
p->time_slice=m_Ptime;
p->memory=m_Pmem;
schd.add_to_reserve(p);
if( m_select==SCHED_PRIO )
{
m_ReserveQ.ResetContent();
Addtolistbox(m_ReserveQ,schd.reserve);
}
else
{
str.Format("PID:%d P:%d T:%d-%d M:%d ",p->pid,p->priority,p->time_slice,p->time_ran,p->memory);
m_ReserveQ.AddString(str); //内容输出
}
p=schd.randpcb();
m_Pid=p->pid; //赋值
m_Pprio=p->priority;
m_Ptime=p->time_slice;
m_Pmem=p->memory;
schd.deletepcb(p); //删除指针
UpdateData(FALSE); //刷新数据
}
//列表内容显示
void COsDlg::Addtolistbox(CListBox& lb,CpcbList& pl)
{
CString str;
for(Cpcb* p=pl.head;p!=pl.end;p=p->nextp)
{
str.Format("PID:%d P:%d T:%d-%d M:%d",p->pid,p->priority,p->time_slice,p->time_ran,p->memory);
lb.AddString(str); //显示输出
}
}
void COsDlg::OnRun()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
schd.sched_kind=m_select;
if (schd.reserve.count<1) //如果还没有添加过任何进程
{
AfxMessageBox("请添加进程!");
}
else
{
schd.schedule();
if (schd.active.count==0)
{
if (schd.reserve.head!=schd.reserve.end)
{
schd.reserve.remove(schd.reserve.head);
}
MessageBox("内存不足了,出错!");
return;
}
SetTimer(1,1500,0); //定时器
m_ReadyQ.ResetContent();
Addtolistbox(m_ReadyQ,schd.active);
m_ReserveQ.ResetContent();
Addtolistbox(m_ReserveQ,schd.reserve);
//使按钮变灰
GetDlgItem(IDC_RUN)->EnableWindow(0);
UpdateData(0);
}
}
void COsDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if( schd.active.count || schd.hang.count )
schd.in_schedule();
else
{
KillTimer(1); //销毁定时器
GetDlgItem(IDC_RUN)->EnableWindow(1);
UpdateAll(); //刷新
MessageBox("进程调度完毕!"); //提示消息
return;
}
UpdateAll();
CDialog::OnTimer(nIDEvent);
}
//更新数据
void COsDlg::UpdateAll()
{
CString str;
//更新显示数据
m_ReserveQ.ResetContent();
Addtolistbox(m_ReserveQ,schd.reserve);
m_ReadyQ.ResetContent();
Addtolistbox(m_ReadyQ,schd.active);
m_OverQ.ResetContent();
Addtolistbox(m_OverQ,schd.over);
int count=m_ReadyQ.GetCount();
CBrush brush(RGB(255,255,255));//设定颜色值
CClientDC dc(this);
dc.SelectObject(&brush);
dc.Rectangle(20,140,411,194); //矩形范围
for (int i=0;i <count;i++)
{
m_ReadyQ.GetText(i,str);
int temp=str.Find(' ');
str=str.Left(temp);
str.Delete(0,4);
temp=atoi(str);
Cpcb* p=schd.active.findp(temp);
UpdateProgress(i,p);
}
UpdateData(FALSE);
Cpcb *current;
if (schd.active.head==schd.active.end)
{
return;
}
if (schd.sched_kind==SCHED_PRIO)
{
current=schd.active.head;
}
else
{
current=schd.active.end->prep;
}
if (current->time_ran==current->time_slice)
{
Cpcb* p=schd.active.pop_front();
schd.add_to_over(p);
schd.schedule();
}
}
//更新进程数据
void COsDlg::UpdateProgress(int i,Cpcb* p)
{
CString str;
int percent=int((float)p->time_ran/p->time_slice*100);
str.Format("进程%d %3d%%",p->pid,percent);
CClientDC dc(this);
SetBkMode(dc,TRANSPARENT);
//定义画刷及颜色
CBrush brush0(RGB(0, 0, 255));
CBrush brush1(RGB(255,100,100));
CBrush brush2(RGB(100,255,100));
CBrush brush3(RGB(100,100,255));
CBrush brush4(RGB(255,0,100));
CBrush brush5(RGB(255,100,0));
CBrush brush6(RGB(100,255,0));
CBrush* pOldBrush = dc.SelectObject(&brush0);
//矩形范围
dc.Rectangle(20,140,52,194);
dc.TextOut(23,147,"Sys");
dc.TextOut(23,167,"64K");
CString strName;
strName.Format("P%d",p->pid);//格式化字符串
CString strMem;
strMem.Format("%dK",p->memory);//格式化字符串
// percent=int((float)schd.current->time_ran/schd.current->time_slice*100);
// percentStr.Format("%3d%%",percent);
switch (i)
{
case 0:
dc.SelectObject(&brush1);
dc.Rectangle(20+p->mm_begin/2,140,20+(p->mm_begin+p->memory)/2,194);
//输出文字
dc.TextOut(23+p->mm_begin/2,147,strName);
dc.TextOut(23+p->mm_begin/2,167,strMem);
break;
case 1:
dc.SelectObject(&brush2);
dc.Rectangle(20+p->mm_begin/2,140,20+(p->mm_begin+p->memory)/2,194);
//输出文字
dc.TextOut(23+p->mm_begin/2,147,strName);
dc.TextOut(23+p->mm_begin/2,167,strMem);
break;
case 2:
dc.SelectObject(&brush3);
dc.Rectangle(20+p->mm_begin/2,140,20+(p->mm_begin+p->memory)/2,194);
dc.TextOut(23+p->mm_begin/2,147,strName);
dc.TextOut(23+p->mm_begin/2,167,strMem);
break;
case 3:
dc.SelectObject(&brush4);
dc.Rectangle(20+p->mm_begin/2,140,20+(p->mm_begin+p->memory)/2,194);
dc.TextOut(23+p->mm_begin/2,147,strName);
dc.TextOut(23+p->mm_begin/2,167,strMem);
break;
case 4:
dc.SelectObject(&brush5);
dc.Rectangle(20+p->mm_begin/2,140,20+(p->mm_begin+p->memory)/2,194);
dc.TextOut(23+p->mm_begin/2,147,strName);
dc.TextOut(23+p->mm_begin/2,167,strMem);
break;
case 5:
dc.SelectObject(&brush6);
dc.Rectangle(20+p->mm_begin/2,140,20+(p->mm_begin+p->memory)/2,194);
dc.TextOut(23+p->mm_begin/2,147,strName);
dc.TextOut(23+p->mm_begin/2,167,strMem);
break;
default:
MessageBox("进度条设置出错!");
break;
}
}
//程序退出
void COsDlg::OnQuit()
{
// TODO: Add your control notification handler code here
exit(EXIT_SUCCESS);
// OnCancel();
}
void COsDlg::OnClose()
{
// TODO: Add your message handler code here and/or call default
exit(EXIT_SUCCESS);
CDialog::OnClose();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -