Files
AccompanyCube/NotificationFormUnit.cpp
2026-06-13 00:10:11 +08:00

567 lines
19 KiB
C++

//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "NotificationFormUnit.h"
#pragma package(smart_init)
#pragma link "PNGButton"
#pragma resource "*.dfm"
TNotificationForm *NotificationForm;
//---------------------------------------------------------------------------
#define PERIOD_SHUTDOW ( 2*60*1000 )
#define PERIOD_TEXT ( 3*1000 )
//---------------------------------------------------------------------------
#define LEN_BKG_EXTERNAL_ROUND (1)
#define LEN_BKG_EXTERNAL_FRMAE (1)
#define LEN_BKG_INTERNAL_LINE (23)
//---------------------------------------------------------------------------
#define AD_PANEL_TOP (LEN_BKG_INTERNAL_LINE + 3)
#define AD_PANEL_LEFT (8)
#define AD_PANEL_WIDTH (Width - 2*AD_PANEL_LEFT)
#define AD_PANEL_HEIGHT (75)
#define TEXT_HEIGHT (LEN_BKG_INTERNAL_LINE + 1)
//---------------------------------------------------------------------------
#define JSON_ID_TYPE 0
#define JSON_ID_NAME 1
#define JSON_ID_COUNT 2
#define JSON_ID_DB 3
#define JSON_TYPE_PIC 1
#define JSON_TYPE_TEXT 2
#define JSON_TYPE_CONTENT 3
//---------------------------------------------------------------------------
const static COLORREF
CLR_TXT_HOT = RGB(171, 186, 207),
CLR_BKG_EXTERNAL_ROUND = RGB(60, 62, 70),
CLR_BKG_EXTERNAL_FRMAE = RGB(98, 106, 114),
CLR_BKG_INTERNAL = RGB(39, 42, 47),
CLR_BKG_INTERNAL_LINE_HIG = RGB(71, 73, 78),
CLR_BKG_INTERNAL_LINE_BLACK = RGB(22, 24, 26);
//---------------------------------------------------------------------------
__fastcall TNotificationForm::TNotificationForm(TComponent* Owner)
: TForm(Owner)
{
Color = CLR_BKG_INTERNAL;
this->Left = Screen->Width - this->Width - 14;
this->Top = Screen->WorkAreaHeight - this->Height;
//公告区
m_pTextNotiTimer = new TTimer(this);
m_pTextNotiTimer->Enabled = false;
m_pTextNotiTimer->Interval = PERIOD_TEXT;
m_pTextNotiTimer->OnTimer = NotificationTimerProc;
//自动关闭
m_pNotiShutDownTimer = new TTimer(this);
m_pNotiShutDownTimer->Enabled = false;
m_pNotiShutDownTimer->Interval = PERIOD_SHUTDOW;
m_pNotiShutDownTimer->OnTimer = NotificationTimerProc;
m_pNotificationThread = new TNotificationThread(true, this);
m_pNotificationThread->Start();
SetTransparent( this );
}
//---------------------------------------------------------------------------
void __fastcall TNotificationForm::setPANEL()
{
bool bSuccess = true;
int tmp = m_VCJsonAD.size();
if( m_VCJsonAD.size() ) {
m_pNotiADPanel = new TNotiADPanel(this, &m_VCJsonAD);
m_pNotiADPanel->Parent = this;
m_pNotiADPanel->Top = AD_PANEL_TOP;
m_pNotiADPanel->Left = AD_PANEL_LEFT;
m_pNotiADPanel->Width = AD_PANEL_WIDTH;
m_pNotiADPanel->Height = AD_PANEL_HEIGHT;
}else { bSuccess = false;}
if( m_VCJsonContent.size() ) {
m_pNotiContenPanel = new TNotiContenPanel(this, &m_VCJsonContent);
m_pNotiContenPanel->Parent = this;
if( bSuccess ) {
m_pNotiContenPanel->Top = AD_PANEL_TOP + AD_PANEL_HEIGHT;
m_pNotiContenPanel->Left = AD_PANEL_LEFT;
m_pNotiContenPanel->Width = this->Width - 2*AD_PANEL_LEFT;
m_pNotiContenPanel->Height = this->Height - TEXT_HEIGHT - m_pNotiContenPanel->Top;
}else{
m_pNotiContenPanel->Top = AD_PANEL_TOP;
m_pNotiContenPanel->Left = 0;
m_pNotiContenPanel->Width = this->Width;
m_pNotiContenPanel->Height = this->Height - TEXT_HEIGHT - m_pNotiContenPanel->Top;
}
bSuccess = true;
}else { bSuccess = false;}
if( m_VCJonNoti.size() ) {
m_pNotiUrl = new TUrlLabel(this);
m_pNotiUrl->Parent = this;
m_pNotiUrl->Caption = (*m_VCJonNoti.begin()).strName;
String url = (*m_VCJonNoti.begin()).strUrl;
if( url.Length() ) {
m_pNotiUrl->StrUrl = url;
}else{
char strURL[256];
f_CS_GetWebPageURL( URL_HOMEPAGE, strURL );
m_pNotiUrl->StrUrl = (String)strURL;
}
m_pNotiUrl->Top = this->Height - TEXT_HEIGHT + (TEXT_HEIGHT - m_pNotiUrl->Height)/2;
m_pNotiUrl->Left = (this->Width - m_pNotiUrl->Width)/2;
if( m_VCJonNoti.size() > 1) {
m_pNotiUrl->OnMouseEnter = NotiChangeMouseEnter;
m_pNotiUrl->OnMouseLeave = NotiChangeMouseLeave;
m_pTextNotiTimer->Enabled = true;
}
}
if( bSuccess ){
m_pNotiShutDownTimer->Enabled = true;
}
}
//---------------------------------------------------------------------------
void __fastcall TNotificationForm::CreateParams(Vcl::Controls::TCreateParams &Params)
{
TForm::CreateParams(Params);
Params.WndParent = GetDesktopWindow();
Params.ExStyle |= WS_EX_TOOLWINDOW;
}
//---------------------------------------------------------------------------
void __fastcall TNotificationForm::FormShow(TObject *Sender)
{
this->Left = Screen->Width - this->Width - 14;
this->Top = Screen->WorkAreaHeight - this->Height;
// AnimateWindow(Handle, 1000, AW_BLEND);
}
//---------------------------------------------------------------------------
void __fastcall TNotificationForm::FormClose(TObject *Sender, TCloseAction &Action)
{
/* AnimateWindow(Handle,1000,AW_BLEND+AW_HIDE);
if( m_pNotiADPanel ) { delete m_pNotiADPanel; m_pNotiADPanel = NULL; }
if( m_pTextNotiTimer ){ delete m_pTextNotiTimer; m_pTextNotiTimer = NULL; }
if( m_pNotiShutDownTimer ){ delete m_pNotiShutDownTimer; m_pNotiShutDownTimer = NULL; }
if( m_pNotiUrl ) { delete m_pNotiUrl; m_pNotiUrl = NULL;}
if( m_pNotiContenPanel ) { delete m_pNotiContenPanel; m_pNotiContenPanel = NULL; }
if( m_pNotificationThread ) { delete m_pNotificationThread; m_pNotificationThread = NULL; }
ClearJsonDirect();
m_VCJsonAD.clear();
m_VCJonNoti.clear();
m_VCJsonContent.clear();
*/
PostMessage(Application->MainForm->Handle, UM_NOTIFICATION,
NOTIFICATION_SHOW_CLOSED, 0 );
}
//---------------------------------------------------------------------------
__fastcall TNotificationForm::~TNotificationForm()
{
if( m_pNotiADPanel ) { delete m_pNotiADPanel; m_pNotiADPanel = NULL; }
if( m_pTextNotiTimer ){ delete m_pTextNotiTimer; m_pTextNotiTimer = NULL; }
if( m_pNotiShutDownTimer ){ delete m_pNotiShutDownTimer; m_pNotiShutDownTimer = NULL; }
if( m_pNotiUrl ) { delete m_pNotiUrl; m_pNotiUrl = NULL;}
if( m_pNotiContenPanel ) { delete m_pNotiContenPanel; m_pNotiContenPanel = NULL; }
if( m_pNotificationThread ) {
StopURLDownload();
m_pNotificationThread->WaitFor();
delete m_pNotificationThread;
m_pNotificationThread = NULL;
}
ClearJsonDirect();
m_VCJsonAD.clear();
m_VCJonNoti.clear();
m_VCJsonContent.clear();
}
bool __fastcall TNotificationForm::ParseJson()
{
ClearJsonDirect();
char strURL[256];
if( S_OK != f_CS_GetWebPageURL( URL_JASONQUERY, strURL ) )
return false;
String urlJson = strURL;
String strJson;
String picDirect = JSON_PIC_DERICTORY;
String picName = L"json.tmp";
TStringStream *strStream = new TStringStream(NULL, 65001);
HRESULT resultJson = GetUrlData(urlJson, picDirect, picName, strStream, true);
strJson= strStream->DataString;
delete strStream;
// HRESULT resultJson = S_OK;
// strJson = L"{\"1\":{\"type\":1,\"name\":\"活动\",\"count\":3,\"db\":{\"1\":{\"name\":\"改编达人\",\"picture\":\"http://www.ichangzuo.com/ichangzuo/File/Music/Cover/201307/414_1374757039.png\",\"file\":\"\",\"url\":\"http://composemusic.xicp.net:81/index.php/Activity?tpl=show\",\"author\":\"\"},\"2\":{\"name\":\"改编达人\",\"picture\":\"http://www.ichangzuo.com/ichangzuo/File/Music/Cover/201403/414_1396202466.png\",\"file\":\"\",\"url\":\"http://composemusic.xicp.net:81/index.php/Activity?tpl=show\",\"author\":\"\"},\"3\":{\"name\":\"改编达人\",\"picture\":\"http://www.ichangzuo.com/ichangzuo/File/User/Photo/414_1374202747.jpg\",\"file\":\"\",\"url\":\"http://composemusic.xicp.net:81/index.php/Activity?tpl=show\",\"author\":\"\"}}},\"2\":{\"type\":1,\"name\":\"广告\",\"count\":0},\"3\":{\"type\":1,\"name\":\"软件\",\"count\":0},\"4\":{\"type\":3,\"name\":\"推荐音乐\",\"count\":3,\"db\":{\"1\":{\"name\":\"跟着感觉走\",\"picture\":\"http://composemusic.xicp.net:81/ichangzuo/File/Web/Default/music_cover_default.jpg\",\"file\":\"http://composemusic.xicp.net:81/ichangzuo/File/Music/Music/201307/867_1374396650.MP3\",\"url\":\"http://composemusic.xicp.net:81/index.php/Music?tpl=play&i=22\",\"author\":\"木木\"},\"2\":{\"name\":\"墨西歌舞\",\"picture\":\"http://composemusic.xicp.net:81/ichangzuo/File/User/Default/Music_cover/10475_1380381900.jpg\",\"file\":\"http://composemusic.xicp.net:81/ichangzuo/File/Music/Music/201309/10475_1378453592.mp3\",\"url\":\"http://composemusic.xicp.net:81/index.php/Music?tpl=play&i=549\",\"author\":\"下雨了\"},\"3\":{\"name\":\"123的\",\"picture\":\"http://composemusic.xicp.net:81/ichangzuo/File/Web/Default/music_cover_default.jpg\",\"file\":\"http://composemusic.xicp.net:81/ichangzuo/File/Music/Music/201403/23599_1395305792.mp3\",\"url\":\"http://composemusic.xicp.net:81/index.php/Music?tpl=play&i=2457\",\"author\":\"小花花\"}}},\"5\":{\"type\":3,\"name\":\"推荐歌词\",\"count\":1,\"db\":{\"1\":{\"name\":\"你会\",\"picture\":\"http://composemusic.xicp.net:81/ichangzuo/File/User/Portrait/default.jpg\",\"file\":\"\",\"url\":\"http://composemusic.xicp.net:81/index.php/Lyric?tpl=show&i=33\",\"author\":\"小花花\"}}},\"6\":{\"type\":2,\"name\":\"公告\",\"count\":3,\"db\":{\"1\":{\"name\":\"333\",\"picture\":\"\",\"file\":\"\",\"url\":\"\",\"author\":\"0\"},\"2\":{\"name\":\"这是一个测试的标题\",\"picture\":\"\",\"file\":\"\",\"url\":\"\",\"author\":\"0\"},\"3\":{\"name\":\"这是一个测试\",\"picture\":\"\",\"file\":\"\",\"url\":\"\",\"author\":\"0\"}}}}";
if( !m_pNotiShutDownTimer || !(resultJson==S_OK && strJson.Length() > 1) ){
return false;
}
String jsonPath = JSON_PIC_DERICTORY;
bool bSuccess = false;
int indexPic = 0;
TJSONObject *jsonOri = GetJsonContext(strJson);
if( jsonOri==NULL ) {
return false;
}
try
{
for( int i=0; i<jsonOri->Size(); i++ )
{
TJSONObject *ValueID = GetJsonValue(jsonOri, i);
if( ValueID==NULL ) {continue;}
TJSONObject *ValueCount = GetJsonValue(ValueID, JSON_ID_COUNT);
if( ValueCount==NULL ) { continue; }
if( StrToInt(ValueCount->Value())==0 ) { continue; }
TJSONObject *ValueDB = GetJsonValue(ValueID, JSON_ID_DB);
if( ValueDB==NULL) {continue;}
bool bTypeContent = false;
TJSONDB jsonContent;
for(int i=0; i<ValueDB->Size(); i++)
{
TJSONObject *ValueDBID = GetJsonValue(ValueDB, i);
if( ValueDBID==NULL ) {continue;}
TJSONObject *ValueType = GetJsonValue(ValueID, JSON_ID_TYPE);
if( ValueType==NULL ) { continue;}
TJSON jsonAdd;
jsonAdd.strName = ValueDBID->GetValue(L"name")->Value();
jsonAdd.strFileUrl = ValueDBID->GetValue(L"file")->Value();
jsonAdd.strUrl = ValueDBID->GetValue(L"url")->Value();
jsonAdd.strAuthor = ValueDBID->GetValue(L"author")->Value();
String strPicDirect;
String strType = ValueType->Value();
if( StrToInt(strType)==JSON_TYPE_PIC ){
strPicDirect = JSON_PIC_DERICTORY_AD;
}else if( StrToInt(strType)==JSON_TYPE_TEXT ) {
strPicDirect = JSON_PIC_DERICTORY_TEXT;
}else if( StrToInt(strType)==JSON_TYPE_CONTENT ){
strPicDirect = JSON_PIC_DERICTORY_CONTENT;
}
String strPicUrl = ValueDBID->GetValue(L"picture")->Value();
// String strPicUrl = L"http://www.ichangzuo.com/ichangzuo/File/Software/Content/Ac/137784127986.jpg";
if( strPicUrl.Length() ) {
strPicDirect = jsonPath + strPicDirect;
String strPicName = IntToStr(indexPic) + ExtractFileExt(strPicUrl);
resultJson = GetUrlData(strPicUrl, strPicDirect, strPicName);
if( !m_pNotiShutDownTimer ){
delete jsonOri;
return false;
}
if( resultJson == S_OK ){
jsonAdd.strNamePic = strPicName;
indexPic++;
}
}
if( StrToInt(strType)==JSON_TYPE_PIC && jsonAdd.strNamePic.Length() ){
m_VCJsonAD.push_back(jsonAdd);
}else if( StrToInt(strType)==JSON_TYPE_TEXT ) {
m_VCJonNoti.push_back(jsonAdd);
}else if( StrToInt(strType)==JSON_TYPE_CONTENT ){
jsonContent.vcContent.push_back(jsonAdd);
bTypeContent = true;
}
}
if( bTypeContent && jsonContent.vcContent.size() ) {
TJSONObject *ValueName = GetJsonValue(ValueID, JSON_ID_NAME);
if( ValueName == NULL ){
jsonContent.strDbName = L"";
}else {
jsonContent.strDbName = ValueName->Value();
}
m_VCJsonContent.push_back(jsonContent);
}
}
}catch(Exception &e){
delete jsonOri;
return false;
}
if( jsonOri ){
delete jsonOri;
jsonOri = NULL;
}
if( m_VCJsonAD.size() ) {
bSuccess = true;
}
if( m_VCJsonContent.size() ){
bSuccess = true;
}else { bSuccess = false;}
return bSuccess;
}
//---------------------------------------------------------------------------
bool __fastcall TNotificationForm::LoadImg()
{
TPicture *picImg = NULL;
int adSize = m_VCJsonAD.size();
if( adSize )
{
VCTJSON::iterator vc_pFindAD;
for( vc_pFindAD = m_VCJsonAD.begin();
vc_pFindAD != m_VCJsonAD.end();
vc_pFindAD++ )
{
if( (*vc_pFindAD).strNamePic.Length() ) {
if( GetPic(vc_pFindAD->strNamePic) )
{
try{
picImg = new TPicture();
picImg->LoadFromFile( (*vc_pFindAD).strNamePic );
delete picImg;
}catch(Exception &e){
return false;
}
}else{
return false;
}
}else{
return false;
}
}
}
adSize = m_VCJsonContent.size();
if( adSize ){
VCTJSONDB::iterator vc_pFindContent;
for( vc_pFindContent = m_VCJsonContent.begin();
vc_pFindContent != m_VCJsonContent.end();
vc_pFindContent++)
{
VCTJSON::iterator vc_pFindContentPIC;
for( vc_pFindContentPIC = (*vc_pFindContent).vcContent.begin();
vc_pFindContentPIC != (*vc_pFindContent).vcContent.end();
vc_pFindContentPIC++)
{
if( (*vc_pFindContentPIC).strNamePic.Length() )
{
if( GetPic(vc_pFindContentPIC->strNamePic) ) {
try{
picImg = new TPicture();
picImg->LoadFromFile( (*vc_pFindContentPIC).strNamePic );
delete picImg;
}catch(Exception &e){
return false;
}
}else{
return false;
}
}
}
}
}
return true;
}
//---------------------------------------------------------------------------
void __fastcall TNotificationForm::NotificationTimerProc(TObject* Sender)
{
TTimer *NotiTimer = (TTimer *)Sender;
int index = -1;
int size = 0;
if( NotiTimer== m_pTextNotiTimer) { //公告切换
index = -1;
size = m_VCJonNoti.size();
if( size>1 ) {
index = (m_iTextNotiIndex+1<size)?(m_iTextNotiIndex+1):0;
}else {
m_pTextNotiTimer->Enabled = false;
return;
}
if( m_iTextNotiIndex != index )
{
m_iTextNotiIndex = index;
VCTJSON::iterator vc_pFindNoti = m_VCJonNoti.begin() + index;
String url = (*vc_pFindNoti).strUrl;
if( url.Length() ) {
m_pNotiUrl->StrUrl = url;
}else{
m_pNotiUrl->StrUrl = L"http://www.ichangzuo.com/";
}
m_pNotiUrl->Caption = (*vc_pFindNoti).strName;
m_pNotiUrl->Left = (this->Width - m_pNotiUrl->Width)/2;
}
}else if( NotiTimer == m_pNotiShutDownTimer ) {
m_pNotiShutDownTimer->Enabled = false;
this->Close();
}
}
//---------------------------------------------------------------------------
void __fastcall TNotificationForm::WMEraseBkgnd(Winapi::Messages::TWMEraseBkgnd &Message)
{
TRect rect = GetClientRect();
TRect rectFill = rect;
TCanvas* tempCanvas = new TCanvas;
tempCanvas->Handle = Message.DC;
rectFill.Top = rectFill.Top + LEN_BKG_EXTERNAL_ROUND;
rectFill.Left = rectFill.Left + LEN_BKG_EXTERNAL_ROUND;
rectFill.Right = rectFill.Right - LEN_BKG_EXTERNAL_ROUND;
rectFill.Bottom = rectFill.Bottom - LEN_BKG_EXTERNAL_ROUND;
tempCanvas->Brush->Color = CLR_BKG_INTERNAL;
tempCanvas->FillRect( rectFill );
// //==========画圆角
tempCanvas->Pen->Color = CLR_BKG_EXTERNAL_ROUND;
int X,Y;
//上面的
Y = 0;
X = 0;
tempCanvas->Pixels[X][Y] = tempCanvas->Pen->Color;
X = Width - LEN_BKG_EXTERNAL_ROUND;
tempCanvas->Pixels[X][Y] = tempCanvas->Pen->Color;
//下面的
Y = Height - LEN_BKG_EXTERNAL_ROUND;
X = 0;
tempCanvas->Pixels[X][Y] = tempCanvas->Pen->Color;
X = Width - LEN_BKG_EXTERNAL_ROUND;
tempCanvas->Pixels[X][Y] = tempCanvas->Pen->Color;
//===========画边框
tempCanvas->Pen->Color = CLR_BKG_EXTERNAL_FRMAE;
tempCanvas->MoveTo( LEN_BKG_EXTERNAL_FRMAE, 0 );
tempCanvas->LineTo( Width-LEN_BKG_EXTERNAL_FRMAE, 0 );
tempCanvas->MoveTo( LEN_BKG_EXTERNAL_FRMAE, Height-LEN_BKG_EXTERNAL_FRMAE );
tempCanvas->LineTo( Width-LEN_BKG_EXTERNAL_FRMAE, Height-LEN_BKG_EXTERNAL_FRMAE );
tempCanvas->MoveTo( 0, LEN_BKG_EXTERNAL_FRMAE );
tempCanvas->LineTo( 0, Height-LEN_BKG_EXTERNAL_FRMAE );
tempCanvas->MoveTo( Width-LEN_BKG_EXTERNAL_FRMAE, LEN_BKG_EXTERNAL_FRMAE );
tempCanvas->LineTo( Width-LEN_BKG_EXTERNAL_FRMAE, Height-LEN_BKG_EXTERNAL_FRMAE );
//===========画分割线
int iLineHeight = LEN_BKG_INTERNAL_LINE;
tempCanvas->Pen->Color = CLR_BKG_INTERNAL_LINE_HIG;
tempCanvas->MoveTo( LEN_BKG_EXTERNAL_FRMAE, iLineHeight );
tempCanvas->LineTo( Width-LEN_BKG_EXTERNAL_FRMAE, iLineHeight );
tempCanvas->MoveTo( LEN_BKG_EXTERNAL_FRMAE, iLineHeight+2 );
tempCanvas->LineTo( Width-LEN_BKG_EXTERNAL_FRMAE, iLineHeight+2 );
tempCanvas->Pen->Color = CLR_BKG_INTERNAL_LINE_BLACK;
tempCanvas->MoveTo( LEN_BKG_EXTERNAL_FRMAE, iLineHeight+1 );
tempCanvas->LineTo( Width-LEN_BKG_EXTERNAL_FRMAE, iLineHeight+1 );
//画标题
tempCanvas->Font->Color = CLR_TXT_HOT;
tempCanvas->Brush->Style = bsClear;
TSize size = tempCanvas->TextExtent( Caption );
X = LEN_BKG_EXTERNAL_FRMAE + ( Width - size.Width )/2,
Y = LEN_BKG_EXTERNAL_FRMAE + ( LEN_BKG_INTERNAL_LINE-size.Height )/2;
X = ( Width - size.Width )/2;
Y = ( LEN_BKG_INTERNAL_LINE-size.Height )/2;
tempCanvas->TextOut( X, Y, Caption );
delete tempCanvas;
Message.Result = true;
}
//---------------------------------------------------------------------------
void __fastcall TNotificationForm::WMNCHitTest(TWMNCHitTest &Msg)
{
TForm::Dispatch(&Msg);
if( Msg.Result==HTCLIENT &&
(Msg.YPos<Top+LEN_BKG_INTERNAL_LINE) &&
(Msg.XPos<(Left+ m_PNGButtonClose->Left) ) )
{
Msg.Result = HTCAPTION;
}
}
//---------------------------------------------------------------------------
void __fastcall TNotificationForm::m_PNGButtonCloseClick(TObject *Sender)
{
this->Close();
}
//---------------------------------------------------------------------------
void __fastcall TNotificationForm::NotiChangeMouseEnter(TObject *Sender)
{
m_pTextNotiTimer->Enabled = false;
}
//---------------------------------------------------------------------------
void __fastcall TNotificationForm::NotiChangeMouseLeave(TObject *Sender)
{
m_pTextNotiTimer->Enabled = true;
}
//---------------------------------------------------------------------------
void __fastcall TNotificationForm::ClearJsonDirect()
{
String fileDirect = g_strUserPath.c_str();
fileDirect += JSON_PIC_DERICTORY;
if( DirectoryExists(fileDirect) ) {
TDirectory *clearDirect = new TDirectory();
clearDirect->Delete(fileDirect, true);
delete clearDirect;
}
}
//---------------------------------------------------------------------------