载入位图,逻辑坐标等14.4.21

// Zuoye1.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "resource.h"
HINSTANCE g_hInst = NULL;


void DrawBmp(HDC hDC){
	//第一个参数是 位图资源所在程序句柄
	//第二个参数位图的名称
	//返回一个资源句柄
	HBITMAP hBmp=LoadBitmap(g_hInst,MAKEINTRESOURCE(IDB_BITMAP01) );
	//获取位图的相关信息
	BITMAP bmpinfo={0};
	GetObject(hBmp,sizeof(bmpinfo),&bmpinfo);
	//创建一个hDC一模一样的DC出来
	//现在hBmpDC和hDC一模一样的DC出来
	HDC hBmpDC=CreateCompatibleDC(hDC);
	HBITMAP hOldBit = (HBITMAP)SelectObject(hBmpDC,hBmp);
	//然后把复制出来的DC 在绘制到我们当前DC上面 呈现出来
	BitBlt(hDC,100,100,bmpinfo.bmWidth,bmpinfo.bmHeight,hBmpDC,0,0,SRCCOPY);

	StretchBlt(hDC,200,100,500,500,hBmpDC,20,20,bmpinfo.bmWidth,bmpinfo.bmHeight,
		SRCCOPY);
	SelectObject(hBmpDC,hOldBit);
	
	
	DeleteDC(hBmpDC);
	DeleteObject(hBmp);

}
void onPaint(HWND hWnd,UINT nMsg,WPARAM wParam,LPARAM lParam){
	PAINTSTRUCT ps={0};
	HDC hDC=BeginPaint(hWnd,&ps);
	//DrawBmp(hDC);
	/*switch(g_nDreawType){
	//case ID_SETPIXEL:
	//	DrawPixel(hDC);
	//	break;
	case ID_LOADBIT:
		
		break;
	}
	*/
	DrawBmp(hDC);
	EndPaint(hWnd,&ps);

}
void onCreate(HWND hWnd,UINT nMsg,WPARAM wParam,LPARAM lParam){
			HMENU hMenu=LoadMenu(g_hInst,MAKEINTRESOURCE(IDR_MENU1));
			SetMenu(hWnd,hMenu);

}

void onCommand(HWND hWnd,UINT nMsg,WPARAM wParam,LPARAM lParam){
			
/*	int nCmdID=LOWORD(wParam);
	switch(nCmdID){
	case ID_EXIT:
		PostQuitMessage(0);
		break;
	default:
		g_nDreawType=nCmdID;
		InvalidateRect(hWnd,NULL,true);
	//绘制一条直线

	}
	*/

}
//主窗口的窗口处理函数
LRESULT CALLBACK WndProc( HWND hWnd,
						   UINT nMsg,
						   WPARAM wParam,
						   LPARAM lParam )
{
	switch( nMsg )
	{
	case WM_PAINT:
		onPaint(hWnd, nMsg, wParam, lParam);
		break;
	case WM_DESTROY:
		PostQuitMessage( 0 );
		return 0;
		//KillTimer(hWnd,1002);
	/*case WM_COMMAND:
		//onCommand(hWnd, nMsg, wParam, lParam);
		break;
	case WM_CREATE:
		//onCreate(hWnd, nMsg, wParam, lParam);
	break;
	*/
	//传递到子窗体处理消息响应
	//return DefFrameProc( hWnd,g_hMDIClient, nMsg, wParam, lParam );
	//传递给系统
	}
	return DefWindowProc( hWnd, nMsg, wParam, lParam );
}
//窗口注册函数
BOOL RegisterWnd( LPSTR   pszClassName,int    nBrush )
{
	WNDCLASSEX wce = { 0 };
	wce.cbSize     = sizeof( wce );
	wce.style      = CS_HREDRAW|CS_VREDRAW;
	wce.cbClsExtra = 0;
	wce.cbWndExtra = 0;
	wce.lpfnWndProc= WndProc;
	wce.hInstance  = g_hInst;
	wce.hCursor    = NULL;
	wce.hIcon      = NULL;
	wce.hbrBackground = HBRUSH(nBrush);
	wce.lpszClassName = pszClassName;
	wce.lpszMenuName  = NULL;
	wce.hIconSm       = NULL;

	ATOM nAtom = RegisterClassEx( &wce );
	if( nAtom == 0 )
	{
		return FALSE;
	}
	return TRUE;
}
//创建主窗口
HWND CreateWnd( LPSTR pszClassName )
{
	HMENU hMenu=LoadMenu(g_hInst,MAKEINTRESOURCE(IDR_MENU1));
	HWND hWnd = CreateWindowEx( 0,
		pszClassName, "MainWnd", 
		WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
		CW_USEDEFAULT, CW_USEDEFAULT,
		CW_USEDEFAULT, NULL, hMenu, g_hInst,
		NULL );
	return hWnd;
}
//消息循环
void Message( )
{
	MSG msg = { 0 };
	while( GetMessage( &msg, NULL, 0, 0 ) )
	{
		TranslateMessage( &msg );
		DispatchMessage( &msg );
	}
}
//显示窗口
void DisplayWnd( HWND hWnd )
{
	ShowWindow( hWnd, SW_SHOW );
	UpdateWindow( hWnd );
}
int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
 	// TODO: Place code here.
	g_hInst = hInstance;
	RegisterWnd("luoqiang",COLOR_BACKGROUND);
	DisplayWnd(CreateWnd("luoqiang"));
	Message();
	return 0;
}


