Win32编程绘图实例--字母图

软件截图

这里写图片描述

下面是win32 界面程序代码


#define WIN32_LEAN_AND_MEAN       //  从 Windows 头文件中排除极少使用的信息

//#include <windows.h>// 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, 窗口类结构体
    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"),   //LPCTSTR   lpClassName,
        _T("字母图"),          //LPCTSTR   lpWindowName,
        WS_OVERLAPPEDWINDOW,//DWORD     dwStyle,
        0,              // int       x,
        0,              //int       y,
        width,              //int       nWidth,
        height - 40,                //int       nHeight,
        NULL,           //HWND      hWndParent,
        NULL,           //HMENU     hMenu,
        hInstance,      //HINSTANCE hInstance,
        NULL);          //LPVOID    lpParam
    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;  //  创建工具栏窗口
    //TOOLBARCLASSNAME
    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/*ILC_COLOR16 | ILC_MASK*/, 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);   //将位图添加到工具栏

//  TCHAR *szBitMAp[] = { "open", "draw", "help" };//标题
    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[j].iString = (INT_PTR)szBitMAp[j];//标题
        //tButton[j].idCommand = j+100;//命令
    }
    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_SETBUTTONSIZE, 0, (LPARAM)MAKELONG(10, 10));  
    SendMessage(hToolBar, TB_AUTOSIZE, 0, 0);    //调整工具栏大小
    //https://msdn.microsoft.com/en-us/library/windows/desktop/bb787421(v=vs.85).aspx
    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);//如果用户指定了一个文件名且点击OK按钮,返回值为非零值。如果用户取消或关闭Save对话框或错误出现,返回值为零。

    if (FilePathFlag)  //打开文件成功
    {
        //std::vector<wstring> vctString(1, FilePath);//保存添加文件的路径
        //MessageBox(hWnd, FilePath, _T("successful"), MB_OK);//"LPCWSTR
        TextOut(GetDC(hWnd), 10, 45, FilePath, strlen(FilePath));
    }
    else
    {
        DWORD dwError = NOERROR;
        dwError = CommDlgExtendedError();
        MessageBox(hWnd, _T("open file failed!"), _T("tips"), MB_OK);//"LPCWSTR
    }
    return TRUE;
}

BOOL MyDraw(HDC hdc)
{
    if (!FilePathFlag)  return FALSE;

    CDC *pDC = CDC::FromHandle(hdc);
    CWnd *pWnd = pDC->GetWindow();

    RECT rc;
    pWnd->GetClientRect(&rc);//填充窗口大小结构体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,                      // nHeight
                    0,                         // nWidth
                    0,                         // nEscapement
                    0,                         // nOrientation
                    FW_NORMAL,                 // nWeight
                    FALSE,                     // bItalic
                    FALSE,                     // bUnderline
                    0,                         // cStrikeOut
                    ANSI_CHARSET,              // nCharSet
                    OUT_DEFAULT_PRECIS,        // nOutPrecision
                    CLIP_DEFAULT_PRECIS,       // nClipPrecision
                    DEFAULT_QUALITY,           // nQuality
                    DEFAULT_PITCH | FF_SWISS,  // nPitchAndFamily
                    _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://菜单命令int
            wmId = LOWORD(wParam);
            wmEvent = HIWORD(wParam);
            switch (wmId)// 分析菜单命令: 
            {
                case ID_FILE_SAVE://截图
                {

                    //TCHAR CmdLine[] = TEXT(buffer);
                       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);//"LPCWSTR
                        MessageBox(hWnd, _T(NowPath), _T("path"), MB_OK);//"LPCWSTR
                    }

                    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);//"LPCWSTR
                    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);//"LPCWSTR
                    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);//"LPCWSTR
                    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);//"LPCWSTR
            break;
        case WM_CREATE: // creating main window 
        {


            RECT lRect;
            GetClientRect(hToolBar, &lRect);
            FillRect(GetDC(hToolBar), &lRect, GetSysColorBrush(COLOR_3DDKSHADOW));
            //CWnd * pCWnd;
            //pCWnd = CWnd::FromHandle(hWnd);
            //btn.Create(_T("btnName"), WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON, CRect(0, 0, 90, 20), pCWnd, 123);
            //btn.ShowWindow(SW_SHOWNORMAL);
            break;
        }


        case WM_PAINT:
            HDC hdc;
            hdc = BeginPaint(hWnd, &ps);
            // TODO:  在此添加任意绘图代码...

            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 Ncolumn 28//读入数据列数

//#define FACTOR   0.6//值越大则高度越高
#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
    lf.lfWeight =500;//字符的粗细
    lf.lfWidth = WIDE;//字符的宽度
    lf.lfHeight = height;//字符的高度
    if(height>0)   lf.lfEscapement = 0;
         else         lf.lfEscapement=1800;    

    //char *tem = "@MS Gothic";
    //MultiByteToWideChar(CP_ACP, 0, tem, sizeof(tem)-1, lf.lfFaceName, sizeof(tem)-1);
    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;
        //BOOL ExtTextOut(  扩展文本输出函数
        //  _In_       HDC     hdc,
        //  _In_       int     X,
        //  _In_       int     Y,
        //  _In_       UINT    fuOptions,
        //  _In_ const RECT    *lprc,
        //  _In_       LPCTSTR lpString,
        //  _In_       UINT    cbCount,
        //  _In_ const INT     *lpDx
        //);
    /*  LONG    left;  rectangle
        LONG    top;
        LONG    right;
        LONG    bottom;*/
    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;//下一次绘图的yy


    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));  // create the font
    CFont* oldfont=pDC->SelectObject(&font);
    pDC->SetTextColor(BLACK);               // Set the text color
    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;//The LOGFONT structure defines the attributes of a font.
    memset(&logfont, 0, sizeof(LOGFONT)); 
    logfont.lfWeight = 200; //For example, 400 is normal and 700 is bold.
    logfont.lfWidth = WIDE/8;
    logfont.lfHeight = WIDE/4;
    strcpy(logfont.lfFaceName, "simsun");//A null-terminated string that specifies the typeface name of the font. 

    VERIFY(font.CreateFontIndirect(&logfont));  // create the font
    pDC->SelectObject(&font);
    pDC->SetTextColor(BLACK);               // Set the text color
    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);    //将当前位置移动到 x 和 y 指定的点(或 point)      
    pDC->LineTo(x_end,origin_y);

//底部很坐标线 包含(底部纵坐标刻度(小横线))
    //y_bottom+=60;
    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);  //max 刻度

    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;  //每个编码方式的属性数,比如稀疏编码,21个属性+6个。

    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);   //编码长度,只能是20 或者21
    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]; // 创建9个二级指针,
    for (int i=0; i<Ncolumn; i++)
     {
        xishu[i] = new double[feature];   //int feature = 27;  
     }  
    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++)  //一个循环读取20或者21 个数据
        {
            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]);   //xishu[i][n]  9*27
            }
            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);//P 1 P2 那行

    }

    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;
}

猜你喜欢

转载自blog.csdn.net/sinat_36219858/article/details/80311585
今日推荐