PathToRegion函数返回0,GetLastError返回OK,怎么办

谁学逆向工程 2017-07-14 10:40:35
#include<Windows.h>
#include<tchar.h>
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE,PSTR,int iCmdShow)
{
static TCHAR szAppName[]=TEXT("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)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName=NULL;
wndclass.lpszClassName=szAppName;

if(!RegisterClass(&wndclass))
{
MessageBox(NULL,TEXT("This NT"),szAppName,MB_ICONERROR);
return 0;
}

hwnd = CreateWindow(szAppName,
TEXT("Hello program"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd,iCmdShow);
UpdateWindow(hwnd);

while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd,UINT message, WPARAM wParam, LPARAM lParam)
{
if(message==WM_CREATE)
{
int x;

HDC hdc=GetDC(hwnd);
x=BeginPath(hdc );// '在窗体上产生"窗体"字路径
x=TextOut (hdc, 0, 0, L"葛军万人编程联盟 荣誉出品", 26);
x=EndPath (hdc);

HRGN hRgn = PathToRegion(hdc);// '将所产生的路径转换为区域

ReleaseDC(hwnd,hdc);

//'设置窗体形状为转换成的区域
x=SetWindowRgn (hwnd, hRgn, 1);

// '删除对象,释放系统资源
DeleteObject( hRgn);
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}







代码执行到64行,PathToRegion函数返回0,GetLastError返回OK($err,hr就是GetLastError)


上面的代码来自于当年学习VB6时候的一个例子,现在想在VC重新实现一个,但是已经不会做了。下面是是当年部分VB6代码,也不知道全不全
Private Sub Form_Load()
Dim hRgn As Long
Me.Height = 1000
Me.Left = 0
Me.Top = 0
Me.Width = 12000

With Me
.Font.Name = "隶书"
.Font.Size = 48
End With


BeginPath Form1.hdc '在窗体上产生"窗体"字路径
TextOut Form1.hdc, 0, 0, "葛军万人编程联盟 荣誉出品", 26
EndPath Form1.hdc

hRgn = PathToRegion(hdc) '将所产生的路径转换为区域

'设置窗体形状为转换成的区域
SetWindowRgn hWnd, hRgn, 1

'删除对象,释放系统资源
DeleteObject hRgn
End Sub
...全文
220 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
schlafenhamster 2017-07-15
  • 打赏
  • 举报
回复
"注意:使用TextOut来创建path时,要注意必须自己重定义字体的。并不是所有的字体都可以创建path的,只有TrueType的字体才能够。而刚好,窗体默认的字体不是TrueType的,所以,不自己重新定义字体话,会看不到效果。。。"
schlafenhamster 2017-07-15
  • 打赏
  • 举报
回复
// 有了 StrokeAndFillPath(dc.m_hDC); // PathToRegion(dc.m_hDC); 会失败 没有 StrokeAndFillPath(dc.m_hDC); 可以成功
schlafenhamster 2017-07-15
  • 打赏
  • 举报
回复

void CBigPenDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	HRGN wndRgn;
	CClientDC dc(this);
	CFont Font;
	CFont *pOldFont;
	if (dc.m_hDC!=NULL)
	{
		Font.CreatePointFont(500,"微软雅黑");// 80=8 point
		dc.BeginPath();
			dc.SetBkMode(TRANSPARENT); 
			pOldFont = dc.SelectObject(&Font);
			dc.TextOut(0, 0, "你好!");
			dc.SelectObject(pOldFont);
		dc.EndPath();
//
		StrokeAndFillPath(dc.m_hDC);
		wndRgn=::PathToRegion(dc.m_hDC);
		SetWindowRgn(wndRgn, TRUE); 
	}
}
vc6 win7 没问题

15,979

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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