| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1940 人关注过本帖
标题:如何在窗口贴图
只看楼主 加入收藏
小白0号
Rank: 1
等 级:新手上路
帖 子:13
专家分:7
注 册:2018-1-4
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:2 
如何在窗口贴图
#include <windows.h>
#include"resource.h"

HINSTANCE    apphInstance;

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
{
     static char szWndClassName[] = "hellowin";
     HWND         hwnd ;
     MSG          msg ;
     WNDCLASS     wndclass ;

     wndclass.style         = CS_HREDRAW|CS_VREDRAW ;
     wndclass.lpfnWndProc   = WndProc ;
     wndclass.cbClsExtra    = 0 ;
     wndclass.cbWndExtra    = 0 ;
     wndclass.hInstance     = hInstance ;
     wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;//图标
     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;//光标
     wndclass.hbrBackground = (HBRUSH)CreateSolidBrush(RGB(238,232,170)) ;
     wndclass.lpszMenuName  = NULL ;
     wndclass.lpszClassName = szWndClassName ;
     apphInstance=hInstance;//获得程序句柄

     if (!RegisterClass (&wndclass))
     {
          MessageBox (NULL, "注册失败",
                      "错误", MB_ICONERROR) ;
          return 0 ;
     }
     
     hwnd = CreateWindow (szWndClassName,             // window class name
                          "香蕉地",                      // window caption
                          WS_OVERLAPPEDWINDOW,        // window style
                          CW_USEDEFAULT,              // initial x position
                          CW_USEDEFAULT,              // initial y position
                          CW_USEDEFAULT,              // initial x size
                          CW_USEDEFAULT,              // initial y size
                          NULL,                       // parent window handle
                          NULL,                       // window menu handle
                          hInstance,                  // program instance handle
                          NULL) ;                     // creation parameters
     
     ShowWindow (hwnd, iCmdShow) ;
     UpdateWindow (hwnd) ;
     
     while (GetMessage (&msg, NULL, 0, 0))  //消息队列
     {
          TranslateMessage (&msg) ;
          DispatchMessage (&msg) ;//将信息传递给操作系统,操作系统传给窗口过程,并停在这,直到窗口过程响应完成
     }
     return msg.wParam ;  //WM_QUIT
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
     HDC           hdc;
     PAINTSTRUCT   ps;
     RECT          rect;
     RECT capRect;
     HBRUSH        hb;
     int SIZE_BORDER=5;
     int SIZE_CAPTION=30;
     HICON         hIcon;
     HDC          hMemDC;
     HBITMAP     hBitmap;
     long        bitmapW;
     long        bitmapH;
     static RECT WindowRect;
     static BITMAP bitmap;
    // HINSTANCE hInstance;

     switch (message)
     {
      case WM_NCPAINT://处理自绘窗口边框和标题栏时的消息
          hdc=GetWindowDC(hwnd);//获得窗口句柄
          //绘制窗体
          GetWindowRect(hwnd,&WindowRect);//函数返回指定窗口的边框矩形的尺寸,该尺寸以相对于屏幕坐标左上角的屏幕坐标给出。
          OffsetRect(&WindowRect,-WindowRect.left,-WindowRect.top);

          ExcludeClipRect(hdc,SIZE_BORDER,SIZE_CAPTION,WindowRect.right-SIZE_BORDER,WindowRect.bottom-SIZE_BORDER);
          hb=CreateSolidBrush(RGB(150,200,110));
          FillRect(hdc,&WindowRect,hb);
          DeleteObject(hb);//释放资源
          //图标
          hIcon=LoadIcon(apphInstance,MAKEINTRESOURCE(IDI_ICON1));
          DrawIconEx(hdc,SIZE_BORDER,(SIZE_CAPTION-GetSystemMetrics(SM_CYSMICON))/2,hIcon,
                    GetSystemMetrics(SM_CXSMICON),GetSystemMetrics(SM_CYSMICON),0,0,DI_NORMAL);
          //标题
          capRect.left=SIZE_BORDER+GetSystemMetrics(SM_CXSMICON)+5;
          capRect.right=capRect.left+60;
          capRect.top=(SIZE_CAPTION-GetSystemMetrics(SM_CYSMICON))/2;
          capRect.bottom=(SIZE_CAPTION+GetSystemMetrics(SM_CYSMICON))/2;
          SetBkMode(hdc,TRANSPARENT);
          SetTextColor(hdc,RGB(255,255,0));
          DrawText(hdc,"我的程序",-1,&capRect,DT_SINGLELINE|DT_CENTER);//向窗口传递文字
         //关闭按钮
          hMemDC=CreateCompatibleDC(hdc);//创建与窗口兼容的内存DC
          hBitmap=LoadBitmap(apphInstance,MAKEINTRESOURCE(IDB_BITMAP2));//载入位图

          GetObject(hBitmap,sizeof(BITMAP),&bitmap);//把hBitmap的信息存入bitmap中
          bitmapW=bitmap.bmWidth;
          bitmapH=bitmap.bmHeight;
          SelectObject(hMemDC,hBitmap);//将位图载入内存DC中
          BitBlt(hdc,WindowRect.right-SIZE_BORDER-bitmapW,(SIZE_CAPTION-bitmapH)/2,bitmapW,bitmapH,hMemDC,0,0,SRCCOPY);
          DeleteObject(hBitmap);
          DeleteDC(hMemDC)
;


          ReleaseDC(hwnd,hdc);
          return 0;
      case WM_PAINT://窗口必须重绘的时候
          hdc = BeginPaint (hwnd, &ps) ;//返回设备描述表句柄

          GetClientRect (hwnd, &rect) ;//函数返回客户区的边框矩形的尺寸
          SetBkMode(hdc,TRANSPARENT);
          DrawText (hdc, TEXT ("编程可以这样学"), -1, &rect,
                    DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
         
          EndPaint (hwnd, &ps) ;
          return 0 ;
     case WM_DESTROY://程序关闭的时候
          PostQuitMessage (0) ;
          return 0 ;
     }
     return DefWindowProc (hwnd, message, wParam, lParam) ;
}
我想将一张关闭按钮的图片放在窗口关闭按钮那,可为什么贴出来的效果是这样的?
搜索更多相关主题的帖子: 窗口 HWND NULL return BITMAP 
2018-01-28 12:11
小白0号
Rank: 1
等 级:新手上路
帖 子:13
专家分:7
注 册:2018-1-4
收藏
得分:0 
图片附件: 游客没有浏览图片的权限,请 登录注册
2018-01-28 12:12
吹水佬
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:451
帖 子:10607
专家分:43186
注 册:2014-5-20
收藏
得分:7 
缺少文件,测试不了。
2018-01-28 16:29
快速回复:如何在窗口贴图
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019786 second(s), 11 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved