不说害怕
生活不止苟与且, 还有诗和远方
C++博客 | 首页 | 发新随笔 | 发新文章 | 联系 | 聚合 | 管理

2013年7月25日

GDI+中用TextureBrush来FillRectangle的问题
在给对话框背景贴图的时候, 使用了九宫格的切图方式.
先平铺: 中, 上下左右, 4条边.
再单独绘制4个角 和标题 图片.
为了平铺方便. 使用了 TextureBrush来FillRectangle. 没有使用DrawImage.
原本是为了简单. 谁知TextureBrush怎么画都不对. 
TextureBrush _Brush(&_Image);
Rect _Rect(x,y,w,h);
_Gfx.FillRectangle(&_Brush,_Rect);
除了在FillRectangle的左边在0,0的时候是正确的.其他都偏了, 很乱.
研究了一下.
原来TextureBrush在FillRectangle的时候在内部会从0,0位置开始平铺好, 然后再取一块_Rect矩形区域贴到你需要绘制的地方.
需要加一句才能正确绘制: TranslateTransform
TextureBrush _Brush(&_Image);
Rect _Rect(x,y,w,h);
_Brush.TranslateTransform(x,y);
_Gfx.FillRectangle(&_Brush,_Rect);
posted @ 2013-07-25 17:28 zhupf 阅读(931) | 评论 (0) | 编辑 收藏
 

2013年7月2日

WinHttp支持HTTPS下载
#include "windows.h"
#include "winhttp.h"
#include "wchar.h"
#pragma comment(lib,"Winhttp.lib")
// SSL (Secure Sockets Layer) example
// compile for console
void main()
{
HINTERNET hOpen = 0;
HINTERNET hConnect = 0;
HINTERNET hRequest = 0;
IStream *stream = NULL;
HRESULT hr;
while (1)
{
hOpen = WinHttpOpen(L"Aurora Console App", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
if (!hOpen) {
wprintf(L"WinHttpOpen failed (0x%.8X)\n", GetLastError());
break;
}
hConnect = WinHttpConnect(hOpen, L"raw.github.com", INTERNET_DEFAULT_HTTPS_PORT, 0);
if (!hConnect) {
wprintf(L"WinHttpConnect failed (0x%.8X)\n", GetLastError());
break;
}
LPCWSTR types[2];
types[0] = L"text/html";
types[1] = 0;
// use flag WINHTTP_FLAG_SECURE to initiate SSL
hRequest = WinHttpOpenRequest(hConnect, L"GET", L"zpfzzz/test/master/README.md",
NULL, WINHTTP_NO_REFERER, &types[0], WINHTTP_FLAG_SECURE);
if (!hRequest)
{
wprintf(L"WinHttpOpenRequest failed (0x%.8X)\n", GetLastError());
break;
}
if (!WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0))
{
wprintf(L"WinHttpSendRequest failed (0x%.8X)\n", GetLastError());
break;
}
if (!WinHttpReceiveResponse(hRequest, 0))
{
wprintf(L"WinHttpReceiveResponse failed (0x%.8X)\n", GetLastError());
break;
}
// query remote file size, set haveContentLength on success and dwContentLength to the length
wchar_t szContentLength[32];
DWORD cch = 64;
DWORD dwHeaderIndex = WINHTTP_NO_HEADER_INDEX;
BOOL haveContentLength = WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_CONTENT_LENGTH, NULL,
&szContentLength, &cch, &dwHeaderIndex);
DWORD dwContentLength;
if (haveContentLength) dwContentLength = _wtoi(szContentLength);
// read the response into memory stream
hr = CreateStreamOnHGlobal(0, true, &stream);
if (hr) {
wprintf(L"CreateStreamOnHGlobal failed (0x%.8X)\n", hr);
break;
}
// allocate buffer for streaming received data
char *p = new char[4096];
if (!p)
{
wprintf(L"failed to allocate buffer\n");
break;
}
// to receive all data, we need to enter a loop
DWORD dwReceivedTotal = 0;
while (WinHttpQueryDataAvailable(hRequest, &cch) && cch)
{
if (cch > 4096) cch = 4096;
dwReceivedTotal += cch;
// display number of received bytes
if (haveContentLength)
{
wprintf(L"received %d of %d (%d%%)%c", dwReceivedTotal, dwContentLength, 
dwReceivedTotal*100/dwContentLength, 13);
}
else {
wprintf(L"received %d (unknown length)%c", dwReceivedTotal, 10);
}
WinHttpReadData(hRequest, p, cch, &cch);
// write into stream
hr = stream->Write(p, cch, NULL);
if (hr)
{
wprintf(L"failed to write data to stream (0x%.8X)\n", hr);
}
}
delete [] p;
wprintf(L"\n\nreceived all data.\n");
// terminate the sream with a NULL
p = NULL;
stream->Write(&p, 1, NULL);
// get pointer to stream bytes
wprintf(L"getting HGLOBAL from stream...\n");
HGLOBAL hgl;
hr = GetHGlobalFromStream(stream, &hgl);
if (hr) {
wprintf(L"GetHGlobalFromStream failed (0x%.8X)\n", hr);
break;
}
wprintf(L"locking memory...\n");
p = (char*)GlobalLock(hgl);
if (!p)
{
wprintf(L"GlobalLock failed (0x%.8X)\n", GetLastError());
break;
}
wprintf(L"displaying received data...\n");
// terminate the string at 1024 bytes (MessageBox lag)
//if (dwReceivedTotal > 1024) dwReceivedTotal = 1024;
//*p[dwReceivedTotal] = 0;
MessageBoxA(0, p, "", 0);
GlobalUnlock(hgl);
break;
}
// delete stream and close handles
if (stream) stream->Release();
if (hRequest) WinHttpCloseHandle(hRequest);
if (hConnect) WinHttpCloseHandle(hConnect);
if (hOpen) WinHttpCloseHandle(hOpen);
system("pause");
}
posted @ 2013-07-02 14:44 zhupf 阅读(3555) | 评论 (0) | 编辑 收藏
 
仅列出标题  
随笔:2 文章:0 评论:0 引用:0
<2025年5月>
日一二三四五六
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

常用链接

  • 我的随笔
  • 我的评论
  • 我参与的随笔

留言簿

  • 给我留言
  • 查看公开留言
  • 查看私人留言

随笔档案

  • 2013年7月 (2)

搜索

  •  

最新评论

阅读排行榜

  • 1. WinHttp支持HTTPS下载(3555)
  • 2. GDI+中用TextureBrush来FillRectangle的问题(931)

评论排行榜

  • 1. WinHttp支持HTTPS下载(0)
  • 2. GDI+中用TextureBrush来FillRectangle的问题(0)

Powered by: 博客园
模板提供:沪江博客
Copyright ©2025 zhupf