// Zuoye1.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
HINSTANCE g_hInst = NULL;

void onPaint(HWND hWnd,UINT nMsg,WPARAM wParam,LPARAM lParam){
	PAINTSTRUCT ps={0};
	HDC hDC=BeginPaint(hWnd,&ps);
	//Rectangle(hDC,100,100,200,200);
	//0.1MM为单位
	
	//Rectangle(hDC,100,-100,200,-200);
	//SetMapMode(hDC,nOldMap);
	//毫米为单位
	//nOldMap=SetMapMode(hDC,MM_HIMETRIC);
	//Rectangle(hDC,100,-100,200,-200);
	//恢复旧映射模式
	//让原本的图像 按照1:10的图像显示 反过来10:1也是可以的 现在就是相当于
	//宽是1逻辑单位对应2个像素
	int nOldMap=SetMapMode(hDC,MM_ISOTROPIC);
	SetViewportExtEx(hDC,10,10,NULL);
	SetWindowExtEx(hDC,5,5,NULL);
	Rectangle(hDC,100,100,200,200);

	SetMapMode(hDC,nOldMap);
	EndPaint(hWnd,&ps);

}


//主窗口的窗口处理函数
LRESULT CALLBACK WndProc( HWND hWnd,
						   UINT nMsg,
						   WPARAM wParam,
						   LPARAM lParam )
{
	switch( nMsg )
	{
	case WM_PAINT:
		onPaint(hWnd, nMsg, wParam, lParam);
		break;
	case WM_DESTROY:
		PostQuitMessage( 0 );
		return 0;
	}
	return DefWindowProc( hWnd, nMsg, wParam, lParam );
}
//窗口注册函数
BOOL RegisterWnd( LPSTR   pszClassName,int    nBrush )
{
	WNDCLASSEX wce = { 0 };
	wce.cbSize     = sizeof( wce );
	wce.style      = CS_HREDRAW|CS_VREDRAW;
	wce.cbClsExtra = 0;
	wce.cbWndExtra = 0;
	wce.lpfnWndProc= WndProc;
	wce.hInstance  = g_hInst;
	wce.hCursor    = NULL;
	wce.hIcon      = NULL;
	wce.hbrBackground = HBRUSH(nBrush);
	wce.lpszClassName = pszClassName;
	wce.lpszMenuName  = NULL;
	wce.hIconSm       = NULL;

	ATOM nAtom = RegisterClassEx( &wce );
	if( nAtom == 0 )
	{
		return FALSE;
	}
	return TRUE;
}
//创建主窗口
HWND CreateWnd( LPSTR pszClassName )
{

	HWND hWnd = CreateWindowEx( 0,
		pszClassName, "MainWnd", 
		WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
		CW_USEDEFAULT, CW_USEDEFAULT,
		CW_USEDEFAULT, NULL, NULL, g_hInst,
		NULL );
	return hWnd;
}
//消息循环
void Message( )
{
	MSG msg = { 0 };
	while( GetMessage( &msg, NULL, 0, 0 ) )
	{
		TranslateMessage( &msg );
		DispatchMessage( &msg );
	}
}
//显示窗口
void DisplayWnd( HWND hWnd )
{
	ShowWindow( hWnd, SW_SHOW );
	UpdateWindow( hWnd );
}
int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
 	// TODO: Place code here.
	g_hInst = hInstance;
	RegisterWnd("luoqiang",COLOR_BACKGROUND);
	DisplayWnd(CreateWnd("luoqiang"));
	Message();
	return 0;
}






你可能感兴趣的:(载入位图,逻辑坐标等14.4.21)