怎样修改这个程序的背景图???求大神帮忙。。。

wqh3520 2015-05-18 09:34:54
#include<windows.h>
#include<tchar.h>
BOOLEAN InitWindowClass(HINSTANCE hInstance, int nCmdShow);
LRESULT WINAPI WndProc(HWND,UINT,UINT,LONG);
HFONT CreateFont(HDC hDC, int nCharHeight, BOOL bItalic);

HBITMAP hbitmap;
hbitmap=(HBITMAP)LoadImage(AfxGetInstanceHandle(),"图片.bmp",IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE|LR_CREATEDIBSECTION);
//主函数
//主函数
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
MSG Message;
if(!InitWindowClass(hInstance, nCmdShow))
{
MessageBox(NULL, _T("创建窗口失败!"), _T("创建窗口"),NULL);
return 1;
}
while(GetMessage(&Message,0,0,0)) //消息循环
{
TranslateMessage(&Message);
DispatchMessage(&Message);
}
return (int)Message.wParam;
}

//消息处理函数
long WINAPI WndProc(HWND hWnd,UINT message,UINT wParam,LONG lParam)
{
HDC hDC;
HFONT hF;
PAINTSTRUCT ps; //定义指向包含绘图信息的结构体变量
wchar_t str[] = L" 你好!欢迎学习VC ";//" Hello VC ";
int i = 0;
static int x[11], y[11];
static int color[11];
POINT pt;
switch(message) //处理消息
{
case WM_CREATE:
SetTimer(hWnd,1111,200,NULL);
GetCursorPos(&pt);
ScreenToClient(hWnd, &pt); //将屏幕坐标转换为窗口坐标
for(i = 0; i < 11; i++)
{
x[i] = pt.x + (i-1)*40;
y[i] = pt.y;
color[i] = 25 * (i-1);
}
break;
case WM_PAINT: //处理重画消息
hDC=BeginPaint(hWnd,&ps);
hF = CreateFont(hDC,40,0);
SelectObject(hDC,hF);
for(i = 10; i > 1; i--)
{
x[i] = x[i-1] + 40;
y[i] = y[i-1];
}
GetCursorPos(&pt);
ScreenToClient(hWnd, &pt); //将屏幕坐标转换为窗口坐标

x[1] = pt.x;
y[1] = pt.y;
for(i = 1; i < 11; i++)
{
SetTextColor(hDC,RGB(255-color[i],color[i],255));
TextOutW(hDC,x[i],y[i],&str[i],1);
}
color[1] = color[10];
for(i = 10; i > 1; i--)
color[i] = color[i-1];
DeleteObject(hF);
EndPaint(hWnd,&ps);
break;
case WM_TIMER:
if(wParam==1111)
InvalidateRect(hWnd, NULL,1);
break;
case WM_DESTROY: //处理结束应用程序消息
KillTimer(hWnd,1);
PostQuitMessage(0);//结束应用程序
break;
default://其他消息处理程序
return(DefWindowProc(hWnd, message, wParam, lParam)) ;
}
return 0;
}

BOOLEAN InitWindowClass(HINSTANCE hInstance, int nCmdShow) //定义窗口类
{
WNDCLASSEX wcex;
HWND hWnd;
TCHAR *szWindowClass = _T("窗口示例");
TCHAR *szTitle = _T("鼠标的应用示例");
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = 0; //窗口类型为缺省类型
wcex.lpfnWndProc = WndProc ; //窗口处理函数为WndProc
wcex.cbClsExtra = 0 ; //窗口类无扩展
wcex.cbWndExtra = 0 ; //窗口实例无扩展
wcex.hInstance = hInstance ; //当前实例句柄
wcex.hIcon = LoadIcon( hInstance, MAKEINTRESOURCE(IDI_APPLICATION) ) ;
//窗口的最小化图标为缺省图标
wcex.hCursor = LoadCursor( NULL, IDC_ARROW) ;
//窗口采用箭头光标
wcex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH) ;
//窗口背景为白色
wcex.lpszMenuName = NULL ; //窗口中无菜单
wcex.lpszClassName = szWindowClass ;//窗口类名为"窗口示例"
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION) ) ;
if( !RegisterClassEx( &wcex)) //如果注册失败则返回
return FALSE ;
hWnd=CreateWindow(szWindowClass, //生成窗口
szTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
0,
CW_USEDEFAULT,
0,
NULL,
NULL,
hInstance,
NULL);
if(!hWnd)
return FALSE;
ShowWindow(hWnd,nCmdShow); //显示窗口
UpdateWindow(hWnd);
return TRUE;
}

HFONT CreateFont(HDC hDC, int nCharHeight, BOOL bItalic)
{
HFONT hFont;
hFont = CreateFont(
nCharHeight,
0,
0,
0,
400,
bItalic,
0,
0,
ANSI_CHARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY,
DEFAULT_PITCH|FF_DONTCARE,
"Arial");
return hFont;
}
...全文
175 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
Eleven 2015-05-19
  • 打赏
  • 举报
回复
WM_PAINT消息中LoadBitmap()/LoadImage()加载图片,然后BitBlt/StretchBlt()贴图。
zgl7903 2015-05-19
  • 打赏
  • 举报
