【UG\NX二次开发】结合Windows窗口开发-设置BMP图片

#include 
#include 
#include 

#include  
#include  
#include 
#include 
#undef CreateDialog	

#include "WindowsTest.hpp"
using namespace NXOpen;
using namespace NXOpen::BlockStyler;


Session *(WindowsTest::theSession) = NULL;
UI *(WindowsTest::theUI) = NULL;

//获取dll路径
string GetProgramPath()
{
	char filePath[MAX_PATH];
	GetModuleFileNameA(_AtlBaseModule.GetModuleInstance(), filePath, MAX_PATH);
	std::string strDllPath(filePath);
	strDllPath = strDllPath.substr(0, strDllPath.find_last_of("\\") + 1);
	return strDllPath;
}

HWND bmp_hWnd = NULL;
CString bmp_path = "";
//自定义消息处理函数
LRESULT CALLBACK __WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) 
{

	switch (msg)//处理所需的消息
	{
		case WM_CREATE://窗口创建的时候添加提示框;
			{
				//MessageBox(hWnd, _T("窗口被创建啦!"), _T("Tip"), MB_OK);
				break;
			}
			//case WM_CLOSE://窗口关闭的时候添加确认提示框,取消则不关闭窗口
			//{
			//	int iRet = MessageBox(hWnd, _T("要关闭窗口吗?"), _T("Tip"), MB_OKCANCEL);
			//	if (iRet == IDOK) return DefWindowProc(hWnd, msg, wParam, lParam);
			//	break;
			//}
			//case WM_SIZE://窗口被最大化的时候添加 MessageBox 提示框;
			//{
			//	if (wParam == SIZE_MAXIMIZED)
			//	{
			//		MessageBox(hWnd, _T("窗口被最大化啦!"), _T("Tip"), MB_OK);
			//	}
			//	break;
			//}
		case WM_PAINT://窗口绘制消息
			{
				BITMAP  bitmap;
				HBITMAP hBitmap = (HBITMAP)LoadImage(NULL, bmp_path, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
				GetObject(hBitmap, sizeof(bitmap), &bitmap);

				PAINTSTRUCT ps;
				HDC dc = BeginPaint(hWnd, &ps);

				HDC memdc = CreateCompatibleDC(dc);
				::SelectObject(memdc, hBitmap);
				RECT rc;
				GetClientRect(hWnd, &rc);

				int showWidth = rc.right - rc.left;
				int showHeight = rc.bottom - rc.top;
				SetStretchBltMode(dc, COLORONCOLOR);
				StretchBlt(dc, rc.left, rc.top, showWidth, showHeight, memdc, 0, 0, bitmap.bmWidth, bitmap.bmHeight, SRCCOPY);
				::ReleaseDC(hWnd, dc);
				DeleteDC(memdc);
				DeleteDC(dc);
				DeleteObject(hBitmap);
				EndPaint(hWnd, &ps);

				break;
			}
		case WM_DESTROY://当用户关闭窗口,窗口销毁,程序需结束
			{
				//PostQuitMessage(0);
				::DestroyWindow(bmp_hWnd);
				bmp_hWnd = NULL;
				break;
			}
		default://其他消息交给由系统提供的缺省处理函数
			return DefWindowProc(hWnd, msg, wParam, lParam);
			break;
	}
	return DefWindowProc(hWnd, msg, wParam, lParam);
}

WindowsTest::WindowsTest()
{
    try
    {
        // Initialize the NX Open C++ API environment
        WindowsTest::theSession = NXOpen::Session::GetSession();
        WindowsTest::theUI = UI::GetUI();

		string dllPath = GetProgramPath();
		//拼接好dlx的绝对路径
		theDlxFileName = dllPath.append("WindowsTest.dlx").c_str();
		theDialog = WindowsTest::theUI->CreateDialog(theDlxFileName);

        // Registration of callback functions
        theDialog->AddApplyHandler(make_callback(this, &WindowsTest::apply_cb));
        theDialog->AddOkHandler(make_callback(this, &WindowsTest::ok_cb));
        theDialog->AddUpdateHandler(make_callback(this, &WindowsTest::update_cb));
        theDialog->AddInitializeHandler(make_callback(this, &WindowsTest::initialize_cb));
        theDialog->AddDialogShownHandler(make_callback(this, &WindowsTest::dialogShown_cb));
    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        throw;
    }
}



WindowsTest::~WindowsTest()
{
	if (bmp_hWnd != NULL)//销毁bmp的窗口句柄
	{
		::DestroyWindow(bmp_hWnd);
		bmp_hWnd = NULL;
	}

    if (theDialog != NULL)
    {
        delete theDialog;
        theDialog = NULL;
    }
}



extern "C" DllExport void  ufusr(char *param, int *retcod, int param_len)
{
    WindowsTest *theWindowsTest = NULL;
    try
    {
        theWindowsTest = new WindowsTest();
        // The following method shows the dialog immediately
        theWindowsTest->Show();
    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        WindowsTest::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }
    if(theWindowsTest != NULL)
    {
        delete theWindowsTest;
        theWindowsTest = NULL;
    }
}



extern "C" DllExport int ufusr_ask_unload()
{
    //return (int)Session::LibraryUnloadOptionExplicitly;
    return (int)Session::LibraryUnloadOptionImmediately;
    //return (int)Session::LibraryUnloadOptionAtTermination;
}




extern "C" DllExport void ufusr_cleanup(void)
{
    try
    {
        //---- Enter your callback code here -----
    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        WindowsTest::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }
}


int WindowsTest::Show()
{
    try
    {
        theDialog->Show();
    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        WindowsTest::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }
    return 0;
}




void WindowsTest::initialize_cb()
{
    try
    {
        group0 = dynamic_cast(theDialog->TopBlock()->FindBlock("group0"));
        button0 = dynamic_cast(theDialog->TopBlock()->FindBlock("button0"));
    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        WindowsTest::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }
}




void WindowsTest::dialogShown_cb()
{
    try
    {
		if (bmp_hWnd != NULL)//销毁上次的窗口句柄
		{
			::DestroyWindow(bmp_hWnd);
			bmp_hWnd = NULL;
		}
    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        WindowsTest::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }
}




int WindowsTest::apply_cb()
{
    int errorCode = 0;
    try
    {
        //---- Enter your callback code here -----
    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        errorCode = 1;
        WindowsTest::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }
    return errorCode;
}




int WindowsTest::update_cb(NXOpen::BlockStyler::UIBlock* block)
{
    try
    {
        if(block == button0)
        {
			//获取鼠标所在位置点
			POINT p;
			GetCursorPos(&p);

			if (bmp_hWnd != NULL)//销毁上次的窗口句柄
			{
				::DestroyWindow(bmp_hWnd);
				bmp_hWnd = NULL;
			}

			int status;
			UF_CFI_ask_file_exist("D:\\316-32G.bmp", &status);
			if (status)//bmp不存在
			{
				uc1601("bmp不存在",1);
				return 0;
			}

			//获取bmp大小
			CImage bmpImage;
			bmp_path = "D:\\316-32G.bmp";
			bmpImage.Load(bmp_path);
			int bmp_width = bmpImage.GetWidth();
			int bmp_height = bmpImage.GetHeight();
			bmpImage.Destroy();

			// 窗口属性初始化
			HINSTANCE hIns = GetModuleHandle(0);
			WNDCLASSEX wc;
			wc.cbSize = sizeof(wc);								// 定义结构大小
			wc.style = CS_HREDRAW | CS_VREDRAW;					// 如果改变了客户区域的宽度或高度,则重新绘制整个窗口 
			wc.cbClsExtra = 0;									// 窗口结构的附加字节数
			wc.cbWndExtra = 0;									// 窗口实例的附加字节数
			wc.hInstance = hIns;								// 本模块的实例句柄
			wc.hIcon = NULL;									// 图标的句柄
			wc.hIconSm = NULL;									// 和窗口类关联的小图标的句柄
			wc.hbrBackground = (HBRUSH)COLOR_WINDOW;			// 背景画刷的句柄
			wc.hCursor = NULL;									// 光标的句柄
			wc.lpfnWndProc = __WndProc;							// 窗口处理函数的指针
			wc.lpszMenuName = NULL;								// 指向菜单的指针
			wc.lpszClassName = L"BmpWindow";						// 指向类名称的指针
			//窗口类已经注册,则取消注册
			UnregisterClassW(L"BmpWindow", hIns);
			// 为窗口注册一个窗口类
			if (!RegisterClassEx(&wc)) 
			{
				uc1601("窗口类注册失败", 1);
				return 0;
			}

			int pos_x = p.x - bmp_width - 20 > 0 ? p.x - bmp_width - 20 : 0;// 窗口初始x坐标
			int pos_y = p.y - bmp_height / 2 > 0 ? p.y - bmp_height / 2 : 0;// 窗口初始y坐标

			// 创建窗口类的实例
			bmp_hWnd = CreateWindowEx(
				WS_EX_TOPMOST,				// 窗口扩展样式:顶级窗口
				L"BmpWindow",				// 窗口类名
				L"详细参数",					// 窗口标题
				WS_OVERLAPPEDWINDOW,		// 窗口样式:重叠窗口
				pos_x,						// 窗口初始x坐标
				pos_y,						// 窗口初始y坐标
				bmp_width,					// 窗口宽度
				bmp_height,					// 窗口高度
				NULL,						// 父窗口句柄
				NULL,						// 菜单句柄 
				hIns,						// 与窗口关联的模块实例的句柄
				NULL						// 用来传递给窗口WM_CREATE消息
				);


			if (bmp_hWnd == NULL)
			{
				uc1601("创建窗口类实例失败", 1);
				return 0;
			}

			//更新与显示窗口
			UpdateWindow(bmp_hWnd);
			ShowWindow(bmp_hWnd, SW_SHOW);
        }
    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        WindowsTest::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }
    return 0;
}




int WindowsTest::ok_cb()
{
    int errorCode = 0;
    try
    {
        errorCode = apply_cb();
    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        errorCode = 1;
        WindowsTest::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }
    return errorCode;
}



PropertyList* WindowsTest::GetBlockProperties(const char *blockID)
{
    return theDialog->GetBlockProperties(blockID);
}

你可能感兴趣的:(NX二次开发专栏,NX二次开发)