如何在窗口贴图
#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) ;
}
我想将一张关闭按钮的图片放在窗口关闭按钮那,可为什么贴出来的效果是这样的?