回复
添加一个BMP位图资源 IDB_BKBMP

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
  switch (message) 
  {
    case(WM_ERASEBKGND):
      {
        //client rect
        RECT rcRect;
        GetClientRect(hWnd, &rcRect);
        
        //dest dc
        HDC hdc = (HDC) wParam; // handle to device context 

        //load bitmap
        HBITMAP hBmp = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BKBMP));

        //create memory dc
        HDC hMemDC = CreateCompatibleDC(hdc);

        //select bitmap
        HBITMAP hBmpOld = (HBITMAP)SelectObject(hMemDC, hBmp);
        BITMAP bmInfo;
        GetObject(hBmp, sizeof(bmInfo), &bmInfo);
        
        //fit the destination rectangle
        SetStretchBltMode(hdc, HALFTONE);
        StretchBlt(hdc,  //dest dc
          rcRect.left, rcRect.top, //dest x, y
          rcRect.right-rcRect.left, rcRect.bottom - rcRect.top, //dest dx, dy
          hMemDC, //src dc
          0, 0, //src x,y
          bmInfo.bmWidth, bmInfo.bmHeight, //src dx,dy
          SRCCOPY); //copy mode

        //clear
        SelectObject(hMemDC, hBmpOld);
        DeleteObject(hMemDC);
        DeleteObject(hBmp);

        return TRUE;
      }
wqh3520 2015-05-19
  • 打赏
  • 举报
回复
能帮忙在代码中加一下吗,,我才接触vc不久。。。
wqh3520 2015-05-19
  • 打赏
  • 举报
回复
引用 9 楼 zgl7903 的回复:
位图正常吗? 是纯白色的? 涂黑看看
我刚刚接触VC不久,,好多东西还不清楚 你能帮我弄一下 把修改背景之后的代码给我看看 学习一下
zgl7903 2015-05-19
  • 打赏
  • 举报
回复
位图正常吗? 是纯白色的? 涂黑看看
wqh3520 2015-05-19
  • 打赏
  • 举报
回复
引用 7 楼 zgl7903 的回复:
贴完整的代码出来
那就是完整的代码啊
zgl7903 2015-05-19
  • 打赏
  • 举报
回复
贴完整的代码出来
zgl7903 2015-05-19
  • 打赏
  • 举报
回复
CreatePatternBrush 创建位图画刷 修改 wcex.hbrBackground 或 添加 WM_ERASEBKGND 贴图
wqh3520 2015-05-19
  • 打赏
  • 举报
回复
引用 3 楼 zgl7903 的回复:
添加一个BMP位图资源 IDB_BKBMP

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
  switch (message) 
  {
    case(WM_ERASEBKGND):
      {
        //client rect
        RECT rcRect;
        GetClientRect(hWnd, &rcRect);
        
        //dest dc
        HDC hdc = (HDC) wParam; // handle to device context 

        //load bitmap
        HBITMAP hBmp = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BKBMP));

        //create memory dc
        HDC hMemDC = CreateCompatibleDC(hdc);

        //select bitmap
        HBITMAP hBmpOld = (HBITMAP)SelectObject(hMemDC, hBmp);
        BITMAP bmInfo;
        GetObject(hBmp, sizeof(bmInfo), &bmInfo);
        
        //fit the destination rectangle
        SetStretchBltMode(hdc, HALFTONE);
        StretchBlt(hdc,  //dest dc
          rcRect.left, rcRect.top, //dest x, y
          rcRect.right-rcRect.left, rcRect.bottom - rcRect.top, //dest dx, dy
          hMemDC, //src dc
          0, 0, //src x,y
          bmInfo.bmWidth, bmInfo.bmHeight, //src dx,dy
          SRCCOPY); //copy mode

        //clear
        SelectObject(hMemDC, hBmpOld);
        DeleteObject(hMemDC);
        DeleteObject(hBmp);

        return TRUE;
      }
为什么背景还是白色的
wqh3520 2015-05-19
  • 打赏
  • 举报
回复
引用 3 楼 zgl7903 的回复:
添加一个BMP位图资源 IDB_BKBMP

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
  switch (message) 
  {
    case(WM_ERASEBKGND):
      {
        //client rect
        RECT rcRect;
        GetClientRect(hWnd, &rcRect);
        
        //dest dc
        HDC hdc = (HDC) wParam; // handle to device context 

        //load bitmap
        HBITMAP hBmp = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BKBMP));

        //create memory dc
        HDC hMemDC = CreateCompatibleDC(hdc);

        //select bitmap
        HBITMAP hBmpOld = (HBITMAP)SelectObject(hMemDC, hBmp);
        BITMAP bmInfo;
        GetObject(hBmp, sizeof(bmInfo), &bmInfo);
        
        //fit the destination rectangle
        SetStretchBltMode(hdc, HALFTONE);
        StretchBlt(hdc,  //dest dc
          rcRect.left, rcRect.top, //dest x, y
          rcRect.right-rcRect.left, rcRect.bottom - rcRect.top, //dest dx, dy
          hMemDC, //src dc
          0, 0, //src x,y
          bmInfo.bmWidth, bmInfo.bmHeight, //src dx,dy
          SRCCOPY); //copy mode

        //clear
        SelectObject(hMemDC, hBmpOld);
        DeleteObject(hMemDC);
        DeleteObject(hBmp);

        return TRUE;
      }
这是MFC的吧???

16,505

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