|
I'm not so sure that's necessarily a bad thing. A lot of folks don't answer question in the Q/A section, so he's maximizing his coverage.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997
|
|
|
|
|
You're right. Thought about this since I'm quite new to the Q/A section and it's role. Still decided (perhaps over eagerly ) to post the info about the duplicate question to notify that discussions may already be found on another section.
|
|
|
|
|
But it is still mildly annoying to answer a question in one forum only to find it has already been answered in another. Especially when you have spent some time researching/testing in order to come up with a reasonable response.
I must get a clever new signature for 2011.
|
|
|
|
|
I am trying to send an "F2" keystroke to a window to refresh it.
I would like to send it to the window while it is minimized, I have Vista just as a little more information.
Here is the code I was testing on, it seems like PostMessage might be my best option, I am able to find the window handle through the code below, just not sure how to send an "F2" to it when minimized.
Thanks for reading.
private void button1_Click(object sender, EventArgs e)
{
const int WM_KEYDOWN = 0x100;
const int WM_KEYUP = 0x101;
Thread.Sleep(3000);
Process[] processes = Process.GetProcessesByName("keytest.txt - Notepad");
foreach (Process p in processes)
{
IntPtr pFoundWindow = p.MainWindowHandle;
}
MessageBox.Show("Done");
}
|
|
|
|
|
This[^] link any good for you? I am sure that's what you want
Illogical thoughts make me ill
|
|
|
|
|
Hi,
Thanks for the response.
I believe I am misunderstanding something, my code is below, can you tell me what I am doing wrong?
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Net;
using Strings;
using System.IO;
using System.Text.RegularExpressions;
using System.Net.Mail;
using System.Threading;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace GenericTester
{
public partial class Form1 : Form
{
private static uint SC_RESTORE = 0xF120;
private static uint SC_MAXIMIZE = 0xF030;
private static uint SC_MINIMIZE = 0xF020;
private static uint WM_SYSCOMMAND = 0x0112;
[DllImport("user32.dll")]
public static extern bool PostMessage(IntPtr hWnd, [MarshalAs(UnmanagedType.U4)] uint Msg, IntPtr wParam, IntPtr lParam);
[DllImport("User32.Dll", EntryPoint = "PostMessageA")]
static extern bool PostMessage(IntPtr hWnd, uint msg, int wParam, int lParam);
[DllImport("user32.dll", EntryPoint = "FindWindowA")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
static extern byte VkKeyScan(string ch);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
const int WM_KEYDOWN = 0x100;
const int WM_KEYUP = 0x101;
Thread.Sleep(3000);
Process[] processes = Process.GetProcessesByName("javaw");
foreach (Process p in processes)
{
IntPtr pFoundWindow = p.MainWindowHandle;
PostMessage(pFoundWindow, WM_KEYDOWN, VkKeyScan("{F2}"), 0);
}
MessageBox.Show("Done");
}
|
|
|
|
|
Firstly, do you need that Thread.Sleep?
For the Function Keys I have found it works with the Keycode value. I tried the following with my browser and it refreshed the page. You will need to change the Keycode from F5 to F2.
F2 = 0x71
F5 = 0x74
See here[^] for full list of codes
foreach(Process process in Process.GetProcessesByName(textBoxProcessName.Text))
{
IntPtr hWnd = process.MainWindowHandle;
PostMessage(hWnd, WM_KEYDOWN, 0x74, 0);
}
hope this helps
Illogical thoughts make me ill
modified on Thursday, February 17, 2011 12:41 PM
|
|
|
|
|
Also, are you sure "javaw" is the right window you want?
Illogical thoughts make me ill
|
|
|
|
|
Hi,
Yes the process in task manager is called javaw ... the code below is the only way I can seem to get it to work? For some reason it requires the Thread.Sleep(2000) or it happens to quickly.
I would rather not use SendKeys, as I would like to send the "F2" keystroke to the window when it is minimized, any ideas?
private void button1_Click(object sender, EventArgs e)
{
const int WM_KEYDOWN = 0x100;
const int WM_KEYUP = 0x101;
Process[] processes = null;
if (Process.GetProcessesByName("javaw") != null)
{
processes = Process.GetProcessesByName("javaw");
}
foreach (Process p in processes)
{
IntPtr pFoundWindow = p.MainWindowHandle;
SetForegroundWindow(pFoundWindow.ToInt32());
Thread.Sleep(2000);
SendKeys.Send("{F2}");
PostMessage(pFoundWindow, WM_KEYDOWN, 0x71, 0);
}
}
|
|
|
|
|
In addition to the other advice you have been given, you should consider that F5 is pretty much the standard key to use for refresh.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
I wouldn't let CG touch my Abacus!
When you're wrestling a gorilla, you don't stop when you're tired, you stop when the gorilla is.
|
|
|
|
|
Yes it is normally f5, our developers must have overlooked that unfortunately
|
|
|
|
|
First of all I don't understand what a refresh is going to do on a window you are not seeing anyway.
And then I have some doubt a minimized window will be receiving anything.
As your target app seems to be an in-house development, this is what I would suggest:
- create an extra window, make it visible, not shown in taskbar, off-screen, and with a unique title;
- have it override its WndProc and check for a special message (a WM_USER kind of message, not a regular one);
The other app now should:
- locate the target app;
- find the off-screen window based on its title;
- send the Window message your target app will react on.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
Hi all,
I have been looking at some example code around and wonder if anyone knows what the differences/uses/benefits are of the following two examples for creating an instance of a class (if there are any at all)
Example 1: The Norm (in my experience)
Class Code:
public Person(){
}
Usage Code:
Person person = new Person();
Example 2: Via a Static Method
Class Code:
public static Person CreatePerson(){
return new Person();
}
Usage Code:
Person person = Person.CreatePerson();
Thanks
Illogical thoughts make me ill
|
|
|
|
|
http://henko.net/imperfection/constructors-and-creation-methods/[^]
TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L
%^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2
W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN%
R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-iTV.C\y<pjxsg-b$f4ia>
-----------------------------------------------
128 bit encrypted signature, crack if you can
|
|
|
|
|
Thanks, I guess I was lacking the term 'Creation Method' is my failed Google attempts.
I think the example use I has seen was perhaps a miss use of this technique, unless of course I missed the point.
My understanding is that the Creation Method would be used if there where multiple (almost template like) default variations of a class. Obviously this could still be achieved by a constructor but I suppose the Create Method way provides distinction as to what you are actually doing.
Perhaps I would be correct in stating the following?..
- If you only have one default set up for a class, use a constructor
- If you have multiple commonly used default set ups then consider a Creation Method style approach
...I guess the later should be used if you were considering using a 'switch' type parameter in a class constructor. i.e. Stick to only property setting parameters in constructors (of course, in .Net 4.0 we don't even need to provide overloads for this )
Illogical thoughts make me ill
|
|
|
|
|
musefan wrote: perhaps a miss use of this technique
Yes, probably.
The benefit of well-defined Design Patterns is that they allow developers to discuss situations clearly. The downside is when developers don't understand them, how to implement, and when to implement them.
|
|
|
|
|
|
Thanks, I read into that from the other Link.
It all makes more sense logically now, I think the problem that I didn't get was with the example I used, which was basically as I had seen in some same code when looking into something else. Misused I think
Illogical thoughts make me ill
|
|
|
|
|
Hi guys...
I have need some help cuz I'm totally lost of how I should figure this out...
My case is:
- Make a storage function, so text documents like PDF, docx, doc, can be stored in the Database (MSSQL08 express).
Functions:
- Show files in a ListView (check)
- Upload files selected from the client, and store them as varbinary(max) in the Database (check)
- Download the selected file on the ListView to the correct documenttype on the clients PC (uncheck)
My database table is quiet simple and look something like:
ID - int (Primary key)NOT NULL (is identity)
FileName - varchar(50)NOT NULL
FileContent - varbinary(MAX)NOT NULL
FileExt - varchar(8) NOT NULL
FileComment - varchar(MAX) NULL
So as you can see, I can with ease locate if the selected file got .doc og .pdf extension.
The storage of a file into the varbinary of byte[] works brilliant!
But I'm stucked at how do I download those selected files back to the client, so they look excatly the same as when the document got uploaded to the db?
I mainly have two types of documents stored:
- WORD files
- PDF files
Can anyone provide at codesnippet or anything, that shows how to make a varbinary file (with bytes) into a appropreated document on the client computer readable as WORD document and/or PDF?
|
|
|
|
|
Please refer to [this^] example for similar task.
What i can say is you can read bytes(varbinary) from SQL database and create a doc file, which you can open and preview to user. But, user cannot do on its own from database.
|
|
|
|
|
The view function is kinda cool, but my problem is, the code ain't ASP, but plain old winforms, and can't find a link between the asp view function and similar function for winforms?
|
|
|
|
|
How did you upload the files into your database in the first place? Assuming you used some form of reading the file from disk, then you need to reverse that process to write it back.
I must get a clever new signature for 2011.
|
|
|
|
|
I used FileStream like this:
public byte[] ReadFile(string filePath)
{
byte[] buffer;
FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
try
{
int length = (int)fileStream.Length;
buffer = new byte[length];
int count;
int sum = 0;
while ((count = fileStream.Read(buffer, sum, length - sum)) > 0)
sum += count;
}
finally
{
fileStream.Close();
}
return buffer;
}
and stored the returned value byte[] as the varbinary into the Database... Try'd to figure the reverse way back, but didn't worked that well tbh, maybe because I didn't do it the write way, but when I stored it as .docx the file was 'damage' and when I store it as .txt there is just binary numbers.
|
|
|
|
|
Well you have not shown what you did to re-create the file so it is difficult to guess what may be the problem. However, if you merely reverse the process above: get the buffer from the database and write to the appropriate file, then it should be correct.
grmihel2 wrote: when I store it as .txt there is just binary numbers.
That is to be expected, .docx files are compressed XML.
I must get a clever new signature for 2011.
|
|
|
|
|
I try'd the following (had no clue where to start, so just gave it a try with BinaryWriter):
public void DownloadFile(string filSti, StoredFile fil)
{
byte[] file = WriteFile(filSti, fil);
BinaryWriter bw = new BinaryWriter(File.Open(@"d:\temp\doctest.docx", FileMode.OpenOrCreate));
bw.Write(file);
bw.Close();
}
Didn't worked that well... It created a .docx file of 32KB (the original was 14KB), and when I try to open it, Word says it cannot be open be cause it may be broken.. Any hint what to do? I have an idea then when I see the light of the end, it would be 'easy' to do the same operation for the PDF documents
|
|
|
|