鱼C论坛

 找回密码
 立即注册
查看: 2860|回复: 2

一个程序·

[复制链接]
发表于 2017-10-30 19:12:39 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
求一个出现图片的C++程序
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-10-30 21:23:53 | 显示全部楼层
如果想要界面显示的话请看MFC或这Windows SDK 编程
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-10-31 10:02:03 | 显示全部楼层
楼主是要显示图片的意思吗?这里有一个Windows加载BMP文件的Demo,供参考。
#include <Windows.h>
#include <Commdlg.h>

//先声明一下消息处理函数
LRESULT CALLBACK MyWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);

//重新设定windows窗口客户区域大小
void ResetClientSize(HWND hWnd,int width, int height)
{
        RECT rectProgram,rectClient;
        GetWindowRect(hWnd,&rectProgram);
        GetClientRect(hWnd,&rectClient);
        int nWidth = rectProgram.right -rectProgram.left - (rectClient.right -rectClient.left); 
        int nHeiht = rectProgram.bottom -rectProgram.top - (rectClient.bottom -rectClient.top); 
        nWidth += width;
        nHeiht += height;
        rectProgram.right = nWidth;
        rectProgram.bottom=  nHeiht;
        int showToScreenx =GetSystemMetrics(SM_CXSCREEN)/2-nWidth/2;
        int showToScreeny =GetSystemMetrics(SM_CYSCREEN)/2-nHeiht/2;
        MoveWindow(hWnd, showToScreenx,showToScreeny, rectProgram.right, rectProgram.bottom, false);
        InvalidateRect(hWnd,NULL,TRUE);
}

// 入口点
int CALLBACK WinMain(
                                         HINSTANCE hInstance,
                                         HINSTANCE hPrvInstance,
                                         LPSTR lpCommandLine,
                                         int cmdShow)
{
        char* clsName = "MyApp";
        WNDCLASS wc;
        ZeroMemory(&wc,sizeof(WNDCLASS));
        wc.hInstance = hInstance;
        wc.lpszClassName = clsName;
        wc.lpfnWndProc = MyWindowProc;
        wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
        wc.style = CS_HREDRAW | CS_VREDRAW; 
        RegisterClass(&wc);
        HWND hMainwind = CreateWindow(clsName,"显示BMP图片",WS_OVERLAPPEDWINDOW,
                300,200,100,100,NULL,NULL,hInstance,NULL);
        if(hMainwind == NULL)
                return 0;

        OPENFILENAME ofn;
        char szFile[MAX_PATH];
        ZeroMemory(&ofn,sizeof(ofn));
        ofn.lStructSize = sizeof(ofn);
        ofn.lpstrFile = szFile;
        ofn.lpstrFile[0] = TEXT('\0');
        ofn.nMaxFile = sizeof(szFile);
        ofn.lpstrFilter= TEXT("bmp\0*.bmp\0");
        ofn.nFilterIndex = 0;
        ofn.lpstrFileTitle = NULL;
        ofn.nMaxFileTitle = 0;
        ofn.lpstrInitialDir = NULL;
        ofn.lpstrTitle=TEXT("请选择一个BMP文件");
        ofn.hwndOwner = hMainwind;
        ofn.Flags = OFN_EXPLORER |OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
        if (GetOpenFileName(&ofn))
        {
                HBITMAP hBitmap = (HBITMAP)LoadImage(NULL, szFile, IMAGE_BITMAP, 
                        0, 0, LR_LOADFROMFILE);
                BITMAP bmp;
                ZeroMemory(&bmp,sizeof(bmp));
                GetObject(hBitmap, sizeof(BITMAP), &bmp);
                ResetClientSize(hMainwind,bmp.bmWidth,bmp.bmHeight);

                ShowWindow(hMainwind,SW_NORMAL);
                UpdateWindow(hMainwind);
                
                MSG msg;
                GetMessage(&msg,NULL,0,0);
                while(msg.message != WM_QUIT)
                {
                        if(PeekMessage( &msg,NULL,0,0,PM_REMOVE))
                        {
                                TranslateMessage(&msg);
                                DispatchMessage(&msg);
                        }
                        else
                        {
                                HDC hDC = GetDC(hMainwind);
                                HBRUSH hBrush = CreatePatternBrush(hBitmap);
                                HBRUSH hOldBrush = (HBRUSH)SelectObject(hDC, hBrush);
                                Rectangle(hDC, 0, 0, bmp.bmWidth,bmp.bmHeight);
                                SelectObject(hDC, hOldBrush);
                                DeleteObject(hOldBrush);
                                ReleaseDC(hMainwind,hDC);
                        }
                }
                DeleteObject(hBitmap);
        }

        return 0;
}

LRESULT CALLBACK MyWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
        switch(msg)
        {
        case WM_DESTROY:
                PostQuitMessage(0);//退出程序
                return 0;
        default:
                break;
        }
        return DefWindowProc(hwnd,msg,wParam,lParam);
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-30 23:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表