📄 demodlga.cpp
字号:
// DemoDlgA.cpp : implementation file
//
#include "stdafx.h"
#include "Messenger.h"
#include "DemoDlgA.h"
#include "MainFrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include "winsock2.h"
#include "stdio.h"
#include "wininet.h"
#pragma comment(lib,"wininet.lib")
/////////////////////////////////////////////////////////////////////////////
// CDemoDlgA dialog
extern CMessengerApp theApp;
CDemoDlgA::CDemoDlgA(CWnd* pParent /*=NULL*/)
: CDialog(CDemoDlgA::IDD, pParent)
{
//{{AFX_DATA_INIT(CDemoDlgA)
//}}AFX_DATA_INIT
m_TickCnt = 0;
_strset(m_strStockNo,'\0');
//memset(m_strStockNo,'\0',sizeof(m_strStockNo));
}
void CDemoDlgA::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDemoDlgA)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDemoDlgA, CDialog)
//{{AFX_MSG_MAP(CDemoDlgA)
ON_BN_CLICKED(IDC_BUTTON1, OnTest)
ON_MESSAGE(WM_SHOWTASK,onShowTask)
ON_WM_TIMER()
ON_WM_CREATE()
ON_WM_DESTROY()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDemoDlgA message handlers
#include "Messenger.h"
void CDemoDlgA::OnTest()
{
NOTIFYICONDATA nid;
nid.cbSize=(DWORD)sizeof(NOTIFYICONDATA);
nid.hWnd=this->m_hWnd;
nid.uID=IDR_MAINFRAME;
nid.uFlags=NIF_ICON|NIF_MESSAGE|NIF_TIP ;
nid.uCallbackMessage = WM_SHOWTASK;//自定义的消息名称
nid.hIcon=LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_MAINFRAME));
strcpy(nid.szTip,"眼睛护士提示");//信息提示条为“计划任务提醒”
Shell_NotifyIcon(NIM_ADD,&nid);//在托盘区添加图标
//ShowWindow(SW_HIDE);//隐藏主窗口
theApp.m_pMainWnd->ShowWindow(SW_HIDE);
}
LRESULT CDemoDlgA::onShowTask(WPARAM wParam,LPARAM lParam)
{//wParam接收的是图标的ID,而lParam接收的是鼠标的行为
if(wParam!=IDR_MAINFRAME)
return 1;
switch(lParam)
{
case WM_RBUTTONUP://右键起来时弹出快捷菜单,这里只有一个“关闭”
{
LPPOINT lpoint=new tagPOINT;
::GetCursorPos(lpoint);//得到鼠标位置
CMenu menu;
menu.CreatePopupMenu();//声明一个弹出式菜单
//增加菜单项“关闭”,点击则发送消息WM_DESTROY给主窗口(已
//隐藏),将程序结束。
menu.AppendMenu(MF_STRING,WM_DESTROY,"关闭");
//确定弹出式菜单的位置
menu.TrackPopupMenu(TPM_LEFTALIGN,lpoint->x,lpoint->y,this);
//资源回收
HMENU hmenu=menu.Detach();
menu.DestroyMenu();
delete lpoint;
PostQuitMessage(0);
}
break;
case WM_LBUTTONDBLCLK://双击左键的处理
{
theApp.m_pMainWnd->ShowWindow(SW_SHOW);//简单的显示主窗口完事儿
//this->ShowWindow(SW_SHOW);
}
break;
default:
break;
}
return 0;
}
void CDemoDlgA::PostNcDestroy()
{
// TODO: Add your specialized code here and/or call the base class
CDialog::PostNcDestroy();
}
void CDemoDlgA::OnTimer(UINT nIDEvent)
{
int yy,mm,dd;
int i,result_index;
BOOL b_htmlformat,b_refresh;
long file_size;
char *str_sh = "sh60";
char *str_sz = "sz00";
char str_filename[16];
char str_buffer[HTML_BUFFER_SIZE];
char str_stockinfo[STOCK_TOP20_CNT][STOCK_STR_LENGTH];
CWnd *pWnd;
CTime cur_time;
FILE *fp_stock;
DWORD dwBytesRead;
CMainFrame *pFrame;
m_TickCnt++;
m_TickCnt = m_TickCnt % 120;
if(m_TickCnt == 0)
AfxMessageBox("一个小时了,该休息下眼睛了!");
dwBytesRead = 0;
b_refresh = FALSE;
pFrame = (CMainFrame *)AfxGetMainWnd();
memset(str_buffer,'\0',sizeof(str_buffer));
memset(str_stockinfo,'\0',sizeof(str_stockinfo));/*Reset all data*/
HINTERNET hNet = ::InternetOpen("Sina Top10",PRE_CONFIG_INTERNET_ACCESS,NULL,INTERNET_INVALID_PORT_NUMBER,0);
HINTERNET hUrlFile = ::InternetOpenUrl(hNet,"http://stock.finance.sina.com.cn/stock/quote/fivemininc10.shtml",NULL,0,INTERNET_FLAG_RELOAD,0);
BOOL bRead = ::InternetReadFile(hUrlFile,str_buffer,sizeof(str_buffer),&dwBytesRead);
::InternetCloseHandle(hUrlFile);
::InternetCloseHandle(hNet);
#if 0
if(dwBytesRead != 0)
{
result_index = 0;
b_htmlformat = TRUE;
for(i = 0;i < HTML_BUFFER_SIZE;i++)
{
if(b_htmlformat == FALSE)
{
if(str_buffer[i] == '>')
b_htmlformat = TRUE;
else
continue;/*html file format parse flag,to indicat if html language contents*/
}
if(str_buffer[i] == '<')
{/*html file < and > must match,and we do not care html content,skip this character*/
b_htmlformat = FALSE;
continue;
}
if(str_buffer[i] == 's')
{
if((strncmp((const char *)(str_buffer + i),str_sh,4) == 0) || (strncmp((const char *)(str_buffer + i),str_sz,4) == 0))
{/*get intresting stock information,code match ok!*/
//if(strncmp((const char *)(str_buffer + i),strno_pre,8) != 0)
//strno_pre = str_stockinfo[result_index];
strncpy(str_stockinfo[result_index],(const char *)(str_buffer + i),8);
str_stockinfo[result_index][8] = '\0';/*Copy stock code*/
pFrame = (CMainFrame *)AfxGetMainWnd();
pFrame->m_StockInfo[result_index].Format(_T("%s"),str_stockinfo[result_index]);
result_index++;
}
}
else if((str_buffer[i] == '\0') \
|| ((str_buffer[i] == EOF)) \
|| (result_index == STOCK_TOP20_CNT))
break;
else
continue;
}
}
#else
if(dwBytesRead != 0)
{
result_index = 0;
b_htmlformat = TRUE;
for(i = 0;i < HTML_BUFFER_SIZE;i++)
{
if(b_htmlformat == FALSE)
{
if(str_buffer[i] == '>')
b_htmlformat = TRUE;
else
continue;/*html file format parse flag,to indicat if html language contents*/
}
if(str_buffer[i] == '<')
{/*html file < and > must match,and we do not care html content,skip this character*/
b_htmlformat = FALSE;
continue;
}
if(str_buffer[i] == 's')
{
if((strncmp((str_buffer + i),str_sh,4) == 0) || (strncmp((const char *)(str_buffer + i),str_sz,4) == 0))
{
if(result_index == 9)
{/*First get stock information,compare to judge if need refresh*/
if(strncmp((str_buffer + i),m_strStockNo,8) == 0)
{
b_refresh = FALSE;
break;/*Need not to refresh Data*/
}
else
{
b_refresh = TRUE;
memcpy(m_strStockNo,(str_buffer + i),8);
}
}
i = filter_html_format(str_buffer,i,str_stockinfo[result_index],STOCK_STR_LENGTH - 1) - 1;
pFrame->m_StockInfo[result_index].Format(_T("%s"),str_stockinfo[result_index]);
result_index++;
}
}
else if((str_buffer[i] == '\0') \
|| ((str_buffer[i] == EOF)) \
|| (result_index == STOCK_TOP20_CNT))
break;
else
continue;
}
}
#endif
if((result_index == 20) && (b_refresh == TRUE))
{/*Recod total 20 information*/
yy = cur_time.GetCurrentTime().GetYear();
mm = cur_time.GetCurrentTime().GetMonth();
dd = cur_time.GetCurrentTime().GetDay();
sprintf(str_filename,"stk%d%d%d.stp",yy,mm,dd);
fp_stock = fopen(str_filename,"ab");
if(fp_stock==NULL)
{
ASSERT(0);
}
fseek(fp_stock,0,SEEK_END);
file_size = ftell(fp_stock);
for(i = 0;i < STOCK_TOP20_CNT;i++)
{
int stockinfo_len = strlen(str_stockinfo[i]);
if(stockinfo_len != 0)
{
fwrite(str_stockinfo[i],sizeof(str_stockinfo[0][0]),stockinfo_len,fp_stock);
fwrite(" ",1,1,fp_stock);/*tab blank between each items*/
}
}
fclose(fp_stock);
}
if(b_refresh == TRUE)
{/*Get data by dlga,notify to update dlgb*/
pWnd = FindWindow(_T("DemoDlgB"),NULL);
if(pWnd != NULL)
{
pWnd->PostMessage(WM_REFRESH_STOCK,0,0);
}
}
CDialog::OnTimer(nIDEvent);
}
int CDemoDlgA::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CDialog::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
SetTimer(1,HTML_REFRESH_INTERVAL,NULL);
OnTimer(1);
return 0;
}
void CDemoDlgA::OnDestroy()
{
CDialog::OnDestroy();
// TODO: Add your message handler code here
KillTimer(1);
}
int CDemoDlgA::filter_html_format(char *p_str, int cur_pos,char *p_info,int info_len)
{/*Skip "<****>" html content,also filter tabs,only leave usefull information*/
int i,j,ret_pos,patch_times;
UINT html_flag;/*not encounter html '<','>' etc.0:init state,1:'<'met,2:'>'match,3:another'<'met,section parser end*/
j = 0;
html_flag = 0;
patch_times = 0;
for(i = 0;((j < info_len - 1) && (i < STOCK_INFO_MAX_BYTES));i++)
{
if((p_str[cur_pos + i] == '>') || (p_str[cur_pos + i] == '<'))
{
html_flag++;/*State increase by 1*/
continue;
}
if(((html_flag % 2) == 0))
{
//if((p_str[cur_pos + i] != ' ') && (p_str[cur_pos + i] != ' '))
{
if(p_str[cur_pos + i] == ':')/*Patch for redudant characters*/
{/*:is the time format,when get it the second time,set it to '\0',as the end of string*/
patch_times++;
if(patch_times == 2)
break;
}
p_info[j++] = p_str[cur_pos + i];
}
}
}
p_info[j] = '\0';
ret_pos = cur_pos + i - 1;
return ret_pos;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -