软件截图
下面是win32 界面程序代码
#define WIN32_LEAN_AND_MEAN // 从 Windows 头文件中排除极少使用的信息
#include <afxwin.h>
#include "resource.h"
#include <tchar.h>
#include"stdio.h"
#include"Draw.h"
#define _AFXDLL
#include <direct.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
BOOL CreateAToolBar(HWND hwnDlg, HINSTANCE hInstance);
char FilePath[MAX_PATH] ;
char NowPath[120];
BOOL FilePathFlag = FALSE;
double Factor = 0.6;
class mywin
{
public:
ATOM m_Register(_In_ HINSTANCE hInstance);
BOOL m_Instance(_In_ HINSTANCE hInstance, _In_ int nCmdShow);
BOOL m_ShowWindow();
BOOL m_UpdateWindow();
HWND m_hWnd;
};
ATOM mywin::m_Register(_In_ HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wcex.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);
wcex.lpszClassName = _T("Class");
wcex.hIconSm = NULL;
return RegisterClassEx(&wcex);
}
BOOL mywin::m_Instance(_In_ HINSTANCE hInstance, _In_ int nCmdShow)
{
UINT width = GetSystemMetrics(SM_CXSCREEN);
UINT height = GetSystemMetrics(SM_CYSCREEN);
m_hWnd = CreateWindow(_T("Class"),
_T("字母图"),
WS_OVERLAPPEDWINDOW,
0,
0,
width,
height - 40,
NULL,
NULL,
hInstance,
NULL);
if (!m_hWnd)
{
MessageBox(NULL, TEXT("创建窗口失败"), TEXT("提示"), MB_ICONWARNING);
return FALSE;
}
CreateAToolBar(m_hWnd, hInstance);
ShowWindow(m_hWnd, SW_SHOWNORMAL);
UpdateWindow(m_hWnd);
return TRUE;
}
HWND hToolBar;
BOOL CreateAToolBar(HWND hwnDlg, HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)GetStockObject(2);
wcex.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);
wcex.lpszClassName = _T("childClass");
wcex.hIconSm = NULL;
RegisterClassEx(&wcex);
const int NumImage = 6;
hToolBar = CreateWindowEx(NULL, TOOLBARCLASSNAME, " ", WS_CHILD | WS_VISIBLE | TBSTYLE_TOOLTIPS,
0, 0, 32*NumImage,32, hwnDlg, NULL, hInstance, NULL);
TBBUTTON tButton[NumImage];
ZeroMemory(tButton, sizeof(tButton));
HIMAGELIST hInmageList = ImageList_Create(32, 32, ILC_COLOR24, NumImage, 0);
int iBitmap[NumImage] = { 0 };
WORD Id[NumImage] = { IDB_OPEN, IDB_CUT, IDB_DRAW, IDB_BIG, IDB_SMALL, IDB_HELP };
for (int i = 0; i < NumImage; i++)
{
iBitmap[i] = ImageList_Add(hInmageList, LoadBitmap(hInstance, MAKEINTRESOURCE(Id[i])), 0);
}
SendMessage(hToolBar, TB_SETIMAGELIST, 0, (LPARAM)hInmageList);
for (int j = 0; j < NumImage; j++)
{
tButton[j].iBitmap = MAKELONG(iBitmap[j], 0);
tButton[j].fsState = TBSTATE_ENABLED;
tButton[j].fsStyle = TBSTYLE_BUTTON | BTNS_AUTOSIZE;
}
tButton[0].idCommand = ID_FILE_OPEN;
tButton[1].idCommand = ID_FILE_SAVE;
tButton[2].idCommand = ID_DRAW_DRAW;
tButton[3].idCommand = ID_EDIT_ENLARGE;
tButton[4].idCommand = ID_EDIT_SHRINK;
tButton[5].idCommand = ID_HELP_INSTRUCTION;
SendMessage(hToolBar, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 90);
SendMessage(hToolBar, TB_ADDBUTTONS, sizeof(tButton) / sizeof(TBBUTTON), (LPARAM)&tButton);
SendMessage(hToolBar, TB_AUTOSIZE, 0, 0);
COLORSCHEME color;
color.dwSize = sizeof(COLORSCHEME);
color.clrBtnHighlight = RGB(0, 255, 0);
color.clrBtnShadow = RGB(0,255,0);
SendMessage(hToolBar, TB_SETCOLORSCHEME, 0, (LPARAM)&color);
ShowWindow(hToolBar, TRUE);
return TRUE;
}
mywin Obj;
HINSTANCE *all_Instance;
int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPTSTR lpCmdLine,
_In_ int nCmdShow)
{
Obj.m_Register(hInstance);
Obj.m_Instance(hInstance, nCmdShow);
all_Instance = &hInstance;
_getcwd(NowPath, 120);
strcat(NowPath, "\\screener_plugin");
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
int xx = 0, yy = 0;
BOOL OpenFile(HWND hWnd)
{
OPENFILENAME ofn;
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hWnd;
ofn.lpstrFile = FilePath;
ofn.nMaxFile = sizeof(FilePath);
ofn.lpstrFilter = NULL;
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
FilePathFlag=GetOpenFileName(&ofn);
if (FilePathFlag)
{
TextOut(GetDC(hWnd), 10, 45, FilePath, strlen(FilePath));
}
else
{
DWORD dwError = NOERROR;
dwError = CommDlgExtendedError();
MessageBox(hWnd, _T("open file failed!"), _T("tips"), MB_OK);
}
return TRUE;
}
BOOL MyDraw(HDC hdc)
{
if (!FilePathFlag) return FALSE;
CDC *pDC = CDC::FromHandle(hdc);
CWnd *pWnd = pDC->GetWindow();
RECT rc;
pWnd->GetClientRect(&rc);
myWin WinRect;
WinRect.x = rc.left;
WinRect.y = rc.top + 500;
WinRect.height = rc.bottom - rc.top;
WinRect.width = rc.right - rc.left;
InterFun(pDC, WinRect, FilePath,Factor);
return TRUE;
}
void Instruction(HDC hdc)
{
CDC *pDC = CDC::FromHandle(hdc);
CPen pen;
pen.CreatePen(PS_SOLID, 1, RGB(0, 0, 250));
CFont font;
font.CreateFont(22,
0,
0,
0,
FW_NORMAL,
FALSE,
FALSE,
0,
ANSI_CHARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY,
DEFAULT_PITCH | FF_SWISS,
_T("宋体"));
pDC->SelectObject(&font);
pDC->SelectObject(pen);
pDC->Rectangle(300, 130, 1050, 410);
pDC->SetTextColor(RGB(0, 0, 250));
pDC->TextOut(320, 150, "使用说明:");
pDC->TextOut(320, 200, "画图数据是一列实型数据,打开数据前要在画图数据中添加画图参数,");
pDC->TextOut(320, 250, "第一行添加一个整型数据:氨基酸字母长度,");
pDC->TextOut(320, 300, "第二行添加一个整型数据:氨基酸编码长度(20或21),");
pDC->TextOut(320, 350, "两者乘积应等于实型数据的个数。");
DeleteObject(pen);
DeleteObject(font);
}
CButton btn;
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
switch (wmId)
{
case ID_FILE_SAVE:
{
STARTUPINFO si;
memset(&si, 0, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_SHOW;
PROCESS_INFORMATION pi;
int flag=CreateProcess(NULL,NowPath ,NULL,NULL,1,CREATE_NO_WINDOW,NULL,NULL,&si,&pi);
if (flag == 0)
{
MessageBox(hWnd, _T("CreateProcess fail!"), _T("title"), MB_OK);
MessageBox(hWnd, _T(NowPath), _T("path"), MB_OK);
}
break;
}
case ID_FILE_OPEN:
{
OpenFile(hWnd);
break;
}
case ID_DRAW_DRAW:
InvalidateRect(Obj.m_hWnd, 0, TRUE);
UpdateWindow(Obj.m_hWnd);
if (!MyDraw(GetDC(hWnd)))
MessageBox(hWnd, _T("请先选择文件再画图!"), _T("title"), MB_OK);
break;
case ID_EDIT_ENLARGE:
Factor += 0.05;
InvalidateRect(Obj.m_hWnd, 0, TRUE);
UpdateWindow(Obj.m_hWnd);
if (!MyDraw(GetDC(hWnd)))
MessageBox(hWnd, _T("请先选择文件再画图!"), _T("title"), MB_OK);
break;
case ID_EDIT_SHRINK:
Factor -= 0.05;
InvalidateRect(Obj.m_hWnd, 0, TRUE);
UpdateWindow(Obj.m_hWnd);
if (!MyDraw(GetDC(hWnd)))
MessageBox(hWnd, _T("请先选择文件再画图!"), _T("title"), MB_OK);
break;
case ID_HELP_INSTRUCTION:
Instruction(GetDC(hWnd));
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_LBUTTONDOWN:
{
break;
}
case WM_RBUTTONDOWN:
MessageBox(hWnd, _T("right"), _T("title"), MB_OK);
break;
case WM_CREATE:
{
RECT lRect;
GetClientRect(hToolBar, &lRect);
FillRect(GetDC(hToolBar), &lRect, GetSysColorBrush(COLOR_3DDKSHADOW));
break;
}
case WM_PAINT:
HDC hdc;
hdc = BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
以下是存放接口函数InterFun(pDC, WinRect, FilePath,Factor)的头文件Draw.h;
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include "float.h"
#include <afxwin.h>
#define N 1
#define WIDE WinRect.width/(double)(Ncolumn+2.5)
#define BLACK RGB(0,0,0)
#define GREEN RGB(0,255,0)
#define RED RGB(255,0,0)
#define BLUE RGB(0,0,255)
#define UN RGB(111,111,111)
int Ncolumn=0;
COLORREF color[26] = { BLACK, UN, GREEN, RED, RED, BLACK, GREEN, BLUE, BLACK, UN, BLUE, BLACK, BLACK, GREEN, UN, BLACK, GREEN, BLUE, GREEN, GREEN, UN, BLACK, BLACK, UN, BLACK, UN };
struct MyChar
{
char mychar[1];
COLORREF mycolor;
double myheight;
struct MyChar* prev;
struct MyChar* after;
};
struct myWin
{
int x;
int y;
int height;
int width;
};
FILE *open_file(const char * filename,const char *mode)
{
FILE *fp;
fp=fopen(filename,mode);
if(fp==NULL)
{
fprintf(stdout,"err msg is %s\n",filename);
exit(1);
}
return fp;
}
void Sort(struct MyChar * &MyChar,const int feature)
{
struct MyChar temp;
for(int i=0;i<feature;i++)
{
for(int j=i+1;j<feature;j++)
{
if(MyChar[i].myheight < MyChar[j].myheight)
{
temp = MyChar[i];
MyChar[i] = MyChar[j];
MyChar[j] = temp;
}
}
}
for(int n=0;n<feature-1;n++)
{
MyChar[feature-1-n].prev=&MyChar[(feature-1-n)-1];
MyChar[n].after=&MyChar[n+1];
}
MyChar[0].prev = NULL;
MyChar[feature-1].after=NULL;
}
int Paint_a_char(int _x,int _y,double height,COLORREF color,char* mychar,CDC *pDC,myWin WinRect)
{
CFont font;
CFont* oldfont;
LOGFONT lf;
memset(&lf, 0, sizeof(LOGFONT));
lf.lfWeight =500;
lf.lfWidth = WIDE;
lf.lfHeight = height;
if(height>0) lf.lfEscapement = 0;
else lf.lfEscapement=1800;
strcpy(lf.lfFaceName, _T("@MS Gothic"));
VERIFY(font.CreateFontIndirect(&lf));
oldfont=pDC->SelectObject(&font);
pDC->SetTextColor(color);
pDC->SetBkMode(TRANSPARENT);
int xx = _x;
int yy = _y;
if(height>0)
{ RECT rct = {xx , yy-lf.lfHeight, xx+lf.lfWidth , yy};
pDC->ExtTextOutA(_x , _y-lf.lfHeight , ETO_CLIPPED , &rct , mychar , 1 , NULL );}
else
{ RECT rct = {xx , yy, xx+lf.lfWidth , yy-lf.lfHeight};
pDC->ExtTextOutA(_x+lf.lfWidth , _y-lf.lfHeight , ETO_CLIPPED , &rct , mychar , 1 , NULL );}
yy-=lf.lfHeight;
pDC->SelectObject(oldfont);
font.DeleteObject();
return yy;
}
void Paint_column(struct MyChar *P_MyChar,int n,CDC *pDC,myWin WinRect,double origin_x,double origin_y,const int feature)
{
int y = origin_y-1;
struct MyChar *p_positive;
p_positive = &P_MyChar[feature-1];
while(p_positive != NULL)
{
if(p_positive->myheight <= 0)
{
p_positive =p_positive->prev;continue;
}
y = Paint_a_char(origin_x+WIDE*n,y,p_positive->myheight,p_positive->mycolor,p_positive->mychar,pDC,WinRect);
p_positive = p_positive->prev;
}
y = (int)origin_y+1;
p_positive = &P_MyChar[0];
while(p_positive != NULL)
{
if(p_positive->myheight >= 0)
{
p_positive =p_positive->after;continue;
}
y = Paint_a_char(origin_x+WIDE*n,y,p_positive->myheight,p_positive->mycolor,p_positive->mychar,pDC,WinRect);
p_positive = p_positive->after;
}
}
void Heng_zuo_biao(CDC *pDC,int n,myWin WinRect,double origin_x,double&FACTOR)
{
CFont font;
LOGFONT lf;
memset(&lf, 0, sizeof(LOGFONT));
lf.lfWeight =300;
lf.lfWidth = WIDE/4*FACTOR;
lf.lfHeight = WIDE/2*FACTOR;
lf.lfEscapement =0;
strcpy(lf.lfFaceName, "simsun");
VERIFY(font.CreateFontIndirect(&lf));
CFont* oldfont=pDC->SelectObject(&font);
pDC->SetTextColor(BLACK);
pDC->SetBkMode(TRANSPARENT);
int site=n;
site++;
char alpha[5]="P";
if (site<10)
{
alpha[1]=(char)(site+'0');
alpha[2]=0;
}
else if (site<100)
{
alpha[1]=(char)(site/10+'0');
alpha[2]=(char)(site%10+'0');
alpha[3]=0;
}
else
{
alpha[1]=(char)(site/100+'0');
alpha[2]=(char)((site%100)/10+'0');
alpha[3]=(char)(site%10+'0');
alpha[4]=0;
}
int x=origin_x + WIDE*n+WIDE*1/4 ;
int y=WinRect.height*FACTOR+WinRect.height*(1-FACTOR)/3+30;
pDC->TextOutA(x+15,y+65,alpha,5);
pDC->SelectObject(oldfont);
font.DeleteObject();
}
void Zong_zuo_biao(int x,int y,double num,CDC *pDC,myWin WinRect)
{
CFont font;
LOGFONT logfont;
memset(&logfont, 0, sizeof(LOGFONT));
logfont.lfWeight = 200;
logfont.lfWidth = WIDE/8;
logfont.lfHeight = WIDE/4;
strcpy(logfont.lfFaceName, "simsun");
VERIFY(font.CreateFontIndirect(&logfont));
pDC->SelectObject(&font);
pDC->SetTextColor(BLACK);
pDC->SetBkMode(TRANSPARENT);
char strnum[10];
sprintf(strnum, "%-.2lf", num);
pDC->TextOutA(x-WIDE/2,y-logfont.lfHeight/2,strnum,strlen(strnum));
font.DeleteObject();
}
void printline(CDC *pDC,myWin &WinRect,double max_y,double min_y,double origin_x,double origin_y,double&FACTOR)
{
int vert_move = 32;
int x_start = origin_x*0.9;
int x_end =origin_x + (Ncolumn)*WIDE;
int y_top = WinRect.height*(1-FACTOR)/3-60+vert_move;
int y_bottom =WinRect.height*FACTOR+ WinRect.height*(1-FACTOR)/3+60+vert_move;
pDC->MoveTo(x_start-8,origin_y);
pDC->LineTo(x_end,origin_y);
pDC->MoveTo(x_start-8,y_bottom-1);
pDC->LineTo(x_end,y_bottom-1);
pDC->MoveTo(x_start-8,y_bottom);
pDC->LineTo(x_end,y_bottom);
pDC->MoveTo(x_start-8,y_bottom+1);
pDC->LineTo(x_end,y_bottom+1);
pDC->MoveTo(x_start-2,origin_y);
pDC->LineTo(x_start-2,y_top);
pDC->LineTo(x_start-2,y_bottom);
pDC->MoveTo(x_start-1,origin_y);
pDC->LineTo(x_start-1,y_top);
pDC->LineTo(x_start-1,y_bottom);
pDC->MoveTo(x_start,origin_y);
pDC->LineTo(x_start,y_top);
pDC->LineTo(x_start,y_bottom);
pDC->MoveTo(x_start-8,y_top);
pDC->LineTo(x_start,y_top);
pDC->MoveTo(x_start-8,y_top+1);
pDC->LineTo(x_start,y_top+1);
pDC->MoveTo(x_start-8,y_top+2);
pDC->LineTo(x_start,y_top+2);
Zong_zuo_biao(x_start-WIDE/2,(int)origin_y,0.0,pDC,WinRect);
Zong_zuo_biao(x_start-WIDE/2,y_top,max_y,pDC,WinRect);
Zong_zuo_biao(x_start-WIDE/2,y_bottom,-min_y,pDC,WinRect);
}
void InterFun(CDC *pDC, myWin& WinRect, char *path, double FACTOR)
{
FILE *fp_xishu = open_file(path,"r");
if (NULL == fp_xishu)
{
pDC->TextOutA(WinRect.width / 2 - 20,WinRect.height / 2, "Open File Failed!", strlen("Open File Failed!"));
return;
}
int feature = 0;
int res=fscanf(fp_xishu,"%d",&Ncolumn);
if (-1==res)
{
pDC->TextOutA(WinRect.width / 2 - 60, WinRect.height / 2, "文件第一个数据应该是int型,代表肽链长度。", strlen("文件第一个数据应该是int型,代表肽链长度。"));
return;
}
res=fscanf(fp_xishu,"%d",&feature);
if (-1 == res)
{
pDC->TextOutA(WinRect.width / 2 - 60, WinRect.height / 2, "文件第二个数据应该是int型,代表编码长度。", strlen("文件第一个数据应该是int型,代表肽链长度。"));
return;
}
int count = 0;
double tem;
while (!feof(fp_xishu))
{
res=fscanf(fp_xishu, "%lf", &tem);
if (-1 == res&& !feof(fp_xishu))
{
char str[20];
char *ss = "row:";
sprintf(str, "%d", count);
pDC->TextOutA(WinRect.width / 2 - 40, WinRect.height / 2, " 数据应该是double型。", strlen(" 数据应该是double型。"));
pDC->TextOutA(WinRect.width / 2 - 40, WinRect.height / 2+20, str, strlen(str));
return;
}
count++;
}
count--;
if (count!=Ncolumn*feature)
{
char str[20];
char *ss = "count:";
sprintf(str, "%s%d",ss, count);
pDC->TextOutA(WinRect.width / 2 - 40, WinRect.height / 2 + 20, str, strlen(str));
pDC->TextOutA(WinRect.width / 2 - 30, WinRect.height / 2, " 数据个数应该等于肽链长度乘以编码长度。", strlen(" 数据个数应该等于肽链长度乘以编码长度。"));
return;
}
rewind(fp_xishu);
fscanf(fp_xishu, "%d", &tem);
fscanf(fp_xishu, "%d", &tem);
feature+=6;
struct MyChar* MyChar = new struct MyChar[feature];
double **xishu = new double *[Ncolumn];
for (int i=0; i<Ncolumn; i++)
{
xishu[i] = new double[feature];
}
double fabsum=0;
double sum=0;
double max1=0;
double max2=0;
for(int i=0;i<Ncolumn;i++)
{
if ((fabsum+sum)/2>=max1) max1=(fabsum+sum)/2;
if ((fabsum-sum)/2>=max2) max2=(fabsum-sum)/2;
fabsum=0;
sum=0;
for(int n=0;n<feature;n++)
{
if( n==1||n==9||n==14||n==20||n==23||n==25)
{
xishu[i][n] = 0.0;
}
else
{
fscanf(fp_xishu,"%lf",&xishu[i][n]);
}
fabsum=fabsum+fabs(xishu[i][n]);
sum=sum+xishu[i][n];
}
}
fclose(fp_xishu);
fp_xishu = NULL;
double origin_y = WinRect.height*max1/(max1+max2)*FACTOR+WinRect.height*(1-FACTOR)/3;
double origin_x = WIDE*1.5;
for(int i=0;i<Ncolumn;i++)
{
for(int n=0;n<feature;n++)
{
if(n!=26)
{
MyChar[n].mychar[0]='A'+n;
MyChar[n].mycolor=color[n];
MyChar[n].myheight = (xishu[i][n]/(max1+max2))*WinRect.height*FACTOR;
}
else
{
MyChar[n].mychar[0]='*';
MyChar[n].mycolor=color[n];
MyChar[n].myheight = (xishu[i][n]/(max1+max2))*WinRect.height*FACTOR;
}
}
Sort(MyChar,feature);
Paint_column(MyChar,i,pDC,WinRect,origin_x,origin_y,feature);
Heng_zuo_biao(pDC,i,WinRect,origin_x, FACTOR);
}
printline(pDC,WinRect,max1,max2,origin_x,origin_y,FACTOR);
for (int i=0; i<Ncolumn; i++)
{
delete []xishu[i];
xishu[i] = NULL;
}
delete []xishu;
xishu = NULL;
delete []MyChar;
MyChar = NULL;
}